blob: 50e3254d7c107c48189cf814d5f3573786f17217 [file] [log] [blame]
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +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 "ssa_builder.h"
18#include "nodes.h"
19
20namespace art {
21
22void SsaBuilder::BuildSsa() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010023 // 1) Visit in reverse post order. We need to have all predecessors of a block visited
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010024 // (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 Geoffray804d0932014-05-02 08:46:00 +010026 for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) {
27 VisitBasicBlock(it.Current());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010028 }
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 Geoffrayf635e632014-05-14 09:43:38 +010033 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010034 HPhi* phi = it.Current()->AsPhi();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010035 for (size_t pred = 0; pred < block->GetPredecessors().Size(); pred++) {
36 phi->AddInput(ValueOfLocal(block->GetPredecessors().Get(pred), phi->GetRegNumber()));
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010037 }
38 }
39 }
40
41 // 3) Clear locals.
42 // TODO: Move this to a dead code eliminator phase.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010043 for (HInstructionIterator it(GetGraph()->GetEntryBlock()->GetInstructions());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010044 !it.Done();
45 it.Advance()) {
46 HInstruction* current = it.Current();
47 if (current->AsLocal() != nullptr) {
48 current->GetBlock()->RemoveInstruction(current);
49 }
50 }
51}
52
53HInstruction* SsaBuilder::ValueOfLocal(HBasicBlock* block, size_t local) {
54 return GetLocalsFor(block)->Get(local);
55}
56
57void 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 Geoffray804d0932014-05-02 08:46:00 +010062 // because we are visiting in reverse post order. We create phis for all initialized
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010063 // 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 Geoffray622d9c32014-05-12 16:11:02 +010078 } else if (block->GetPredecessors().Size() > 0) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010079 // All predecessors have already been visited because we are visiting in reverse post order.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010080 // 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 Geoffray622d9c32014-05-12 16:11:02 +010083 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 Geoffrayc32e7702014-04-24 12:43:16 +010086 is_different = true;
87 break;
88 }
89 }
90 if (is_different) {
91 // TODO: Compute union type.
92 HPhi* phi = new (GetGraph()->GetArena()) HPhi(
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010093 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 Geoffrayc32e7702014-04-24 12:43:16 +010096 }
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 Geoffrayf635e632014-05-14 09:43:38 +0100109 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100110 it.Current()->Accept(this);
111 }
112}
113
114void SsaBuilder::VisitLoadLocal(HLoadLocal* load) {
115 load->ReplaceWith(current_locals_->Get(load->GetLocal()->GetRegNumber()));
116 load->GetBlock()->RemoveInstruction(load);
117}
118
119void SsaBuilder::VisitStoreLocal(HStoreLocal* store) {
120 current_locals_->Put(store->GetLocal()->GetRegNumber(), store->InputAt(1));
121 store->GetBlock()->RemoveInstruction(store);
122}
123
124void 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