Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1 | /* |
| 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 "gvn.h" |
| 18 | |
| 19 | namespace art { |
| 20 | |
| 21 | void GlobalValueNumberer::Run() { |
| 22 | ComputeSideEffects(); |
| 23 | |
| 24 | sets_.Put(graph_->GetEntryBlock()->GetBlockId(), new (allocator_) ValueSet(allocator_)); |
| 25 | |
| 26 | // Do reverse post order to ensure the non back-edge predecessors of a block are |
| 27 | // visited before the block itself. |
| 28 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 29 | VisitBasicBlock(it.Current()); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | void GlobalValueNumberer::UpdateLoopEffects(HLoopInformation* info, SideEffects effects) { |
| 34 | int id = info->GetHeader()->GetBlockId(); |
| 35 | loop_effects_.Put(id, loop_effects_.Get(id).Union(effects)); |
| 36 | } |
| 37 | |
| 38 | void GlobalValueNumberer::ComputeSideEffects() { |
| 39 | if (kIsDebugBuild) { |
| 40 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 41 | HBasicBlock* block = it.Current(); |
| 42 | SideEffects effects = GetBlockEffects(block); |
| 43 | DCHECK(!effects.HasSideEffects() && !effects.HasDependencies()); |
| 44 | if (block->IsLoopHeader()) { |
| 45 | effects = GetLoopEffects(block); |
| 46 | DCHECK(!effects.HasSideEffects() && !effects.HasDependencies()); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Do a post order visit to ensure we visit a loop header after its loop body. |
| 52 | for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 53 | HBasicBlock* block = it.Current(); |
| 54 | |
| 55 | SideEffects effects = SideEffects::None(); |
| 56 | // Update `effects` with the side effects of all instructions in this block. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 57 | for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done(); |
| 58 | inst_it.Advance()) { |
| 59 | HInstruction* instruction = inst_it.Current(); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 60 | effects = effects.Union(instruction->GetSideEffects()); |
| 61 | if (effects.HasAllSideEffects()) { |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | block_effects_.Put(block->GetBlockId(), effects); |
| 67 | |
| 68 | if (block->IsLoopHeader()) { |
| 69 | // The side effects of the loop header are part of the loop. |
| 70 | UpdateLoopEffects(block->GetLoopInformation(), effects); |
| 71 | HBasicBlock* pre_header = block->GetLoopInformation()->GetPreHeader(); |
| 72 | if (pre_header->IsInLoop()) { |
| 73 | // Update the side effects of the outer loop with the side effects of the inner loop. |
| 74 | // Note that this works because we know all the blocks of the inner loop are visited |
| 75 | // before the loop header of the outer loop. |
| 76 | UpdateLoopEffects(pre_header->GetLoopInformation(), GetLoopEffects(block)); |
| 77 | } |
| 78 | } else if (block->IsInLoop()) { |
| 79 | // Update the side effects of the loop with the side effects of this block. |
| 80 | UpdateLoopEffects(block->GetLoopInformation(), effects); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | SideEffects GlobalValueNumberer::GetLoopEffects(HBasicBlock* block) const { |
| 86 | DCHECK(block->IsLoopHeader()); |
| 87 | return loop_effects_.Get(block->GetBlockId()); |
| 88 | } |
| 89 | |
| 90 | SideEffects GlobalValueNumberer::GetBlockEffects(HBasicBlock* block) const { |
| 91 | return block_effects_.Get(block->GetBlockId()); |
| 92 | } |
| 93 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 94 | void GlobalValueNumberer::VisitBasicBlock(HBasicBlock* block) { |
Nicolas Geoffray | dbca6fa | 2014-11-27 12:01:59 +0000 | [diff] [blame^] | 95 | ValueSet* set = nullptr; |
| 96 | const GrowableArray<HBasicBlock*>& predecessors = block->GetPredecessors(); |
| 97 | if (predecessors.Size() == 0 || predecessors.Get(0)->IsEntryBlock()) { |
| 98 | // The entry block should only accumulate constant instructions, and |
| 99 | // the builder puts constants only in the entry block. |
| 100 | // Therefore, there is no need to propagate the value set to the next block. |
| 101 | set = new (allocator_) ValueSet(allocator_); |
| 102 | } else { |
| 103 | HBasicBlock* dominator = block->GetDominator(); |
| 104 | set = sets_.Get(dominator->GetBlockId())->Copy(); |
| 105 | if (dominator->GetSuccessors().Size() != 1 || dominator->GetSuccessors().Get(0) != block) { |
| 106 | // We have to copy if the dominator has other successors, or `block` is not a successor |
| 107 | // of the dominator. |
| 108 | set = set->Copy(); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 109 | } |
Nicolas Geoffray | dbca6fa | 2014-11-27 12:01:59 +0000 | [diff] [blame^] | 110 | if (!set->IsEmpty()) { |
| 111 | if (block->IsLoopHeader()) { |
| 112 | DCHECK_EQ(block->GetDominator(), block->GetLoopInformation()->GetPreHeader()); |
| 113 | set->Kill(GetLoopEffects(block)); |
| 114 | } else if (predecessors.Size() > 1) { |
| 115 | for (size_t i = 0, e = predecessors.Size(); i < e; ++i) { |
| 116 | set->IntersectionWith(sets_.Get(predecessors.Get(i)->GetBlockId())); |
| 117 | if (set->IsEmpty()) { |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Nicolas Geoffray | dbca6fa | 2014-11-27 12:01:59 +0000 | [diff] [blame^] | 125 | sets_.Put(block->GetBlockId(), set); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 126 | |
| 127 | HInstruction* current = block->GetFirstInstruction(); |
| 128 | while (current != nullptr) { |
| 129 | set->Kill(current->GetSideEffects()); |
| 130 | // Save the next instruction in case `current` is removed from the graph. |
| 131 | HInstruction* next = current->GetNext(); |
| 132 | if (current->CanBeMoved()) { |
| 133 | HInstruction* existing = set->Lookup(current); |
| 134 | if (existing != nullptr) { |
| 135 | current->ReplaceWith(existing); |
| 136 | current->GetBlock()->RemoveInstruction(current); |
| 137 | } else { |
| 138 | set->Add(current); |
| 139 | } |
| 140 | } |
| 141 | current = next; |
| 142 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } // namespace art |