Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +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 "ssa_builder.h" |
| 18 | #include "nodes.h" |
| 19 | |
| 20 | namespace art { |
| 21 | |
| 22 | void SsaBuilder::BuildSsa() { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 23 | // 1) Visit in reverse post order. We need to have all predecessors of a block visited |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 24 | // (with the exception of loops) in order to create the right environment for that |
| 25 | // block. For loops, we create phis whose inputs will be set in 2). |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 26 | for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) { |
| 27 | VisitBasicBlock(it.Current()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | // 2) Set inputs of loop phis. |
| 31 | for (size_t i = 0; i < loop_headers_.Size(); i++) { |
| 32 | HBasicBlock* block = loop_headers_.Get(i); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame^] | 33 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 34 | HPhi* phi = it.Current()->AsPhi(); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 35 | for (size_t pred = 0; pred < block->GetPredecessors().Size(); pred++) { |
| 36 | phi->AddInput(ValueOfLocal(block->GetPredecessors().Get(pred), phi->GetRegNumber())); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // 3) Clear locals. |
| 42 | // TODO: Move this to a dead code eliminator phase. |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame^] | 43 | for (HInstructionIterator it(GetGraph()->GetEntryBlock()->GetInstructions()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 44 | !it.Done(); |
| 45 | it.Advance()) { |
| 46 | HInstruction* current = it.Current(); |
| 47 | if (current->AsLocal() != nullptr) { |
| 48 | current->GetBlock()->RemoveInstruction(current); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | HInstruction* SsaBuilder::ValueOfLocal(HBasicBlock* block, size_t local) { |
| 54 | return GetLocalsFor(block)->Get(local); |
| 55 | } |
| 56 | |
| 57 | void SsaBuilder::VisitBasicBlock(HBasicBlock* block) { |
| 58 | current_locals_ = GetLocalsFor(block); |
| 59 | |
| 60 | if (block->IsLoopHeader()) { |
| 61 | // If the block is a loop header, we know we only have visited the pre header |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 62 | // because we are visiting in reverse post order. We create phis for all initialized |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 63 | // locals from the pre header. Their inputs will be populated at the end of |
| 64 | // the analysis. |
| 65 | for (size_t local = 0; local < current_locals_->Size(); local++) { |
| 66 | HInstruction* incoming = ValueOfLocal(block->GetLoopInformation()->GetPreHeader(), local); |
| 67 | if (incoming != nullptr) { |
| 68 | // TODO: Compute union type. |
| 69 | HPhi* phi = new (GetGraph()->GetArena()) HPhi( |
| 70 | GetGraph()->GetArena(), local, 0, Primitive::kPrimVoid); |
| 71 | block->AddPhi(phi); |
| 72 | current_locals_->Put(local, phi); |
| 73 | } |
| 74 | } |
| 75 | // Save the loop header so that the last phase of the analysis knows which |
| 76 | // blocks need to be updated. |
| 77 | loop_headers_.Add(block); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 78 | } else if (block->GetPredecessors().Size() > 0) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 79 | // All predecessors have already been visited because we are visiting in reverse post order. |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 80 | // We merge the values of all locals, creating phis if those values differ. |
| 81 | for (size_t local = 0; local < current_locals_->Size(); local++) { |
| 82 | bool is_different = false; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 83 | HInstruction* value = ValueOfLocal(block->GetPredecessors().Get(0), local); |
| 84 | for (size_t i = 1; i < block->GetPredecessors().Size(); i++) { |
| 85 | if (ValueOfLocal(block->GetPredecessors().Get(i), local) != value) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 86 | is_different = true; |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | if (is_different) { |
| 91 | // TODO: Compute union type. |
| 92 | HPhi* phi = new (GetGraph()->GetArena()) HPhi( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 93 | GetGraph()->GetArena(), local, block->GetPredecessors().Size(), Primitive::kPrimVoid); |
| 94 | for (size_t i = 0; i < block->GetPredecessors().Size(); i++) { |
| 95 | phi->SetRawInputAt(i, ValueOfLocal(block->GetPredecessors().Get(i), local)); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 96 | } |
| 97 | block->AddPhi(phi); |
| 98 | value = phi; |
| 99 | } |
| 100 | current_locals_->Put(local, value); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Visit all instructions. The instructions of interest are: |
| 105 | // - HLoadLocal: replace them with the current value of the local. |
| 106 | // - HStoreLocal: update current value of the local and remove the instruction. |
| 107 | // - Instructions that require an environment: populate their environment |
| 108 | // with the current values of the locals. |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame^] | 109 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 110 | it.Current()->Accept(this); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void SsaBuilder::VisitLoadLocal(HLoadLocal* load) { |
| 115 | load->ReplaceWith(current_locals_->Get(load->GetLocal()->GetRegNumber())); |
| 116 | load->GetBlock()->RemoveInstruction(load); |
| 117 | } |
| 118 | |
| 119 | void SsaBuilder::VisitStoreLocal(HStoreLocal* store) { |
| 120 | current_locals_->Put(store->GetLocal()->GetRegNumber(), store->InputAt(1)); |
| 121 | store->GetBlock()->RemoveInstruction(store); |
| 122 | } |
| 123 | |
| 124 | void SsaBuilder::VisitInstruction(HInstruction* instruction) { |
| 125 | if (!instruction->NeedsEnvironment()) { |
| 126 | return; |
| 127 | } |
| 128 | HEnvironment* environment = new (GetGraph()->GetArena()) HEnvironment( |
| 129 | GetGraph()->GetArena(), current_locals_->Size()); |
| 130 | environment->Populate(*current_locals_); |
| 131 | instruction->SetEnvironment(environment); |
| 132 | } |
| 133 | |
| 134 | } // namespace art |