blob: 6fbe75e8025a25191f9cbf9c5dfdcba080cdc5ba [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
David Brazdila4b8c212015-05-07 09:59:30 +010050static void MarkLoopHeadersContaining(const HBasicBlock& block, ArenaBitVector* set) {
51 for (HLoopInformationOutwardIterator it(block); !it.Done(); it.Advance()) {
52 set->SetBit(it.Current()->GetHeader()->GetBlockId());
53 }
54}
55
David Brazdil2d7352b2015-04-20 14:52:42 +010056void HDeadCodeElimination::MaybeRecordDeadBlock(HBasicBlock* block) {
57 if (stats_ != nullptr) {
58 stats_->RecordStat(MethodCompilationStat::kRemovedDeadInstruction,
59 block->GetPhis().CountSize() + block->GetInstructions().CountSize());
60 }
61}
62
63void HDeadCodeElimination::RemoveDeadBlocks() {
64 // Classify blocks as reachable/unreachable.
65 ArenaAllocator* allocator = graph_->GetArena();
66 ArenaBitVector live_blocks(allocator, graph_->GetBlocks().Size(), false);
David Brazdila4b8c212015-05-07 09:59:30 +010067 ArenaBitVector affected_loops(allocator, graph_->GetBlocks().Size(), false);
68
David Brazdil2d7352b2015-04-20 14:52:42 +010069 MarkReachableBlocks(graph_->GetEntryBlock(), &live_blocks);
70
David Brazdila4b8c212015-05-07 09:59:30 +010071 // Remove all dead blocks. Iterate in post order because removal needs the
72 // block's chain of dominators and nested loops need to be updated from the
73 // inside out.
David Brazdil2d7352b2015-04-20 14:52:42 +010074 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
75 HBasicBlock* block = it.Current();
David Brazdila4b8c212015-05-07 09:59:30 +010076 int id = block->GetBlockId();
77 if (live_blocks.IsBitSet(id)) {
78 if (affected_loops.IsBitSet(id)) {
79 DCHECK(block->IsLoopHeader());
80 block->GetLoopInformation()->Update();
81 }
David Brazdil69a28042015-04-29 17:16:07 +010082 } else {
83 MaybeRecordDeadBlock(block);
David Brazdila4b8c212015-05-07 09:59:30 +010084 MarkLoopHeadersContaining(*block, &affected_loops);
David Brazdil69a28042015-04-29 17:16:07 +010085 block->DisconnectAndDelete();
David Brazdil2d7352b2015-04-20 14:52:42 +010086 }
David Brazdil2d7352b2015-04-20 14:52:42 +010087 }
88
89 // Connect successive blocks created by dead branches. Order does not matter.
90 for (HReversePostOrderIterator it(*graph_); !it.Done();) {
91 HBasicBlock* block = it.Current();
92 if (block->IsEntryBlock() || block->GetSuccessors().Size() != 1u) {
93 it.Advance();
94 continue;
95 }
96 HBasicBlock* successor = block->GetSuccessors().Get(0);
97 if (successor->IsExitBlock() || successor->GetPredecessors().Size() != 1u) {
98 it.Advance();
99 continue;
100 }
101 block->MergeWith(successor);
102
103 // Reiterate on this block in case it can be merged with its new successor.
104 }
105}
106
107void HDeadCodeElimination::RemoveDeadInstructions() {
Roland Levillain72bceff2014-09-15 18:29:00 +0100108 // Process basic blocks in post-order in the dominator tree, so that
David Brazdil2d7352b2015-04-20 14:52:42 +0100109 // a dead instruction depending on another dead instruction is removed.
Roland Levillain72bceff2014-09-15 18:29:00 +0100110 for (HPostOrderIterator b(*graph_); !b.Done(); b.Advance()) {
111 HBasicBlock* block = b.Current();
112 // Traverse this block's instructions in backward order and remove
113 // the unused ones.
114 HBackwardInstructionIterator i(block->GetInstructions());
115 // Skip the first iteration, as the last instruction of a block is
116 // a branching instruction.
117 DCHECK(i.Current()->IsControlFlow());
118 for (i.Advance(); !i.Done(); i.Advance()) {
119 HInstruction* inst = i.Current();
120 DCHECK(!inst->IsControlFlow());
Roland Levillaine161a2a2014-10-03 12:45:18 +0100121 if (!inst->HasSideEffects()
122 && !inst->CanThrow()
123 && !inst->IsSuspendCheck()
Calin Juravle27df7582015-04-17 19:12:31 +0100124 && !inst->IsMemoryBarrier() // If we added an explicit barrier then we should keep it.
Roland Levillaine161a2a2014-10-03 12:45:18 +0100125 && !inst->HasUses()) {
Roland Levillain72bceff2014-09-15 18:29:00 +0100126 block->RemoveInstruction(inst);
Calin Juravle8f20bdb2015-04-21 14:07:50 +0100127 MaybeRecordStat(MethodCompilationStat::kRemovedDeadInstruction);
Roland Levillain72bceff2014-09-15 18:29:00 +0100128 }
129 }
130 }
131}
132
David Brazdil2d7352b2015-04-20 14:52:42 +0100133void HDeadCodeElimination::Run() {
134 RemoveDeadBlocks();
135 RemoveDeadInstructions();
136}
137
Roland Levillain72bceff2014-09-15 18:29:00 +0100138} // namespace art