blob: 7a75d260fd8fbd6c2bbe168a943c8a132de02529 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 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
17#include "nodes.h"
Calin Juravle77520bc2015-01-12 18:45:46 +000018
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010019#include "ssa_builder.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000020#include "utils/growable_array.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000021#include "scoped_thread_state_change.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000022
23namespace art {
24
25void HGraph::AddBlock(HBasicBlock* block) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000026 block->SetBlockId(blocks_.Size());
Nicolas Geoffray818f2102014-02-18 16:43:35 +000027 blocks_.Add(block);
28}
29
Nicolas Geoffray804d0932014-05-02 08:46:00 +010030void HGraph::FindBackEdges(ArenaBitVector* visited) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000031 ArenaBitVector visiting(arena_, blocks_.Size(), false);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032 VisitBlockForBackEdges(entry_block_, visited, &visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000033}
34
Roland Levillainfc600dc2014-12-02 17:16:31 +000035static void RemoveAsUser(HInstruction* instruction) {
36 for (size_t i = 0; i < instruction->InputCount(); i++) {
37 instruction->InputAt(i)->RemoveUser(instruction, i);
38 }
39
40 HEnvironment* environment = instruction->GetEnvironment();
41 if (environment != nullptr) {
42 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
David Brazdiled596192015-01-23 10:39:45 +000043 HUseListNode<HEnvironment*>* vreg_env_use = environment->GetInstructionEnvUseAt(i);
44 if (vreg_env_use != nullptr) {
45 HInstruction* vreg = environment->GetInstructionAt(i);
46 DCHECK(vreg != nullptr);
47 vreg->RemoveEnvironmentUser(vreg_env_use);
Roland Levillainfc600dc2014-12-02 17:16:31 +000048 }
49 }
50 }
51}
52
53void HGraph::RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const {
54 for (size_t i = 0; i < blocks_.Size(); ++i) {
55 if (!visited.IsBitSet(i)) {
56 HBasicBlock* block = blocks_.Get(i);
57 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
58 RemoveAsUser(it.Current());
59 }
60 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
61 RemoveAsUser(it.Current());
62 }
63 }
64 }
65}
66
Jean Christophe Beyler53d9da82014-12-04 13:28:25 -080067void HGraph::RemoveBlock(HBasicBlock* block) const {
68 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
69 block->GetSuccessors().Get(j)->RemovePredecessor(block);
70 }
71 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
72 block->RemovePhi(it.Current()->AsPhi());
73 }
74 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
75 block->RemoveInstruction(it.Current());
76 }
77}
78
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000079void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010080 for (size_t i = 0; i < blocks_.Size(); ++i) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081 if (!visited.IsBitSet(i)) {
Jean Christophe Beyler53d9da82014-12-04 13:28:25 -080082 RemoveBlock(blocks_.Get(i));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000083 }
84 }
85}
86
87void HGraph::VisitBlockForBackEdges(HBasicBlock* block,
88 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +010089 ArenaBitVector* visiting) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000090 int id = block->GetBlockId();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000091 if (visited->IsBitSet(id)) return;
92
93 visited->SetBit(id);
94 visiting->SetBit(id);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010095 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
96 HBasicBlock* successor = block->GetSuccessors().Get(i);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000097 if (visiting->IsBitSet(successor->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000098 successor->AddBackEdge(block);
99 } else {
100 VisitBlockForBackEdges(successor, visited, visiting);
101 }
102 }
103 visiting->ClearBit(id);
104}
105
106void HGraph::BuildDominatorTree() {
107 ArenaBitVector visited(arena_, blocks_.Size(), false);
108
109 // (1) Find the back edges in the graph doing a DFS traversal.
110 FindBackEdges(&visited);
111
Roland Levillainfc600dc2014-12-02 17:16:31 +0000112 // (2) Remove instructions and phis from blocks not visited during
113 // the initial DFS as users from other instructions, so that
114 // users can be safely removed before uses later.
115 RemoveInstructionsAsUsersFromDeadBlocks(visited);
116
117 // (3) Remove blocks not visited during the initial DFS.
118 // Step (4) requires dead blocks to be removed from the
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000119 // predecessors list of live blocks.
120 RemoveDeadBlocks(visited);
121
Roland Levillainfc600dc2014-12-02 17:16:31 +0000122 // (4) Simplify the CFG now, so that we don't need to recompute
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100123 // dominators and the reverse post order.
124 SimplifyCFG();
125
Roland Levillainfc600dc2014-12-02 17:16:31 +0000126 // (5) Compute the immediate dominator of each block. We visit
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000127 // the successors of a block only when all its forward branches
128 // have been processed.
129 GrowableArray<size_t> visits(arena_, blocks_.Size());
130 visits.SetSize(blocks_.Size());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100131 reverse_post_order_.Add(entry_block_);
132 for (size_t i = 0; i < entry_block_->GetSuccessors().Size(); i++) {
133 VisitBlockForDominatorTree(entry_block_->GetSuccessors().Get(i), entry_block_, &visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000134 }
135}
136
137HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
138 ArenaBitVector visited(arena_, blocks_.Size(), false);
139 // Walk the dominator tree of the first block and mark the visited blocks.
140 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000141 visited.SetBit(first->GetBlockId());
142 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000143 }
144 // Walk the dominator tree of the second block until a marked block is found.
145 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000146 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000147 return second;
148 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000149 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000150 }
151 LOG(ERROR) << "Could not find common dominator";
152 return nullptr;
153}
154
155void HGraph::VisitBlockForDominatorTree(HBasicBlock* block,
156 HBasicBlock* predecessor,
157 GrowableArray<size_t>* visits) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000158 if (block->GetDominator() == nullptr) {
159 block->SetDominator(predecessor);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000160 } else {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000161 block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000162 }
163
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000164 visits->Increment(block->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000165 // Once all the forward edges have been visited, we know the immediate
166 // dominator of the block. We can then start visiting its successors.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000167 if (visits->Get(block->GetBlockId()) ==
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100168 block->GetPredecessors().Size() - block->NumberOfBackEdges()) {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100169 block->GetDominator()->AddDominatedBlock(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100170 reverse_post_order_.Add(block);
171 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
172 VisitBlockForDominatorTree(block->GetSuccessors().Get(i), block, visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000173 }
174 }
175}
176
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177void HGraph::TransformToSsa() {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100178 DCHECK(!reverse_post_order_.IsEmpty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100179 SsaBuilder ssa_builder(this);
180 ssa_builder.BuildSsa();
181}
182
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100183void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) {
184 // Insert a new node between `block` and `successor` to split the
185 // critical edge.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100186 HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100187 AddBlock(new_block);
188 new_block->AddInstruction(new (arena_) HGoto());
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100189 block->ReplaceSuccessor(successor, new_block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100190 new_block->AddSuccessor(successor);
191 if (successor->IsLoopHeader()) {
192 // If we split at a back edge boundary, make the new block the back edge.
193 HLoopInformation* info = successor->GetLoopInformation();
194 if (info->IsBackEdge(block)) {
195 info->RemoveBackEdge(block);
196 info->AddBackEdge(new_block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100197 }
198 }
199}
200
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100201void HGraph::SimplifyLoop(HBasicBlock* header) {
202 HLoopInformation* info = header->GetLoopInformation();
203
204 // If there are more than one back edge, make them branch to the same block that
205 // will become the only back edge. This simplifies finding natural loops in the
206 // graph.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100207 // Also, if the loop is a do/while (that is the back edge is an if), change the
208 // back edge to be a goto. This simplifies code generation of suspend cheks.
209 if (info->NumberOfBackEdges() > 1 || info->GetBackEdges().Get(0)->GetLastInstruction()->IsIf()) {
210 HBasicBlock* new_back_edge = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100211 AddBlock(new_back_edge);
212 new_back_edge->AddInstruction(new (arena_) HGoto());
213 for (size_t pred = 0, e = info->GetBackEdges().Size(); pred < e; ++pred) {
214 HBasicBlock* back_edge = info->GetBackEdges().Get(pred);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100215 back_edge->ReplaceSuccessor(header, new_back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100216 }
217 info->ClearBackEdges();
218 info->AddBackEdge(new_back_edge);
219 new_back_edge->AddSuccessor(header);
220 }
221
222 // Make sure the loop has only one pre header. This simplifies SSA building by having
223 // to just look at the pre header to know which locals are initialized at entry of the
224 // loop.
225 size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges();
226 if (number_of_incomings != 1) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100227 HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100228 AddBlock(pre_header);
229 pre_header->AddInstruction(new (arena_) HGoto());
230
231 ArenaBitVector back_edges(arena_, GetBlocks().Size(), false);
232 HBasicBlock* back_edge = info->GetBackEdges().Get(0);
233 for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) {
234 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
235 if (predecessor != back_edge) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100236 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100237 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238 }
239 }
240 pre_header->AddSuccessor(header);
241 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100242
243 // Make sure the second predecessor of a loop header is the back edge.
244 if (header->GetPredecessors().Get(1) != info->GetBackEdges().Get(0)) {
245 header->SwapPredecessors();
246 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100247
248 // Place the suspend check at the beginning of the header, so that live registers
249 // will be known when allocating registers. Note that code generation can still
250 // generate the suspend check at the back edge, but needs to be careful with
251 // loop phi spill slots (which are not written to at back edge).
252 HInstruction* first_instruction = header->GetFirstInstruction();
253 if (!first_instruction->IsSuspendCheck()) {
254 HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc());
255 header->InsertInstructionBefore(check, first_instruction);
256 first_instruction = check;
257 }
258 info->SetSuspendCheck(first_instruction->AsSuspendCheck());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100259}
260
261void HGraph::SimplifyCFG() {
262 // Simplify the CFG for future analysis, and code generation:
263 // (1): Split critical edges.
264 // (2): Simplify loops by having only one back edge, and one preheader.
265 for (size_t i = 0; i < blocks_.Size(); ++i) {
266 HBasicBlock* block = blocks_.Get(i);
267 if (block->GetSuccessors().Size() > 1) {
268 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
269 HBasicBlock* successor = block->GetSuccessors().Get(j);
270 if (successor->GetPredecessors().Size() > 1) {
271 SplitCriticalEdge(block, successor);
272 --j;
273 }
274 }
275 }
276 if (block->IsLoopHeader()) {
277 SimplifyLoop(block);
278 }
279 }
280}
281
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000282bool HGraph::AnalyzeNaturalLoops() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100283 for (size_t i = 0; i < blocks_.Size(); ++i) {
284 HBasicBlock* block = blocks_.Get(i);
285 if (block->IsLoopHeader()) {
286 HLoopInformation* info = block->GetLoopInformation();
287 if (!info->Populate()) {
288 // Abort if the loop is non natural. We currently bailout in such cases.
289 return false;
290 }
291 }
292 }
293 return true;
294}
295
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000296HNullConstant* HGraph::GetNullConstant() {
297 if (cached_null_constant_ == nullptr) {
298 cached_null_constant_ = new (arena_) HNullConstant();
299 entry_block_->InsertInstructionBefore(cached_null_constant_,
300 entry_block_->GetLastInstruction());
301 }
302 return cached_null_constant_;
303}
304
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000305void HLoopInformation::Add(HBasicBlock* block) {
306 blocks_.SetBit(block->GetBlockId());
307}
308
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100309void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
310 if (blocks_.IsBitSet(block->GetBlockId())) {
311 return;
312 }
313
314 blocks_.SetBit(block->GetBlockId());
315 block->SetInLoop(this);
316 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
317 PopulateRecursive(block->GetPredecessors().Get(i));
318 }
319}
320
321bool HLoopInformation::Populate() {
322 DCHECK_EQ(GetBackEdges().Size(), 1u);
323 HBasicBlock* back_edge = GetBackEdges().Get(0);
324 DCHECK(back_edge->GetDominator() != nullptr);
325 if (!header_->Dominates(back_edge)) {
326 // This loop is not natural. Do not bother going further.
327 return false;
328 }
329
330 // Populate this loop: starting with the back edge, recursively add predecessors
331 // that are not already part of that loop. Set the header as part of the loop
332 // to end the recursion.
333 // This is a recursive implementation of the algorithm described in
334 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
335 blocks_.SetBit(header_->GetBlockId());
336 PopulateRecursive(back_edge);
337 return true;
338}
339
340HBasicBlock* HLoopInformation::GetPreHeader() const {
341 DCHECK_EQ(header_->GetPredecessors().Size(), 2u);
342 return header_->GetDominator();
343}
344
345bool HLoopInformation::Contains(const HBasicBlock& block) const {
346 return blocks_.IsBitSet(block.GetBlockId());
347}
348
349bool HLoopInformation::IsIn(const HLoopInformation& other) const {
350 return other.blocks_.IsBitSet(header_->GetBlockId());
351}
352
353bool HBasicBlock::Dominates(HBasicBlock* other) const {
354 // Walk up the dominator tree from `other`, to find out if `this`
355 // is an ancestor.
356 HBasicBlock* current = other;
357 while (current != nullptr) {
358 if (current == this) {
359 return true;
360 }
361 current = current->GetDominator();
362 }
363 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100364}
365
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100366static void UpdateInputsUsers(HInstruction* instruction) {
367 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
368 instruction->InputAt(i)->AddUseAt(instruction, i);
369 }
370 // Environment should be created later.
371 DCHECK(!instruction->HasEnvironment());
372}
373
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100374void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
Roland Levillain476df552014-10-09 17:51:36 +0100375 DCHECK(!cursor->IsPhi());
376 DCHECK(!instruction->IsPhi());
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100377 DCHECK_EQ(instruction->GetId(), -1);
378 DCHECK_NE(cursor->GetId(), -1);
379 DCHECK_EQ(cursor->GetBlock(), this);
380 DCHECK(!instruction->IsControlFlow());
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100381 instruction->next_ = cursor;
382 instruction->previous_ = cursor->previous_;
383 cursor->previous_ = instruction;
384 if (GetFirstInstruction() == cursor) {
385 instructions_.first_instruction_ = instruction;
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100386 } else {
387 instruction->previous_->next_ = instruction;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100388 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100389 instruction->SetBlock(this);
390 instruction->SetId(GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100391 UpdateInputsUsers(instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100392}
393
Roland Levillainccc07a92014-09-16 14:48:16 +0100394void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
395 HInstruction* replacement) {
396 DCHECK(initial->GetBlock() == this);
397 InsertInstructionBefore(replacement, initial);
398 initial->ReplaceWith(replacement);
399 RemoveInstruction(initial);
400}
401
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100402static void Add(HInstructionList* instruction_list,
403 HBasicBlock* block,
404 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000405 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000406 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100407 instruction->SetBlock(block);
408 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100409 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100410 instruction_list->AddInstruction(instruction);
411}
412
413void HBasicBlock::AddInstruction(HInstruction* instruction) {
414 Add(&instructions_, this, instruction);
415}
416
417void HBasicBlock::AddPhi(HPhi* phi) {
418 Add(&phis_, this, phi);
419}
420
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100421void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
422 DCHECK_EQ(phi->GetId(), -1);
423 DCHECK_NE(cursor->GetId(), -1);
424 DCHECK_EQ(cursor->GetBlock(), this);
425 if (cursor->next_ == nullptr) {
426 cursor->next_ = phi;
427 phi->previous_ = cursor;
428 DCHECK(phi->next_ == nullptr);
429 } else {
430 phi->next_ = cursor->next_;
431 phi->previous_ = cursor;
432 cursor->next_ = phi;
433 phi->next_->previous_ = phi;
434 }
435 phi->SetBlock(this);
436 phi->SetId(GetGraph()->GetNextInstructionId());
437 UpdateInputsUsers(phi);
438}
439
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100440static void Remove(HInstructionList* instruction_list,
441 HBasicBlock* block,
442 HInstruction* instruction) {
443 DCHECK_EQ(block, instruction->GetBlock());
David Brazdiled596192015-01-23 10:39:45 +0000444 DCHECK(instruction->GetUses().IsEmpty());
445 DCHECK(instruction->GetEnvUses().IsEmpty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100446 instruction->SetBlock(nullptr);
447 instruction_list->RemoveInstruction(instruction);
448
Roland Levillainfc600dc2014-12-02 17:16:31 +0000449 RemoveAsUser(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100450}
451
452void HBasicBlock::RemoveInstruction(HInstruction* instruction) {
453 Remove(&instructions_, this, instruction);
454}
455
456void HBasicBlock::RemovePhi(HPhi* phi) {
457 Remove(&phis_, this, phi);
458}
459
David Brazdiled596192015-01-23 10:39:45 +0000460void HEnvironment::CopyFrom(HEnvironment* env) {
461 for (size_t i = 0; i < env->Size(); i++) {
462 HInstruction* instruction = env->GetInstructionAt(i);
463 SetRawEnvAt(i, instruction);
464 if (instruction != nullptr) {
465 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100466 }
David Brazdiled596192015-01-23 10:39:45 +0000467 }
468}
469
470template <typename T>
471static void RemoveFromUseList(T user, size_t input_index, HUseList<T>* list) {
472 HUseListNode<T>* current;
473 for (HUseIterator<HInstruction*> use_it(*list); !use_it.Done(); use_it.Advance()) {
474 current = use_it.Current();
475 if (current->GetUser() == user && current->GetIndex() == input_index) {
476 list->Remove(current);
477 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100478 }
479}
480
Calin Juravle77520bc2015-01-12 18:45:46 +0000481HInstruction* HInstruction::GetNextDisregardingMoves() const {
482 HInstruction* next = GetNext();
483 while (next != nullptr && next->IsParallelMove()) {
484 next = next->GetNext();
485 }
486 return next;
487}
488
489HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
490 HInstruction* previous = GetPrevious();
491 while (previous != nullptr && previous->IsParallelMove()) {
492 previous = previous->GetPrevious();
493 }
494 return previous;
495}
496
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100497void HInstruction::RemoveUser(HInstruction* user, size_t input_index) {
498 RemoveFromUseList(user, input_index, &uses_);
499}
500
David Brazdiled596192015-01-23 10:39:45 +0000501void HInstruction::RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use) {
502 env_uses_.Remove(use);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100503}
504
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100505void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000506 if (first_instruction_ == nullptr) {
507 DCHECK(last_instruction_ == nullptr);
508 first_instruction_ = last_instruction_ = instruction;
509 } else {
510 last_instruction_->next_ = instruction;
511 instruction->previous_ = last_instruction_;
512 last_instruction_ = instruction;
513 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000514}
515
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100516void HInstructionList::RemoveInstruction(HInstruction* instruction) {
517 if (instruction->previous_ != nullptr) {
518 instruction->previous_->next_ = instruction->next_;
519 }
520 if (instruction->next_ != nullptr) {
521 instruction->next_->previous_ = instruction->previous_;
522 }
523 if (instruction == first_instruction_) {
524 first_instruction_ = instruction->next_;
525 }
526 if (instruction == last_instruction_) {
527 last_instruction_ = instruction->previous_;
528 }
529}
530
Roland Levillain6b469232014-09-25 10:10:38 +0100531bool HInstructionList::Contains(HInstruction* instruction) const {
532 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
533 if (it.Current() == instruction) {
534 return true;
535 }
536 }
537 return false;
538}
539
Roland Levillainccc07a92014-09-16 14:48:16 +0100540bool HInstructionList::FoundBefore(const HInstruction* instruction1,
541 const HInstruction* instruction2) const {
542 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
543 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
544 if (it.Current() == instruction1) {
545 return true;
546 }
547 if (it.Current() == instruction2) {
548 return false;
549 }
550 }
551 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
552 return true;
553}
554
Roland Levillain6c82d402014-10-13 16:10:27 +0100555bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
556 if (other_instruction == this) {
557 // An instruction does not strictly dominate itself.
558 return false;
559 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100560 HBasicBlock* block = GetBlock();
561 HBasicBlock* other_block = other_instruction->GetBlock();
562 if (block != other_block) {
563 return GetBlock()->Dominates(other_instruction->GetBlock());
564 } else {
565 // If both instructions are in the same block, ensure this
566 // instruction comes before `other_instruction`.
567 if (IsPhi()) {
568 if (!other_instruction->IsPhi()) {
569 // Phis appear before non phi-instructions so this instruction
570 // dominates `other_instruction`.
571 return true;
572 } else {
573 // There is no order among phis.
574 LOG(FATAL) << "There is no dominance between phis of a same block.";
575 return false;
576 }
577 } else {
578 // `this` is not a phi.
579 if (other_instruction->IsPhi()) {
580 // Phis appear before non phi-instructions so this instruction
581 // does not dominate `other_instruction`.
582 return false;
583 } else {
584 // Check whether this instruction comes before
585 // `other_instruction` in the instruction list.
586 return block->GetInstructions().FoundBefore(this, other_instruction);
587 }
588 }
589 }
590}
591
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100592void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100593 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000594 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
595 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100596 HInstruction* user = current->GetUser();
597 size_t input_index = current->GetIndex();
598 user->SetRawInputAt(input_index, other);
599 other->AddUseAt(user, input_index);
600 }
601
David Brazdiled596192015-01-23 10:39:45 +0000602 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
603 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100604 HEnvironment* user = current->GetUser();
605 size_t input_index = current->GetIndex();
606 user->SetRawEnvAt(input_index, other);
607 other->AddEnvUseAt(user, input_index);
608 }
609
David Brazdiled596192015-01-23 10:39:45 +0000610 uses_.Clear();
611 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100612}
613
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100614void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
615 InputAt(index)->RemoveUser(this, index);
616 SetRawInputAt(index, replacement);
617 replacement->AddUseAt(this, index);
618}
619
Nicolas Geoffray39468442014-09-02 15:17:15 +0100620size_t HInstruction::EnvironmentSize() const {
621 return HasEnvironment() ? environment_->Size() : 0;
622}
623
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100624void HPhi::AddInput(HInstruction* input) {
625 DCHECK(input->GetBlock() != nullptr);
626 inputs_.Add(input);
627 input->AddUseAt(this, inputs_.Size() - 1);
628}
629
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100630#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000631void H##name::Accept(HGraphVisitor* visitor) { \
632 visitor->Visit##name(this); \
633}
634
635FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
636
637#undef DEFINE_ACCEPT
638
639void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100640 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
641 for (size_t i = 0 ; i < blocks.Size(); i++) {
642 VisitBasicBlock(blocks.Get(i));
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643 }
644}
645
Roland Levillain633021e2014-10-01 14:12:25 +0100646void HGraphVisitor::VisitReversePostOrder() {
647 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
648 VisitBasicBlock(it.Current());
649 }
650}
651
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100653 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100654 it.Current()->Accept(this);
655 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100656 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000657 it.Current()->Accept(this);
658 }
659}
660
Roland Levillain9240d6a2014-10-20 16:47:04 +0100661HConstant* HUnaryOperation::TryStaticEvaluation() const {
662 if (GetInput()->IsIntConstant()) {
663 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
664 return new(GetBlock()->GetGraph()->GetArena()) HIntConstant(value);
665 } else if (GetInput()->IsLongConstant()) {
Roland Levillainb762d2e2014-10-22 10:11:06 +0100666 // TODO: Implement static evaluation of long unary operations.
667 //
668 // Do not exit with a fatal condition here. Instead, simply
669 // return `nullptr' to notify the caller that this instruction
670 // cannot (yet) be statically evaluated.
Roland Levillain9240d6a2014-10-20 16:47:04 +0100671 return nullptr;
672 }
673 return nullptr;
674}
675
676HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +0100677 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
678 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
679 GetRight()->AsIntConstant()->GetValue());
Roland Levillain9240d6a2014-10-20 16:47:04 +0100680 return new(GetBlock()->GetGraph()->GetArena()) HIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100681 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
682 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
683 GetRight()->AsLongConstant()->GetValue());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000684 if (GetResultType() == Primitive::kPrimLong) {
685 return new(GetBlock()->GetGraph()->GetArena()) HLongConstant(value);
686 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000687 DCHECK_EQ(GetResultType(), Primitive::kPrimInt);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000688 return new(GetBlock()->GetGraph()->GetArena()) HIntConstant(value);
689 }
Roland Levillain556c3d12014-09-18 15:25:07 +0100690 }
691 return nullptr;
692}
Dave Allison20dfc792014-06-16 20:44:29 -0700693
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100694bool HCondition::IsBeforeWhenDisregardMoves(HIf* if_) const {
Calin Juravle77520bc2015-01-12 18:45:46 +0000695 return this == if_->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100696}
697
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100698bool HInstruction::Equals(HInstruction* other) const {
699 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100700 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100701 if (!InstructionDataEquals(other)) return false;
702 if (GetType() != other->GetType()) return false;
703 if (InputCount() != other->InputCount()) return false;
704
705 for (size_t i = 0, e = InputCount(); i < e; ++i) {
706 if (InputAt(i) != other->InputAt(i)) return false;
707 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100708 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100709 return true;
710}
711
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700712std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
713#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
714 switch (rhs) {
715 FOR_EACH_INSTRUCTION(DECLARE_CASE)
716 default:
717 os << "Unknown instruction kind " << static_cast<int>(rhs);
718 break;
719 }
720#undef DECLARE_CASE
721 return os;
722}
723
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000724void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000725 next_->previous_ = previous_;
726 if (previous_ != nullptr) {
727 previous_->next_ = next_;
728 }
729 if (block_->instructions_.first_instruction_ == this) {
730 block_->instructions_.first_instruction_ = next_;
731 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000732 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000733
734 previous_ = cursor->previous_;
735 if (previous_ != nullptr) {
736 previous_->next_ = this;
737 }
738 next_ = cursor;
739 cursor->previous_ = this;
740 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000741
742 if (block_->instructions_.first_instruction_ == cursor) {
743 block_->instructions_.first_instruction_ = this;
744 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000745}
746
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000747HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
748 DCHECK(!cursor->IsControlFlow());
749 DCHECK_NE(instructions_.last_instruction_, cursor);
750 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000751
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000752 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
753 new_block->instructions_.first_instruction_ = cursor->GetNext();
754 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
755 cursor->next_->previous_ = nullptr;
756 cursor->next_ = nullptr;
757 instructions_.last_instruction_ = cursor;
758
759 new_block->instructions_.SetBlockOfInstructions(new_block);
760 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
761 HBasicBlock* successor = GetSuccessors().Get(i);
762 new_block->successors_.Add(successor);
763 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
764 }
765 successors_.Reset();
766
767 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
768 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
769 dominated->dominator_ = new_block;
770 new_block->dominated_blocks_.Add(dominated);
771 }
772 dominated_blocks_.Reset();
773 return new_block;
774}
775
776void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
777 for (HInstruction* current = first_instruction_;
778 current != nullptr;
779 current = current->GetNext()) {
780 current->SetBlock(block);
781 }
782}
783
784void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
785 DCHECK(Contains(cursor));
786 if (!instruction_list.IsEmpty()) {
787 if (cursor == last_instruction_) {
788 last_instruction_ = instruction_list.last_instruction_;
789 } else {
790 cursor->next_->previous_ = instruction_list.last_instruction_;
791 }
792 instruction_list.last_instruction_->next_ = cursor->next_;
793 cursor->next_ = instruction_list.first_instruction_;
794 instruction_list.first_instruction_->previous_ = cursor;
795 }
796}
797
798void HInstructionList::Add(const HInstructionList& instruction_list) {
799 DCHECK(!IsEmpty());
800 AddAfter(last_instruction_, instruction_list);
801}
802
803void HBasicBlock::MergeWith(HBasicBlock* other) {
804 DCHECK(successors_.IsEmpty()) << "Unimplemented block merge scenario";
805 DCHECK(dominated_blocks_.IsEmpty()) << "Unimplemented block merge scenario";
806 DCHECK(other->GetDominator()->IsEntryBlock() && other->GetGraph() != graph_)
807 << "Unimplemented block merge scenario";
808 DCHECK(other->GetPhis().IsEmpty());
809
810 successors_.Reset();
811 dominated_blocks_.Reset();
812 instructions_.Add(other->GetInstructions());
813 other->GetInstructions().SetBlockOfInstructions(this);
814
815 while (!other->GetSuccessors().IsEmpty()) {
816 HBasicBlock* successor = other->GetSuccessors().Get(0);
817 successor->ReplacePredecessor(other, this);
818 }
819
820 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
821 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
822 dominated_blocks_.Add(dominated);
823 dominated->SetDominator(this);
824 }
825 other->dominated_blocks_.Reset();
826 other->dominator_ = nullptr;
827 other->graph_ = nullptr;
828}
829
830void HBasicBlock::ReplaceWith(HBasicBlock* other) {
831 while (!GetPredecessors().IsEmpty()) {
832 HBasicBlock* predecessor = GetPredecessors().Get(0);
833 predecessor->ReplaceSuccessor(this, other);
834 }
835 while (!GetSuccessors().IsEmpty()) {
836 HBasicBlock* successor = GetSuccessors().Get(0);
837 successor->ReplacePredecessor(this, other);
838 }
839 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
840 other->AddDominatedBlock(dominated_blocks_.Get(i));
841 }
842 GetDominator()->ReplaceDominatedBlock(this, other);
843 other->SetDominator(GetDominator());
844 dominator_ = nullptr;
845 graph_ = nullptr;
846}
847
848// Create space in `blocks` for adding `number_of_new_blocks` entries
849// starting at location `at`. Blocks after `at` are moved accordingly.
850static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
851 size_t number_of_new_blocks,
852 size_t at) {
853 size_t old_size = blocks->Size();
854 size_t new_size = old_size + number_of_new_blocks;
855 blocks->SetSize(new_size);
856 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
857 blocks->Put(j, blocks->Get(i));
858 }
859}
860
861void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000862 // Walk over the entry block and:
863 // - Move constants from the entry block to the outer_graph's entry block,
864 // - Replace HParameterValue instructions with their real value.
865 // - Remove suspend checks, that hold an environment.
866 int parameter_index = 0;
867 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
868 HInstruction* current = it.Current();
869 if (current->IsConstant()) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000870 current->MoveBefore(outer_graph->GetEntryBlock()->GetLastInstruction());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000871 } else if (current->IsParameterValue()) {
872 current->ReplaceWith(invoke->InputAt(parameter_index++));
873 } else {
874 DCHECK(current->IsGoto() || current->IsSuspendCheck());
875 entry_block_->RemoveInstruction(current);
876 }
877 }
878
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000879 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +0000880 // Simple case of an entry block, a body block, and an exit block.
881 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000882 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +0000883 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
884 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000885 DCHECK(!body->IsExitBlock());
886 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000887
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000888 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
889 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000890
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000891 // Replace the invoke with the return value of the inlined graph.
892 if (last->IsReturn()) {
893 invoke->ReplaceWith(last->InputAt(0));
894 } else {
895 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000896 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000897
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000898 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000899 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000900 // Need to inline multiple blocks. We split `invoke`'s block
901 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +0000902 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000903 // with the second half.
904 ArenaAllocator* allocator = outer_graph->GetArena();
905 HBasicBlock* at = invoke->GetBlock();
906 HBasicBlock* to = at->SplitAfter(invoke);
907
908 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
909 DCHECK(!first->IsInLoop());
910 at->MergeWith(first);
911 exit_block_->ReplaceWith(to);
912
913 // Update all predecessors of the exit block (now the `to` block)
914 // to not `HReturn` but `HGoto` instead. Also collect the return
915 // values if any, and potentially make it a phi if there are multiple
916 // predecessors.
917 HInstruction* return_value = nullptr;
918 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
919 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
920 HInstruction* last = predecessor->GetLastInstruction();
921 if (!last->IsReturnVoid()) {
922 if (return_value != nullptr) {
923 if (!return_value->IsPhi()) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +0000924 HPhi* phi = new (allocator) HPhi(allocator, kNoRegNumber, 0, invoke->GetType());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000925 to->AddPhi(phi);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +0000926 phi->AddInput(return_value);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000927 return_value = phi;
928 }
929 return_value->AsPhi()->AddInput(last->InputAt(0));
930 } else {
931 return_value = last->InputAt(0);
932 }
933 }
934 predecessor->AddInstruction(new (allocator) HGoto());
935 predecessor->RemoveInstruction(last);
936 }
937
938 if (return_value != nullptr) {
939 invoke->ReplaceWith(return_value);
940 }
941
942 // Update the meta information surrounding blocks:
943 // (1) the graph they are now in,
944 // (2) the reverse post order of that graph,
945 // (3) the potential loop information they are now in.
946
947 // We don't add the entry block, the exit block, and the first block, which
948 // has been merged with `at`.
949 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
950
951 // We add the `to` block.
952 static constexpr int kNumberOfNewBlocksInCaller = 1;
953 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
954 + kNumberOfNewBlocksInCaller;
955
956 // Find the location of `at` in the outer graph's reverse post order. The new
957 // blocks will be added after it.
958 size_t index_of_at = 0;
959 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
960 index_of_at++;
961 }
962 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
963
964 // Do a reverse post order of the blocks in the callee and do (1), (2),
965 // and (3) to the blocks that apply.
966 HLoopInformation* info = at->GetLoopInformation();
967 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
968 HBasicBlock* current = it.Current();
969 if (current != exit_block_ && current != entry_block_ && current != first) {
970 DCHECK(!current->IsInLoop());
971 DCHECK(current->GetGraph() == this);
972 current->SetGraph(outer_graph);
973 outer_graph->AddBlock(current);
974 outer_graph->reverse_post_order_.Put(++index_of_at, current);
975 if (info != nullptr) {
976 info->Add(current);
977 current->SetLoopInformation(info);
978 }
979 }
980 }
981
982 // Do (1), (2), and (3) to `to`.
983 to->SetGraph(outer_graph);
984 outer_graph->AddBlock(to);
985 outer_graph->reverse_post_order_.Put(++index_of_at, to);
986 if (info != nullptr) {
987 info->Add(to);
988 to->SetLoopInformation(info);
989 if (info->IsBackEdge(at)) {
990 // Only `at` can become a back edge, as the inlined blocks
991 // are predecessors of `at`.
992 DCHECK_EQ(1u, info->NumberOfBackEdges());
993 info->ClearBackEdges();
994 info->AddBackEdge(to);
995 }
996 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000997 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000998
999 // Finally remove the invoke from the caller.
1000 invoke->GetBlock()->RemoveInstruction(invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001001}
1002
Calin Juravleacf735c2015-02-12 15:25:22 +00001003std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1004 ScopedObjectAccess soa(Thread::Current());
1005 os << "["
1006 << " is_top=" << rhs.IsTop()
1007 << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
1008 << " is_exact=" << rhs.IsExact()
1009 << " ]";
1010 return os;
1011}
1012
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001013} // namespace art