blob: fdfe518e9592e88909571df819d5a81e665e1b5f [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"
David Brazdil84daae52015-05-18 12:06:52 +010020#include "ssa_phi_elimination.h"
Roland Levillain72bceff2014-09-15 18:29:00 +010021
22namespace art {
23
David Brazdil2d7352b2015-04-20 14:52:42 +010024static void MarkReachableBlocks(HBasicBlock* block, ArenaBitVector* visited) {
25 int block_id = block->GetBlockId();
26 if (visited->IsBitSet(block_id)) {
27 return;
28 }
29 visited->SetBit(block_id);
30
31 HInstruction* last_instruction = block->GetLastInstruction();
32 if (last_instruction->IsIf()) {
33 HIf* if_instruction = last_instruction->AsIf();
34 HInstruction* condition = if_instruction->InputAt(0);
35 if (!condition->IsIntConstant()) {
36 MarkReachableBlocks(if_instruction->IfTrueSuccessor(), visited);
37 MarkReachableBlocks(if_instruction->IfFalseSuccessor(), visited);
38 } else if (condition->AsIntConstant()->IsOne()) {
39 MarkReachableBlocks(if_instruction->IfTrueSuccessor(), visited);
40 } else {
41 DCHECK(condition->AsIntConstant()->IsZero());
42 MarkReachableBlocks(if_instruction->IfFalseSuccessor(), visited);
43 }
44 } else {
45 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
46 MarkReachableBlocks(block->GetSuccessors().Get(i), visited);
47 }
48 }
49}
50
David Brazdila4b8c212015-05-07 09:59:30 +010051static void MarkLoopHeadersContaining(const HBasicBlock& block, ArenaBitVector* set) {
52 for (HLoopInformationOutwardIterator it(block); !it.Done(); it.Advance()) {
53 set->SetBit(it.Current()->GetHeader()->GetBlockId());
54 }
55}
56
David Brazdil2d7352b2015-04-20 14:52:42 +010057void HDeadCodeElimination::MaybeRecordDeadBlock(HBasicBlock* block) {
58 if (stats_ != nullptr) {
59 stats_->RecordStat(MethodCompilationStat::kRemovedDeadInstruction,
60 block->GetPhis().CountSize() + block->GetInstructions().CountSize());
61 }
62}
63
64void HDeadCodeElimination::RemoveDeadBlocks() {
65 // Classify blocks as reachable/unreachable.
66 ArenaAllocator* allocator = graph_->GetArena();
67 ArenaBitVector live_blocks(allocator, graph_->GetBlocks().Size(), false);
David Brazdila4b8c212015-05-07 09:59:30 +010068 ArenaBitVector affected_loops(allocator, graph_->GetBlocks().Size(), false);
69
David Brazdil2d7352b2015-04-20 14:52:42 +010070 MarkReachableBlocks(graph_->GetEntryBlock(), &live_blocks);
71
David Brazdila4b8c212015-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 Brazdila4b8c212015-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 Brazdil69a28042015-04-29 17:16:07 +010083 } else {
84 MaybeRecordDeadBlock(block);
David Brazdila4b8c212015-05-07 09:59:30 +010085 MarkLoopHeadersContaining(*block, &affected_loops);
David Brazdil69a28042015-04-29 17:16:07 +010086 block->DisconnectAndDelete();
David Brazdil2d7352b2015-04-20 14:52:42 +010087 }
David Brazdil2d7352b2015-04-20 14:52:42 +010088 }
89
90 // Connect successive blocks created by dead branches. Order does not matter.
91 for (HReversePostOrderIterator it(*graph_); !it.Done();) {
92 HBasicBlock* block = it.Current();
93 if (block->IsEntryBlock() || block->GetSuccessors().Size() != 1u) {
94 it.Advance();
95 continue;
96 }
97 HBasicBlock* successor = block->GetSuccessors().Get(0);
98 if (successor->IsExitBlock() || successor->GetPredecessors().Size() != 1u) {
99 it.Advance();
100 continue;
101 }
102 block->MergeWith(successor);
103
104 // Reiterate on this block in case it can be merged with its new successor.
105 }
106}
107
108void HDeadCodeElimination::RemoveDeadInstructions() {
Roland Levillain72bceff2014-09-15 18:29:00 +0100109 // Process basic blocks in post-order in the dominator tree, so that
David Brazdil2d7352b2015-04-20 14:52:42 +0100110 // a dead instruction depending on another dead instruction is removed.
Roland Levillain72bceff2014-09-15 18:29:00 +0100111 for (HPostOrderIterator b(*graph_); !b.Done(); b.Advance()) {
112 HBasicBlock* block = b.Current();
113 // Traverse this block's instructions in backward order and remove
114 // the unused ones.
115 HBackwardInstructionIterator i(block->GetInstructions());
116 // Skip the first iteration, as the last instruction of a block is
117 // a branching instruction.
118 DCHECK(i.Current()->IsControlFlow());
119 for (i.Advance(); !i.Done(); i.Advance()) {
120 HInstruction* inst = i.Current();
121 DCHECK(!inst->IsControlFlow());
Roland Levillaine161a2a2014-10-03 12:45:18 +0100122 if (!inst->HasSideEffects()
123 && !inst->CanThrow()
124 && !inst->IsSuspendCheck()
Nicolas Geoffray839188b2015-06-02 10:38:12 +0100125 // If we added an explicit barrier then we should keep it.
126 && !inst->IsMemoryBarrier()
Roland Levillaine161a2a2014-10-03 12:45:18 +0100127 && !inst->HasUses()) {
Roland Levillain72bceff2014-09-15 18:29:00 +0100128 block->RemoveInstruction(inst);
Calin Juravle8f20bdb2015-04-21 14:07:50 +0100129 MaybeRecordStat(MethodCompilationStat::kRemovedDeadInstruction);
Roland Levillain72bceff2014-09-15 18:29:00 +0100130 }
131 }
132 }
133}
134
David Brazdil2d7352b2015-04-20 14:52:42 +0100135void HDeadCodeElimination::Run() {
136 RemoveDeadBlocks();
David Brazdil84daae52015-05-18 12:06:52 +0100137 SsaRedundantPhiElimination(graph_).Run();
David Brazdil2d7352b2015-04-20 14:52:42 +0100138 RemoveDeadInstructions();
139}
140
Roland Levillain72bceff2014-09-15 18:29:00 +0100141} // namespace art