blob: 838597d4ac6afe52e580fb18b5a6fc42327d58d2 [file] [log] [blame]
Nicolas Geoffray804d0932014-05-02 08:46: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 "ssa_liveness_analysis.h"
18#include "nodes.h"
19
20namespace art {
21
22void SsaLivenessAnalysis::Analyze() {
23 NumberInstructions();
24 ComputeSets();
25}
26
27void SsaLivenessAnalysis::NumberInstructions() {
28 int ssa_index = 0;
29 for (HReversePostOrderIterator it(graph_); !it.Done(); it.Advance()) {
30 HBasicBlock* block = it.Current();
31
32 for (HInstructionIterator it(*block->GetPhis()); !it.Done(); it.Advance()) {
33 HInstruction* current = it.Current();
34 if (current->HasUses()) {
35 current->SetSsaIndex(ssa_index++);
36 }
37 }
38
39 for (HInstructionIterator it(*block->GetInstructions()); !it.Done(); it.Advance()) {
40 HInstruction* current = it.Current();
41 if (current->HasUses()) {
42 current->SetSsaIndex(ssa_index++);
43 }
44 }
45 }
46 number_of_ssa_values_ = ssa_index;
47}
48
49void SsaLivenessAnalysis::ComputeSets() {
50 for (HReversePostOrderIterator it(graph_); !it.Done(); it.Advance()) {
51 HBasicBlock* block = it.Current();
52 block_infos_.Put(
53 block->GetBlockId(),
54 new (graph_.GetArena()) BlockInfo(graph_.GetArena(), *block, number_of_ssa_values_));
55 }
56
57 // Compute the initial live_in, live_out, and kill sets. This method does not handle
58 // backward branches, therefore live_in and live_out sets are not yet correct.
59 ComputeInitialSets();
60
61 // Do a fixed point calculation to take into account backward branches,
62 // that will update live_in of loop headers, and therefore live_out and live_in
63 // of blocks in the loop.
64 ComputeLiveInAndLiveOutSets();
65}
66
67void SsaLivenessAnalysis::ComputeInitialSets() {
68 // Do a post orderr visit, adding inputs of instructions live in the block where
69 // that instruction is defined, and killing instructions that are being visited.
70 for (HPostOrderIterator it(graph_); !it.Done(); it.Advance()) {
71 HBasicBlock* block = it.Current();
72
73 BitVector* kill = GetKillSet(*block);
74 BitVector* live_in = GetLiveInSet(*block);
75
76 for (HBackwardInstructionIterator it(*block->GetInstructions()); !it.Done(); it.Advance()) {
77 HInstruction* current = it.Current();
78 if (current->HasSsaIndex()) {
79 kill->SetBit(current->GetSsaIndex());
80 live_in->ClearBit(current->GetSsaIndex());
81 }
82
83 // All inputs of an instruction must be live.
84 for (size_t i = 0, e = current->InputCount(); i < e; ++i) {
85 DCHECK(current->InputAt(i)->HasSsaIndex());
86 live_in->SetBit(current->InputAt(i)->GetSsaIndex());
87 }
88
89 if (current->HasEnvironment()) {
90 // All instructions in the environment must be live.
91 GrowableArray<HInstruction*>* environment = current->GetEnvironment()->GetVRegs();
92 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
93 HInstruction* instruction = environment->Get(i);
94 if (instruction != nullptr) {
95 DCHECK(instruction->HasSsaIndex());
96 live_in->SetBit(instruction->GetSsaIndex());
97 }
98 }
99 }
100 }
101
102 for (HInstructionIterator it(*block->GetPhis()); !it.Done(); it.Advance()) {
103 HInstruction* current = it.Current();
104 if (current->HasSsaIndex()) {
105 kill->SetBit(current->GetSsaIndex());
106 live_in->ClearBit(current->GetSsaIndex());
107 }
108
109 // Mark a phi input live_in for its corresponding predecessor.
110 for (size_t i = 0, e = current->InputCount(); i < e; ++i) {
111 HInstruction* input = current->InputAt(i);
112
113 HBasicBlock* predecessor = block->GetPredecessors()->Get(i);
114 size_t ssa_index = input->GetSsaIndex();
115 BitVector* predecessor_kill = GetKillSet(*predecessor);
116 BitVector* predecessor_live_in = GetLiveInSet(*predecessor);
117
118 // Phi inputs from a back edge have already been visited. If the back edge
119 // block defines that input, we should not add it to its live_in.
120 if (!predecessor_kill->IsBitSet(ssa_index)) {
121 predecessor_live_in->SetBit(ssa_index);
122 }
123 }
124 }
125 }
126}
127
128void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() {
129 bool changed;
130 do {
131 changed = false;
132
133 for (HPostOrderIterator it(graph_); !it.Done(); it.Advance()) {
134 const HBasicBlock& block = *it.Current();
135
136 // The live_in set depends on the kill set (which does not
137 // change in this loop), and the live_out set. If the live_out
138 // set does not change, there is no need to update the live_in set.
139 if (UpdateLiveOut(block) && UpdateLiveIn(block)) {
140 changed = true;
141 }
142 }
143 } while (changed);
144}
145
146bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) {
147 BitVector* live_out = GetLiveOutSet(block);
148 bool changed = false;
149 // The live_out set of a block is the union of live_in sets of its successors.
150 for (size_t i = 0, e = block.GetSuccessors()->Size(); i < e; ++i) {
151 HBasicBlock* successor = block.GetSuccessors()->Get(i);
152 if (live_out->Union(GetLiveInSet(*successor))) {
153 changed = true;
154 }
155 }
156 return changed;
157}
158
159
160bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) {
161 BitVector* live_out = GetLiveOutSet(block);
162 BitVector* kill = GetKillSet(block);
163 BitVector* live_in = GetLiveInSet(block);
164 // If live_out is updated (because of backward branches), we need to make
165 // sure instructions in live_out are also in live_in, unless they are killed
166 // by this block.
167 return live_in->UnionIfNotIn(live_out, kill);
168}
169
170} // namespace art