blob: 2362cc107309c5e43ec8808b94bd2b1e81c8e98f [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 Brazdile8ff50d2015-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 Brazdile8ff50d2015-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);
Nicolas Geoffray18b236e2015-06-24 12:20:24 +010070 bool removed_one_or_more_blocks = false;
David Brazdil2d7352b2015-04-20 14:52:42 +010071
David Brazdile8ff50d2015-05-07 09:59:30 +010072 // Remove all dead blocks. Iterate in post order because removal needs the
73 // block's chain of dominators and nested loops need to be updated from the
74 // inside out.
David Brazdil2d7352b2015-04-20 14:52:42 +010075 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
76 HBasicBlock* block = it.Current();
David Brazdile8ff50d2015-05-07 09:59:30 +010077 int id = block->GetBlockId();
78 if (live_blocks.IsBitSet(id)) {
79 if (affected_loops.IsBitSet(id)) {
80 DCHECK(block->IsLoopHeader());
81 block->GetLoopInformation()->Update();
82 }
David Brazdil395086f2015-04-29 17:16:07 +010083 } else {
84 MaybeRecordDeadBlock(block);
David Brazdile8ff50d2015-05-07 09:59:30 +010085 MarkLoopHeadersContaining(*block, &affected_loops);
David Brazdil395086f2015-04-29 17:16:07 +010086 block->DisconnectAndDelete();
Nicolas Geoffray18b236e2015-06-24 12:20:24 +010087 removed_one_or_more_blocks = true;
David Brazdil2d7352b2015-04-20 14:52:42 +010088 }
David Brazdil2d7352b2015-04-20 14:52:42 +010089 }
90
Nicolas Geoffray18b236e2015-06-24 12:20:24 +010091 // If we removed at least one block, we need to recompute the full
92 // dominator tree.
93 if (removed_one_or_more_blocks) {
94 graph_->ClearDominanceInformation();
95 graph_->ComputeDominanceInformation();
96 }
97
David Brazdil2d7352b2015-04-20 14:52:42 +010098 // Connect successive blocks created by dead branches. Order does not matter.
99 for (HReversePostOrderIterator it(*graph_); !it.Done();) {
100 HBasicBlock* block = it.Current();
101 if (block->IsEntryBlock() || block->GetSuccessors().Size() != 1u) {
102 it.Advance();
103 continue;
104 }
105 HBasicBlock* successor = block->GetSuccessors().Get(0);
106 if (successor->IsExitBlock() || successor->GetPredecessors().Size() != 1u) {
107 it.Advance();
108 continue;
109 }
110 block->MergeWith(successor);
111
112 // Reiterate on this block in case it can be merged with its new successor.
113 }
114}
115
116void HDeadCodeElimination::RemoveDeadInstructions() {
Roland Levillain72bceff2014-09-15 18:29:00 +0100117 // Process basic blocks in post-order in the dominator tree, so that
David Brazdil2d7352b2015-04-20 14:52:42 +0100118 // a dead instruction depending on another dead instruction is removed.
Roland Levillain72bceff2014-09-15 18:29:00 +0100119 for (HPostOrderIterator b(*graph_); !b.Done(); b.Advance()) {
120 HBasicBlock* block = b.Current();
121 // Traverse this block's instructions in backward order and remove
122 // the unused ones.
123 HBackwardInstructionIterator i(block->GetInstructions());
124 // Skip the first iteration, as the last instruction of a block is
125 // a branching instruction.
126 DCHECK(i.Current()->IsControlFlow());
127 for (i.Advance(); !i.Done(); i.Advance()) {
128 HInstruction* inst = i.Current();
129 DCHECK(!inst->IsControlFlow());
Roland Levillaine161a2a2014-10-03 12:45:18 +0100130 if (!inst->HasSideEffects()
131 && !inst->CanThrow()
132 && !inst->IsSuspendCheck()
Calin Juravle27df7582015-04-17 19:12:31 +0100133 && !inst->IsMemoryBarrier() // If we added an explicit barrier then we should keep it.
Roland Levillaine161a2a2014-10-03 12:45:18 +0100134 && !inst->HasUses()) {
Roland Levillain72bceff2014-09-15 18:29:00 +0100135 block->RemoveInstruction(inst);
Calin Juravle8f20bdb2015-04-21 14:07:50 +0100136 MaybeRecordStat(MethodCompilationStat::kRemovedDeadInstruction);
Roland Levillain72bceff2014-09-15 18:29:00 +0100137 }
138 }
139 }
140}
141
David Brazdil2d7352b2015-04-20 14:52:42 +0100142void HDeadCodeElimination::Run() {
143 RemoveDeadBlocks();
144 RemoveDeadInstructions();
145}
146
Roland Levillain72bceff2014-09-15 18:29:00 +0100147} // namespace art