blob: cd427c5ed878d60b3ffc379efca1f771be1c6055 [file] [log] [blame]
Roland Levillain72bceff2014-09-15 18:29:00 +01001/*
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 "dead_code_elimination.h"
18
19#include "base/bit_vector-inl.h"
20
21namespace art {
22
David Brazdil2d7352b2015-04-20 14:52:42 +010023static void MarkReachableBlocks(HBasicBlock* block, ArenaBitVector* visited) {
24 int block_id = block->GetBlockId();
25 if (visited->IsBitSet(block_id)) {
26 return;
27 }
28 visited->SetBit(block_id);
29
30 HInstruction* last_instruction = block->GetLastInstruction();
31 if (last_instruction->IsIf()) {
32 HIf* if_instruction = last_instruction->AsIf();
33 HInstruction* condition = if_instruction->InputAt(0);
34 if (!condition->IsIntConstant()) {
35 MarkReachableBlocks(if_instruction->IfTrueSuccessor(), visited);
36 MarkReachableBlocks(if_instruction->IfFalseSuccessor(), visited);
37 } else if (condition->AsIntConstant()->IsOne()) {
38 MarkReachableBlocks(if_instruction->IfTrueSuccessor(), visited);
39 } else {
40 DCHECK(condition->AsIntConstant()->IsZero());
41 MarkReachableBlocks(if_instruction->IfFalseSuccessor(), visited);
42 }
43 } else {
44 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
45 MarkReachableBlocks(block->GetSuccessors().Get(i), visited);
46 }
47 }
48}
49
50void HDeadCodeElimination::MaybeRecordDeadBlock(HBasicBlock* block) {
51 if (stats_ != nullptr) {
52 stats_->RecordStat(MethodCompilationStat::kRemovedDeadInstruction,
53 block->GetPhis().CountSize() + block->GetInstructions().CountSize());
54 }
55}
56
57void HDeadCodeElimination::RemoveDeadBlocks() {
58 // Classify blocks as reachable/unreachable.
59 ArenaAllocator* allocator = graph_->GetArena();
60 ArenaBitVector live_blocks(allocator, graph_->GetBlocks().Size(), false);
61 MarkReachableBlocks(graph_->GetEntryBlock(), &live_blocks);
62
63 // Remove all dead blocks. Process blocks in post-order, because removal needs
64 // the block's chain of dominators.
65 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
66 HBasicBlock* block = it.Current();
67 if (live_blocks.IsBitSet(block->GetBlockId())) {
David Brazdil69a28042015-04-29 17:16:07 +010068 // If this block is part of a loop that is being dismantled, we need to
69 // update its loop information.
70 block->UpdateLoopInformation();
71 } else {
72 MaybeRecordDeadBlock(block);
73 block->DisconnectAndDelete();
David Brazdil2d7352b2015-04-20 14:52:42 +010074 }
David Brazdil2d7352b2015-04-20 14:52:42 +010075 }
76
77 // Connect successive blocks created by dead branches. Order does not matter.
78 for (HReversePostOrderIterator it(*graph_); !it.Done();) {
79 HBasicBlock* block = it.Current();
80 if (block->IsEntryBlock() || block->GetSuccessors().Size() != 1u) {
81 it.Advance();
82 continue;
83 }
84 HBasicBlock* successor = block->GetSuccessors().Get(0);
85 if (successor->IsExitBlock() || successor->GetPredecessors().Size() != 1u) {
86 it.Advance();
87 continue;
88 }
89 block->MergeWith(successor);
90
91 // Reiterate on this block in case it can be merged with its new successor.
92 }
93}
94
95void HDeadCodeElimination::RemoveDeadInstructions() {
Roland Levillain72bceff2014-09-15 18:29:00 +010096 // Process basic blocks in post-order in the dominator tree, so that
David Brazdil2d7352b2015-04-20 14:52:42 +010097 // a dead instruction depending on another dead instruction is removed.
Roland Levillain72bceff2014-09-15 18:29:00 +010098 for (HPostOrderIterator b(*graph_); !b.Done(); b.Advance()) {
99 HBasicBlock* block = b.Current();
100 // Traverse this block's instructions in backward order and remove
101 // the unused ones.
102 HBackwardInstructionIterator i(block->GetInstructions());
103 // Skip the first iteration, as the last instruction of a block is
104 // a branching instruction.
105 DCHECK(i.Current()->IsControlFlow());
106 for (i.Advance(); !i.Done(); i.Advance()) {
107 HInstruction* inst = i.Current();
108 DCHECK(!inst->IsControlFlow());
Roland Levillaine161a2a2014-10-03 12:45:18 +0100109 if (!inst->HasSideEffects()
110 && !inst->CanThrow()
111 && !inst->IsSuspendCheck()
Calin Juravle27df7582015-04-17 19:12:31 +0100112 && !inst->IsMemoryBarrier() // If we added an explicit barrier then we should keep it.
Roland Levillaine161a2a2014-10-03 12:45:18 +0100113 && !inst->HasUses()) {
Roland Levillain72bceff2014-09-15 18:29:00 +0100114 block->RemoveInstruction(inst);
Calin Juravle8f20bdb2015-04-21 14:07:50 +0100115 MaybeRecordStat(MethodCompilationStat::kRemovedDeadInstruction);
Roland Levillain72bceff2014-09-15 18:29:00 +0100116 }
117 }
118 }
119}
120
David Brazdil2d7352b2015-04-20 14:52:42 +0100121void HDeadCodeElimination::Run() {
122 RemoveDeadBlocks();
123 RemoveDeadInstructions();
124}
125
Roland Levillain72bceff2014-09-15 18:29:00 +0100126} // namespace art