blob: fc3dd01ef5f5a55b30966873a82d81b43424f4d7 [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
Roland Levillain75be2832014-10-17 17:02:00 +010023void HDeadCodeElimination::Run() {
Roland Levillain72bceff2014-09-15 18:29:00 +010024 // Process basic blocks in post-order in the dominator tree, so that
25 // a dead instruction depending on another dead instruction is
26 // removed.
27 for (HPostOrderIterator b(*graph_); !b.Done(); b.Advance()) {
28 HBasicBlock* block = b.Current();
29 // Traverse this block's instructions in backward order and remove
30 // the unused ones.
31 HBackwardInstructionIterator i(block->GetInstructions());
32 // Skip the first iteration, as the last instruction of a block is
33 // a branching instruction.
34 DCHECK(i.Current()->IsControlFlow());
35 for (i.Advance(); !i.Done(); i.Advance()) {
36 HInstruction* inst = i.Current();
37 DCHECK(!inst->IsControlFlow());
Roland Levillaine161a2a2014-10-03 12:45:18 +010038 if (!inst->HasSideEffects()
39 && !inst->CanThrow()
40 && !inst->IsSuspendCheck()
41 && !inst->HasUses()) {
Roland Levillain72bceff2014-09-15 18:29:00 +010042 block->RemoveInstruction(inst);
43 }
44 }
45 }
46}
47
48} // namespace art