blob: 471307ec31c566d3b786769276f67866bcbb14ef [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"
Nicolas Geoffray184d6402014-06-09 14:06:02 +010018
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010019#include "nodes.h"
Nicolas Geoffray184d6402014-06-09 14:06:02 +010020#include "ssa_type_propagation.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010021
22namespace art {
23
24void SsaBuilder::BuildSsa() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010025 // 1) Visit in reverse post order. We need to have all predecessors of a block visited
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010026 // (with the exception of loops) in order to create the right environment for that
27 // block. For loops, we create phis whose inputs will be set in 2).
Nicolas Geoffray804d0932014-05-02 08:46:00 +010028 for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) {
29 VisitBasicBlock(it.Current());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010030 }
31
32 // 2) Set inputs of loop phis.
33 for (size_t i = 0; i < loop_headers_.Size(); i++) {
34 HBasicBlock* block = loop_headers_.Get(i);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010035 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010036 HPhi* phi = it.Current()->AsPhi();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010037 for (size_t pred = 0; pred < block->GetPredecessors().Size(); pred++) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010038 HInstruction* input = ValueOfLocal(block->GetPredecessors().Get(pred), phi->GetRegNumber());
39 phi->AddInput(input);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010040 }
41 }
42 }
43
Nicolas Geoffray184d6402014-06-09 14:06:02 +010044 // 3) Propagate types of phis.
45 SsaTypePropagation type_propagation(GetGraph());
46 type_propagation.Run();
47
48 // 4) Clear locals.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010049 // TODO: Move this to a dead code eliminator phase.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010050 for (HInstructionIterator it(GetGraph()->GetEntryBlock()->GetInstructions());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010051 !it.Done();
52 it.Advance()) {
53 HInstruction* current = it.Current();
54 if (current->AsLocal() != nullptr) {
55 current->GetBlock()->RemoveInstruction(current);
56 }
57 }
58}
59
60HInstruction* SsaBuilder::ValueOfLocal(HBasicBlock* block, size_t local) {
61 return GetLocalsFor(block)->Get(local);
62}
63
64void SsaBuilder::VisitBasicBlock(HBasicBlock* block) {
65 current_locals_ = GetLocalsFor(block);
66
67 if (block->IsLoopHeader()) {
68 // If the block is a loop header, we know we only have visited the pre header
Nicolas Geoffray804d0932014-05-02 08:46:00 +010069 // because we are visiting in reverse post order. We create phis for all initialized
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010070 // locals from the pre header. Their inputs will be populated at the end of
71 // the analysis.
72 for (size_t local = 0; local < current_locals_->Size(); local++) {
73 HInstruction* incoming = ValueOfLocal(block->GetLoopInformation()->GetPreHeader(), local);
74 if (incoming != nullptr) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010075 HPhi* phi = new (GetGraph()->GetArena()) HPhi(
76 GetGraph()->GetArena(), local, 0, Primitive::kPrimVoid);
77 block->AddPhi(phi);
78 current_locals_->Put(local, phi);
79 }
80 }
81 // Save the loop header so that the last phase of the analysis knows which
82 // blocks need to be updated.
83 loop_headers_.Add(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010084 } else if (block->GetPredecessors().Size() > 0) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010085 // All predecessors have already been visited because we are visiting in reverse post order.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010086 // We merge the values of all locals, creating phis if those values differ.
87 for (size_t local = 0; local < current_locals_->Size(); local++) {
Nicolas Geoffray7c3560f2014-06-04 12:12:08 +010088 bool one_predecessor_has_no_value = false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010089 bool is_different = false;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010090 HInstruction* value = ValueOfLocal(block->GetPredecessors().Get(0), local);
Nicolas Geoffray7c3560f2014-06-04 12:12:08 +010091
92 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
93 HInstruction* current = ValueOfLocal(block->GetPredecessors().Get(i), local);
94 if (current == nullptr) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +010095 one_predecessor_has_no_value = true;
96 break;
Nicolas Geoffray7c3560f2014-06-04 12:12:08 +010097 } else if (current != value) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010098 is_different = true;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010099 }
100 }
Nicolas Geoffray7c3560f2014-06-04 12:12:08 +0100101
102 if (one_predecessor_has_no_value) {
103 // If one predecessor has no value for this local, we trust the verifier has
104 // successfully checked that there is a store dominating any read after this block.
105 continue;
106 }
107
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100108 if (is_different) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109 HPhi* phi = new (GetGraph()->GetArena()) HPhi(
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100110 GetGraph()->GetArena(), local, block->GetPredecessors().Size(), Primitive::kPrimVoid);
111 for (size_t i = 0; i < block->GetPredecessors().Size(); i++) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100112 HInstruction* value = ValueOfLocal(block->GetPredecessors().Get(i), local);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100113 phi->SetRawInputAt(i, value);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100114 }
115 block->AddPhi(phi);
116 value = phi;
117 }
118 current_locals_->Put(local, value);
119 }
120 }
121
122 // Visit all instructions. The instructions of interest are:
123 // - HLoadLocal: replace them with the current value of the local.
124 // - HStoreLocal: update current value of the local and remove the instruction.
125 // - Instructions that require an environment: populate their environment
126 // with the current values of the locals.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100127 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100128 it.Current()->Accept(this);
129 }
130}
131
132void SsaBuilder::VisitLoadLocal(HLoadLocal* load) {
133 load->ReplaceWith(current_locals_->Get(load->GetLocal()->GetRegNumber()));
134 load->GetBlock()->RemoveInstruction(load);
135}
136
137void SsaBuilder::VisitStoreLocal(HStoreLocal* store) {
138 current_locals_->Put(store->GetLocal()->GetRegNumber(), store->InputAt(1));
139 store->GetBlock()->RemoveInstruction(store);
140}
141
142void SsaBuilder::VisitInstruction(HInstruction* instruction) {
143 if (!instruction->NeedsEnvironment()) {
144 return;
145 }
146 HEnvironment* environment = new (GetGraph()->GetArena()) HEnvironment(
147 GetGraph()->GetArena(), current_locals_->Size());
148 environment->Populate(*current_locals_);
149 instruction->SetEnvironment(environment);
150}
151
152} // namespace art