ssa_liveness_analysis.cc revision 8a16d97fb8f031822b206e65f9109a071da40563
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_liveness_analysis.h" 18 19#include "base/bit_vector-inl.h" 20#include "code_generator.h" 21#include "nodes.h" 22 23namespace art { 24 25void SsaLivenessAnalysis::Analyze() { 26 LinearizeGraph(); 27 NumberInstructions(); 28 ComputeLiveness(); 29} 30 31static bool IsLoopExit(HLoopInformation* current, HLoopInformation* to) { 32 // `to` is either not part of a loop, or `current` is an inner loop of `to`. 33 return to == nullptr || (current != to && current->IsIn(*to)); 34} 35 36static bool IsLoop(HLoopInformation* info) { 37 return info != nullptr; 38} 39 40static bool InSameLoop(HLoopInformation* first_loop, HLoopInformation* second_loop) { 41 return first_loop == second_loop; 42} 43 44static bool IsInnerLoop(HLoopInformation* outer, HLoopInformation* inner) { 45 return (inner != outer) 46 && (inner != nullptr) 47 && (outer != nullptr) 48 && inner->IsIn(*outer); 49} 50 51static void VisitBlockForLinearization(HBasicBlock* block, 52 GrowableArray<HBasicBlock*>* order, 53 ArenaBitVector* visited) { 54 if (visited->IsBitSet(block->GetBlockId())) { 55 return; 56 } 57 visited->SetBit(block->GetBlockId()); 58 size_t number_of_successors = block->GetSuccessors().Size(); 59 if (number_of_successors == 0) { 60 // Nothing to do. 61 } else if (number_of_successors == 1) { 62 VisitBlockForLinearization(block->GetSuccessors().Get(0), order, visited); 63 } else { 64 DCHECK_EQ(number_of_successors, 2u); 65 HBasicBlock* first_successor = block->GetSuccessors().Get(0); 66 HBasicBlock* second_successor = block->GetSuccessors().Get(1); 67 HLoopInformation* my_loop = block->GetLoopInformation(); 68 HLoopInformation* first_loop = first_successor->GetLoopInformation(); 69 HLoopInformation* second_loop = second_successor->GetLoopInformation(); 70 71 if (!IsLoop(my_loop)) { 72 // Nothing to do. Current order is fine. 73 } else if (IsLoopExit(my_loop, second_loop) && InSameLoop(my_loop, first_loop)) { 74 // Visit the loop exit first in post order. 75 std::swap(first_successor, second_successor); 76 } else if (IsInnerLoop(my_loop, first_loop) && !IsInnerLoop(my_loop, second_loop)) { 77 // Visit the inner loop last in post order. 78 std::swap(first_successor, second_successor); 79 } 80 VisitBlockForLinearization(first_successor, order, visited); 81 VisitBlockForLinearization(second_successor, order, visited); 82 } 83 order->Add(block); 84} 85 86void SsaLivenessAnalysis::LinearizeGraph() { 87 // For simplicity of the implementation, we create post linear order. The order for 88 // computing live ranges is the reverse of that order. 89 ArenaBitVector visited(graph_.GetArena(), graph_.GetBlocks().Size(), false); 90 VisitBlockForLinearization(graph_.GetEntryBlock(), &linear_post_order_, &visited); 91} 92 93void SsaLivenessAnalysis::NumberInstructions() { 94 int ssa_index = 0; 95 size_t lifetime_position = 0; 96 // Each instruction gets a lifetime position, and a block gets a lifetime 97 // start and end position. Non-phi instructions have a distinct lifetime position than 98 // the block they are in. Phi instructions have the lifetime start of their block as 99 // lifetime position. 100 // 101 // Because the register allocator will insert moves in the graph, we need 102 // to differentiate between the start and end of an instruction. Adding 2 to 103 // the lifetime position for each instruction ensures the start of an 104 // instruction is different than the end of the previous instruction. 105 HGraphVisitor* location_builder = codegen_->GetLocationBuilder(); 106 for (HLinearOrderIterator it(*this); !it.Done(); it.Advance()) { 107 HBasicBlock* block = it.Current(); 108 block->SetLifetimeStart(lifetime_position); 109 110 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { 111 HInstruction* current = it.Current(); 112 current->Accept(location_builder); 113 LocationSummary* locations = current->GetLocations(); 114 if (locations != nullptr && locations->Out().IsValid()) { 115 instructions_from_ssa_index_.Add(current); 116 current->SetSsaIndex(ssa_index++); 117 current->SetLiveInterval( 118 new (graph_.GetArena()) LiveInterval(graph_.GetArena(), current->GetType(), current)); 119 } 120 current->SetLifetimePosition(lifetime_position); 121 } 122 lifetime_position += 2; 123 124 // Add a null marker to notify we are starting a block. 125 instructions_from_lifetime_position_.Add(nullptr); 126 127 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { 128 HInstruction* current = it.Current(); 129 current->Accept(codegen_->GetLocationBuilder()); 130 LocationSummary* locations = current->GetLocations(); 131 if (locations != nullptr && locations->Out().IsValid()) { 132 instructions_from_ssa_index_.Add(current); 133 current->SetSsaIndex(ssa_index++); 134 current->SetLiveInterval( 135 new (graph_.GetArena()) LiveInterval(graph_.GetArena(), current->GetType(), current)); 136 } 137 instructions_from_lifetime_position_.Add(current); 138 current->SetLifetimePosition(lifetime_position); 139 lifetime_position += 2; 140 } 141 142 block->SetLifetimeEnd(lifetime_position); 143 } 144 number_of_ssa_values_ = ssa_index; 145} 146 147void SsaLivenessAnalysis::ComputeLiveness() { 148 for (HLinearOrderIterator it(*this); !it.Done(); it.Advance()) { 149 HBasicBlock* block = it.Current(); 150 block_infos_.Put( 151 block->GetBlockId(), 152 new (graph_.GetArena()) BlockInfo(graph_.GetArena(), *block, number_of_ssa_values_)); 153 } 154 155 // Compute the live ranges, as well as the initial live_in, live_out, and kill sets. 156 // This method does not handle backward branches for the sets, therefore live_in 157 // and live_out sets are not yet correct. 158 ComputeLiveRanges(); 159 160 // Do a fixed point calculation to take into account backward branches, 161 // that will update live_in of loop headers, and therefore live_out and live_in 162 // of blocks in the loop. 163 ComputeLiveInAndLiveOutSets(); 164} 165 166void SsaLivenessAnalysis::ComputeLiveRanges() { 167 // Do a post order visit, adding inputs of instructions live in the block where 168 // that instruction is defined, and killing instructions that are being visited. 169 for (HLinearPostOrderIterator it(*this); !it.Done(); it.Advance()) { 170 HBasicBlock* block = it.Current(); 171 172 BitVector* kill = GetKillSet(*block); 173 BitVector* live_in = GetLiveInSet(*block); 174 175 // Set phi inputs of successors of this block corresponding to this block 176 // as live_in. 177 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) { 178 HBasicBlock* successor = block->GetSuccessors().Get(i); 179 live_in->Union(GetLiveInSet(*successor)); 180 size_t phi_input_index = successor->GetPredecessorIndexOf(block); 181 for (HInstructionIterator it(successor->GetPhis()); !it.Done(); it.Advance()) { 182 HInstruction* phi = it.Current(); 183 HInstruction* input = phi->InputAt(phi_input_index); 184 input->GetLiveInterval()->AddPhiUse(phi, phi_input_index, block); 185 // A phi input whose last user is the phi dies at the end of the predecessor block, 186 // and not at the phi's lifetime position. 187 live_in->SetBit(input->GetSsaIndex()); 188 } 189 } 190 191 // Add a range that covers this block to all instructions live_in because of successors. 192 for (uint32_t idx : live_in->Indexes()) { 193 HInstruction* current = instructions_from_ssa_index_.Get(idx); 194 current->GetLiveInterval()->AddRange(block->GetLifetimeStart(), block->GetLifetimeEnd()); 195 } 196 197 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { 198 HInstruction* current = it.Current(); 199 if (current->HasSsaIndex()) { 200 // Kill the instruction and shorten its interval. 201 kill->SetBit(current->GetSsaIndex()); 202 live_in->ClearBit(current->GetSsaIndex()); 203 current->GetLiveInterval()->SetFrom(current->GetLifetimePosition()); 204 } 205 206 // All inputs of an instruction must be live. 207 for (size_t i = 0, e = current->InputCount(); i < e; ++i) { 208 HInstruction* input = current->InputAt(i); 209 // Some instructions 'inline' their inputs, that is they do not need 210 // to be materialized. 211 if (input->HasSsaIndex()) { 212 live_in->SetBit(input->GetSsaIndex()); 213 input->GetLiveInterval()->AddUse(current, i, false); 214 } 215 } 216 217 if (current->HasEnvironment()) { 218 // All instructions in the environment must be live. 219 GrowableArray<HInstruction*>* environment = current->GetEnvironment()->GetVRegs(); 220 for (size_t i = 0, e = environment->Size(); i < e; ++i) { 221 HInstruction* instruction = environment->Get(i); 222 if (instruction != nullptr) { 223 DCHECK(instruction->HasSsaIndex()); 224 live_in->SetBit(instruction->GetSsaIndex()); 225 instruction->GetLiveInterval()->AddUse(current, i, true); 226 } 227 } 228 } 229 } 230 231 // Kill phis defined in this block. 232 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { 233 HInstruction* current = it.Current(); 234 if (current->HasSsaIndex()) { 235 kill->SetBit(current->GetSsaIndex()); 236 live_in->ClearBit(current->GetSsaIndex()); 237 LiveInterval* interval = current->GetLiveInterval(); 238 DCHECK((interval->GetFirstRange() == nullptr) 239 || (interval->GetStart() == current->GetLifetimePosition())); 240 interval->SetFrom(current->GetLifetimePosition()); 241 } 242 } 243 244 if (block->IsLoopHeader()) { 245 HBasicBlock* back_edge = block->GetLoopInformation()->GetBackEdges().Get(0); 246 // For all live_in instructions at the loop header, we need to create a range 247 // that covers the full loop. 248 for (uint32_t idx : live_in->Indexes()) { 249 HInstruction* current = instructions_from_ssa_index_.Get(idx); 250 current->GetLiveInterval()->AddLoopRange(block->GetLifetimeStart(), 251 back_edge->GetLifetimeEnd()); 252 } 253 } 254 } 255} 256 257void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() { 258 bool changed; 259 do { 260 changed = false; 261 262 for (HPostOrderIterator it(graph_); !it.Done(); it.Advance()) { 263 const HBasicBlock& block = *it.Current(); 264 265 // The live_in set depends on the kill set (which does not 266 // change in this loop), and the live_out set. If the live_out 267 // set does not change, there is no need to update the live_in set. 268 if (UpdateLiveOut(block) && UpdateLiveIn(block)) { 269 changed = true; 270 } 271 } 272 } while (changed); 273} 274 275bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) { 276 BitVector* live_out = GetLiveOutSet(block); 277 bool changed = false; 278 // The live_out set of a block is the union of live_in sets of its successors. 279 for (size_t i = 0, e = block.GetSuccessors().Size(); i < e; ++i) { 280 HBasicBlock* successor = block.GetSuccessors().Get(i); 281 if (live_out->Union(GetLiveInSet(*successor))) { 282 changed = true; 283 } 284 } 285 return changed; 286} 287 288 289bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) { 290 BitVector* live_out = GetLiveOutSet(block); 291 BitVector* kill = GetKillSet(block); 292 BitVector* live_in = GetLiveInSet(block); 293 // If live_out is updated (because of backward branches), we need to make 294 // sure instructions in live_out are also in live_in, unless they are killed 295 // by this block. 296 return live_in->UnionIfNotIn(live_out, kill); 297} 298 299} // namespace art 300