blob: 67ff87a759166e49dc0f364c9db16f47120cf593 [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
Vladimir Marko211c2112015-09-24 16:52:33 +010019#include "utils/array_ref.h"
Roland Levillain72bceff2014-09-15 18:29:00 +010020#include "base/bit_vector-inl.h"
David Brazdil84daae52015-05-18 12:06:52 +010021#include "ssa_phi_elimination.h"
Roland Levillain72bceff2014-09-15 18:29:00 +010022
23namespace art {
24
Vladimir Marko211c2112015-09-24 16:52:33 +010025static void MarkReachableBlocks(HGraph* graph, ArenaBitVector* visited) {
26 ArenaVector<HBasicBlock*> worklist(graph->GetArena()->Adapter());
27 constexpr size_t kDefaultWorlistSize = 8;
28 worklist.reserve(kDefaultWorlistSize);
29 visited->SetBit(graph->GetEntryBlock()->GetBlockId());
30 worklist.push_back(graph->GetEntryBlock());
David Brazdil2d7352b2015-04-20 14:52:42 +010031
Vladimir Marko211c2112015-09-24 16:52:33 +010032 while (!worklist.empty()) {
33 HBasicBlock* block = worklist.back();
34 worklist.pop_back();
35 int block_id = block->GetBlockId();
36 DCHECK(visited->IsBitSet(block_id));
37
38 ArrayRef<HBasicBlock* const> live_successors(block->GetSuccessors());
39 HInstruction* last_instruction = block->GetLastInstruction();
40 if (last_instruction->IsIf()) {
41 HIf* if_instruction = last_instruction->AsIf();
42 HInstruction* condition = if_instruction->InputAt(0);
43 if (condition->IsIntConstant()) {
44 if (condition->AsIntConstant()->IsOne()) {
45 live_successors = live_successors.SubArray(0u, 1u);
46 DCHECK_EQ(live_successors[0], if_instruction->IfTrueSuccessor());
47 } else {
48 DCHECK(condition->AsIntConstant()->IsZero());
49 live_successors = live_successors.SubArray(1u, 1u);
50 DCHECK_EQ(live_successors[0], if_instruction->IfFalseSuccessor());
51 }
Mark Mendellfe57faa2015-09-18 09:26:15 -040052 }
Vladimir Marko211c2112015-09-24 16:52:33 +010053 } else if (last_instruction->IsPackedSwitch()) {
54 HPackedSwitch* switch_instruction = last_instruction->AsPackedSwitch();
55 HInstruction* switch_input = switch_instruction->InputAt(0);
56 if (switch_input->IsIntConstant()) {
57 int32_t switch_value = switch_input->AsIntConstant()->GetValue();
58 int32_t start_value = switch_instruction->GetStartValue();
Vladimir Marko430c4f52015-09-25 17:10:15 +010059 // Note: Though the spec forbids packed-switch values to wrap around, we leave
60 // that task to the verifier and use unsigned arithmetic with it's "modulo 2^32"
61 // semantics to check if the value is in range, wrapped or not.
62 uint32_t switch_index =
63 static_cast<uint32_t>(switch_value) - static_cast<uint32_t>(start_value);
Vladimir Marko211c2112015-09-24 16:52:33 +010064 if (switch_index < switch_instruction->GetNumEntries()) {
65 live_successors = live_successors.SubArray(switch_index, 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +010066 DCHECK_EQ(live_successors[0], block->GetSuccessors()[switch_index]);
Vladimir Marko211c2112015-09-24 16:52:33 +010067 } else {
68 live_successors = live_successors.SubArray(switch_instruction->GetNumEntries(), 1u);
69 DCHECK_EQ(live_successors[0], switch_instruction->GetDefaultBlock());
70 }
Mark Mendellfe57faa2015-09-18 09:26:15 -040071 }
72 }
Vladimir Marko211c2112015-09-24 16:52:33 +010073
74 for (HBasicBlock* successor : live_successors) {
75 // Add only those successors that have not been visited yet.
76 if (!visited->IsBitSet(successor->GetBlockId())) {
77 visited->SetBit(successor->GetBlockId());
78 worklist.push_back(successor);
79 }
David Brazdil2d7352b2015-04-20 14:52:42 +010080 }
81 }
82}
83
David Brazdila4b8c212015-05-07 09:59:30 +010084static void MarkLoopHeadersContaining(const HBasicBlock& block, ArenaBitVector* set) {
85 for (HLoopInformationOutwardIterator it(block); !it.Done(); it.Advance()) {
86 set->SetBit(it.Current()->GetHeader()->GetBlockId());
87 }
88}
89
David Brazdil2d7352b2015-04-20 14:52:42 +010090void HDeadCodeElimination::MaybeRecordDeadBlock(HBasicBlock* block) {
91 if (stats_ != nullptr) {
92 stats_->RecordStat(MethodCompilationStat::kRemovedDeadInstruction,
93 block->GetPhis().CountSize() + block->GetInstructions().CountSize());
94 }
95}
96
97void HDeadCodeElimination::RemoveDeadBlocks() {
98 // Classify blocks as reachable/unreachable.
99 ArenaAllocator* allocator = graph_->GetArena();
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100100 ArenaBitVector live_blocks(allocator, graph_->GetBlocks().size(), false);
101 ArenaBitVector affected_loops(allocator, graph_->GetBlocks().size(), false);
David Brazdila4b8c212015-05-07 09:59:30 +0100102
Vladimir Marko211c2112015-09-24 16:52:33 +0100103 MarkReachableBlocks(graph_, &live_blocks);
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100104 bool removed_one_or_more_blocks = false;
David Brazdil2d7352b2015-04-20 14:52:42 +0100105
David Brazdila4b8c212015-05-07 09:59:30 +0100106 // Remove all dead blocks. Iterate in post order because removal needs the
107 // block's chain of dominators and nested loops need to be updated from the
108 // inside out.
David Brazdil2d7352b2015-04-20 14:52:42 +0100109 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
110 HBasicBlock* block = it.Current();
David Brazdila4b8c212015-05-07 09:59:30 +0100111 int id = block->GetBlockId();
112 if (live_blocks.IsBitSet(id)) {
113 if (affected_loops.IsBitSet(id)) {
114 DCHECK(block->IsLoopHeader());
115 block->GetLoopInformation()->Update();
116 }
David Brazdil69a28042015-04-29 17:16:07 +0100117 } else {
118 MaybeRecordDeadBlock(block);
David Brazdila4b8c212015-05-07 09:59:30 +0100119 MarkLoopHeadersContaining(*block, &affected_loops);
David Brazdil69a28042015-04-29 17:16:07 +0100120 block->DisconnectAndDelete();
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100121 removed_one_or_more_blocks = true;
David Brazdil2d7352b2015-04-20 14:52:42 +0100122 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100123 }
124
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100125 // If we removed at least one block, we need to recompute the full
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000126 // dominator tree and try block membership.
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100127 if (removed_one_or_more_blocks) {
128 graph_->ClearDominanceInformation();
129 graph_->ComputeDominanceInformation();
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000130 graph_->ComputeTryBlockInformation();
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100131 }
132
David Brazdil2d7352b2015-04-20 14:52:42 +0100133 // Connect successive blocks created by dead branches. Order does not matter.
134 for (HReversePostOrderIterator it(*graph_); !it.Done();) {
135 HBasicBlock* block = it.Current();
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000136 if (block->IsEntryBlock() || !block->GetLastInstruction()->IsGoto()) {
David Brazdil2d7352b2015-04-20 14:52:42 +0100137 it.Advance();
138 continue;
139 }
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000140 HBasicBlock* successor = block->GetSingleSuccessor();
Vladimir Marko60584552015-09-03 13:35:12 +0000141 if (successor->IsExitBlock() || successor->GetPredecessors().size() != 1u) {
David Brazdil2d7352b2015-04-20 14:52:42 +0100142 it.Advance();
143 continue;
144 }
145 block->MergeWith(successor);
146
147 // Reiterate on this block in case it can be merged with its new successor.
148 }
149}
150
151void HDeadCodeElimination::RemoveDeadInstructions() {
Roland Levillain72bceff2014-09-15 18:29:00 +0100152 // Process basic blocks in post-order in the dominator tree, so that
David Brazdil2d7352b2015-04-20 14:52:42 +0100153 // a dead instruction depending on another dead instruction is removed.
Roland Levillain72bceff2014-09-15 18:29:00 +0100154 for (HPostOrderIterator b(*graph_); !b.Done(); b.Advance()) {
155 HBasicBlock* block = b.Current();
156 // Traverse this block's instructions in backward order and remove
157 // the unused ones.
158 HBackwardInstructionIterator i(block->GetInstructions());
159 // Skip the first iteration, as the last instruction of a block is
160 // a branching instruction.
161 DCHECK(i.Current()->IsControlFlow());
162 for (i.Advance(); !i.Done(); i.Advance()) {
163 HInstruction* inst = i.Current();
164 DCHECK(!inst->IsControlFlow());
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100165 if (!inst->HasSideEffects()
Roland Levillaine161a2a2014-10-03 12:45:18 +0100166 && !inst->CanThrow()
167 && !inst->IsSuspendCheck()
David Srbecky0cf44932015-12-09 14:09:59 +0000168 && !inst->IsNativeDebugInfo()
Nicolas Geoffray839188b2015-06-02 10:38:12 +0100169 // If we added an explicit barrier then we should keep it.
170 && !inst->IsMemoryBarrier()
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700171 && !inst->IsParameterValue()
Roland Levillaine161a2a2014-10-03 12:45:18 +0100172 && !inst->HasUses()) {
Roland Levillain72bceff2014-09-15 18:29:00 +0100173 block->RemoveInstruction(inst);
Calin Juravle8f20bdb2015-04-21 14:07:50 +0100174 MaybeRecordStat(MethodCompilationStat::kRemovedDeadInstruction);
Roland Levillain72bceff2014-09-15 18:29:00 +0100175 }
176 }
177 }
178}
179
David Brazdil2d7352b2015-04-20 14:52:42 +0100180void HDeadCodeElimination::Run() {
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000181 RemoveDeadBlocks();
David Brazdil84daae52015-05-18 12:06:52 +0100182 SsaRedundantPhiElimination(graph_).Run();
David Brazdil2d7352b2015-04-20 14:52:42 +0100183 RemoveDeadInstructions();
184}
185
Roland Levillain72bceff2014-09-15 18:29:00 +0100186} // namespace art