Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +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_liveness_analysis.h" |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 18 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 19 | #include "base/bit_vector-inl.h" |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 20 | #include "code_generator.h" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 21 | #include "nodes.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | void SsaLivenessAnalysis::Analyze() { |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 26 | LinearizeGraph(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 27 | NumberInstructions(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 28 | ComputeLiveness(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 29 | } |
| 30 | |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 31 | static bool IsLoop(HLoopInformation* info) { |
| 32 | return info != nullptr; |
| 33 | } |
| 34 | |
| 35 | static bool InSameLoop(HLoopInformation* first_loop, HLoopInformation* second_loop) { |
| 36 | return first_loop == second_loop; |
| 37 | } |
| 38 | |
| 39 | static bool IsInnerLoop(HLoopInformation* outer, HLoopInformation* inner) { |
| 40 | return (inner != outer) |
| 41 | && (inner != nullptr) |
| 42 | && (outer != nullptr) |
| 43 | && inner->IsIn(*outer); |
| 44 | } |
| 45 | |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 46 | static void AddToListForLinearization(GrowableArray<HBasicBlock*>* worklist, HBasicBlock* block) { |
| 47 | size_t insert_at = worklist->Size(); |
| 48 | HLoopInformation* block_loop = block->GetLoopInformation(); |
| 49 | for (; insert_at > 0; --insert_at) { |
| 50 | HBasicBlock* current = worklist->Get(insert_at - 1); |
| 51 | HLoopInformation* current_loop = current->GetLoopInformation(); |
| 52 | if (InSameLoop(block_loop, current_loop) |
| 53 | || !IsLoop(current_loop) |
| 54 | || IsInnerLoop(current_loop, block_loop)) { |
| 55 | // The block can be processed immediately. |
| 56 | break; |
Nicolas Geoffray | e50fa58 | 2014-11-24 17:44:15 +0000 | [diff] [blame] | 57 | } |
Nicolas Geoffray | e50fa58 | 2014-11-24 17:44:15 +0000 | [diff] [blame] | 58 | } |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 59 | worklist->InsertAt(insert_at, block); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 62 | void SsaLivenessAnalysis::LinearizeGraph() { |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 63 | // Create a reverse post ordering with the following properties: |
| 64 | // - Blocks in a loop are consecutive, |
| 65 | // - Back-edge is the last block before loop exits. |
| 66 | |
| 67 | // (1): Record the number of forward predecessors for each block. This is to |
| 68 | // ensure the resulting order is reverse post order. We could use the |
| 69 | // current reverse post order in the graph, but it would require making |
| 70 | // order queries to a GrowableArray, which is not the best data structure |
| 71 | // for it. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 72 | GrowableArray<uint32_t> forward_predecessors(graph_->GetArena(), graph_->GetBlocks().size()); |
| 73 | forward_predecessors.SetSize(graph_->GetBlocks().size()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 74 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 75 | HBasicBlock* block = it.Current(); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 76 | size_t number_of_forward_predecessors = block->GetPredecessors().size(); |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 77 | if (block->IsLoopHeader()) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 78 | number_of_forward_predecessors -= block->GetLoopInformation()->NumberOfBackEdges(); |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 79 | } |
| 80 | forward_predecessors.Put(block->GetBlockId(), number_of_forward_predecessors); |
| 81 | } |
| 82 | |
| 83 | // (2): Following a worklist approach, first start with the entry block, and |
| 84 | // iterate over the successors. When all non-back edge predecessors of a |
| 85 | // successor block are visited, the successor block is added in the worklist |
| 86 | // following an order that satisfies the requirements to build our linear graph. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 87 | graph_->linear_order_.reserve(graph_->GetReversePostOrder().size()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 88 | GrowableArray<HBasicBlock*> worklist(graph_->GetArena(), 1); |
| 89 | worklist.Add(graph_->GetEntryBlock()); |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 90 | do { |
| 91 | HBasicBlock* current = worklist.Pop(); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 92 | graph_->linear_order_.push_back(current); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 93 | for (HBasicBlock* successor : current->GetSuccessors()) { |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 94 | int block_id = successor->GetBlockId(); |
| 95 | size_t number_of_remaining_predecessors = forward_predecessors.Get(block_id); |
| 96 | if (number_of_remaining_predecessors == 1) { |
| 97 | AddToListForLinearization(&worklist, successor); |
| 98 | } |
| 99 | forward_predecessors.Put(block_id, number_of_remaining_predecessors - 1); |
| 100 | } |
| 101 | } while (!worklist.IsEmpty()); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 104 | void SsaLivenessAnalysis::NumberInstructions() { |
| 105 | int ssa_index = 0; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 106 | size_t lifetime_position = 0; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 107 | // Each instruction gets a lifetime position, and a block gets a lifetime |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 108 | // start and end position. Non-phi instructions have a distinct lifetime position than |
| 109 | // the block they are in. Phi instructions have the lifetime start of their block as |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 110 | // lifetime position. |
| 111 | // |
| 112 | // Because the register allocator will insert moves in the graph, we need |
| 113 | // to differentiate between the start and end of an instruction. Adding 2 to |
| 114 | // the lifetime position for each instruction ensures the start of an |
| 115 | // instruction is different than the end of the previous instruction. |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 116 | for (HLinearOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 117 | HBasicBlock* block = it.Current(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 118 | block->SetLifetimeStart(lifetime_position); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 119 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 120 | for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) { |
| 121 | HInstruction* current = inst_it.Current(); |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 122 | codegen_->AllocateLocations(current); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 123 | LocationSummary* locations = current->GetLocations(); |
| 124 | if (locations != nullptr && locations->Out().IsValid()) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 125 | instructions_from_ssa_index_.Add(current); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 126 | current->SetSsaIndex(ssa_index++); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 127 | current->SetLiveInterval( |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 128 | LiveInterval::MakeInterval(graph_->GetArena(), current->GetType(), current)); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 129 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 130 | current->SetLifetimePosition(lifetime_position); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 131 | } |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 132 | lifetime_position += 2; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 133 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 134 | // Add a null marker to notify we are starting a block. |
| 135 | instructions_from_lifetime_position_.Add(nullptr); |
| 136 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 137 | for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done(); |
| 138 | inst_it.Advance()) { |
| 139 | HInstruction* current = inst_it.Current(); |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 140 | codegen_->AllocateLocations(current); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 141 | LocationSummary* locations = current->GetLocations(); |
| 142 | if (locations != nullptr && locations->Out().IsValid()) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 143 | instructions_from_ssa_index_.Add(current); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 144 | current->SetSsaIndex(ssa_index++); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 145 | current->SetLiveInterval( |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 146 | LiveInterval::MakeInterval(graph_->GetArena(), current->GetType(), current)); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 147 | } |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 148 | instructions_from_lifetime_position_.Add(current); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 149 | current->SetLifetimePosition(lifetime_position); |
| 150 | lifetime_position += 2; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 151 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 152 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 153 | block->SetLifetimeEnd(lifetime_position); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 154 | } |
| 155 | number_of_ssa_values_ = ssa_index; |
| 156 | } |
| 157 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 158 | void SsaLivenessAnalysis::ComputeLiveness() { |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 159 | for (HLinearOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 160 | HBasicBlock* block = it.Current(); |
| 161 | block_infos_.Put( |
| 162 | block->GetBlockId(), |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 163 | new (graph_->GetArena()) BlockInfo(graph_->GetArena(), *block, number_of_ssa_values_)); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 166 | // Compute the live ranges, as well as the initial live_in, live_out, and kill sets. |
| 167 | // This method does not handle backward branches for the sets, therefore live_in |
| 168 | // and live_out sets are not yet correct. |
| 169 | ComputeLiveRanges(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 170 | |
| 171 | // Do a fixed point calculation to take into account backward branches, |
| 172 | // that will update live_in of loop headers, and therefore live_out and live_in |
| 173 | // of blocks in the loop. |
| 174 | ComputeLiveInAndLiveOutSets(); |
| 175 | } |
| 176 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 177 | void SsaLivenessAnalysis::ComputeLiveRanges() { |
| 178 | // Do a post order visit, adding inputs of instructions live in the block where |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 179 | // that instruction is defined, and killing instructions that are being visited. |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 180 | for (HLinearPostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 181 | HBasicBlock* block = it.Current(); |
| 182 | |
| 183 | BitVector* kill = GetKillSet(*block); |
| 184 | BitVector* live_in = GetLiveInSet(*block); |
| 185 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 186 | // Set phi inputs of successors of this block corresponding to this block |
| 187 | // as live_in. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 188 | for (HBasicBlock* successor : block->GetSuccessors()) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 189 | live_in->Union(GetLiveInSet(*successor)); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 190 | if (successor->IsCatchBlock()) { |
| 191 | // Inputs of catch phis will be kept alive through their environment |
| 192 | // uses, allowing the runtime to copy their values to the corresponding |
| 193 | // catch phi spill slots when an exception is thrown. |
| 194 | // The only instructions which may not be recorded in the environments |
| 195 | // are constants created by the SSA builder as typed equivalents of |
| 196 | // untyped constants from the bytecode, or phis with only such constants |
| 197 | // as inputs (verified by SSAChecker). Their raw binary value must |
| 198 | // therefore be the same and we only need to keep alive one. |
| 199 | } else { |
| 200 | size_t phi_input_index = successor->GetPredecessorIndexOf(block); |
| 201 | for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 202 | HInstruction* phi = phi_it.Current(); |
| 203 | HInstruction* input = phi->InputAt(phi_input_index); |
| 204 | input->GetLiveInterval()->AddPhiUse(phi, phi_input_index, block); |
| 205 | // A phi input whose last user is the phi dies at the end of the predecessor block, |
| 206 | // and not at the phi's lifetime position. |
| 207 | live_in->SetBit(input->GetSsaIndex()); |
| 208 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | // Add a range that covers this block to all instructions live_in because of successors. |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 213 | // Instructions defined in this block will have their start of the range adjusted. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 214 | for (uint32_t idx : live_in->Indexes()) { |
| 215 | HInstruction* current = instructions_from_ssa_index_.Get(idx); |
| 216 | current->GetLiveInterval()->AddRange(block->GetLifetimeStart(), block->GetLifetimeEnd()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 219 | for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done(); |
| 220 | back_it.Advance()) { |
| 221 | HInstruction* current = back_it.Current(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 222 | if (current->HasSsaIndex()) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 223 | // Kill the instruction and shorten its interval. |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 224 | kill->SetBit(current->GetSsaIndex()); |
| 225 | live_in->ClearBit(current->GetSsaIndex()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 226 | current->GetLiveInterval()->SetFrom(current->GetLifetimePosition()); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 229 | // Process the environment first, because we know their uses come after |
| 230 | // or at the same liveness position of inputs. |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 231 | for (HEnvironment* environment = current->GetEnvironment(); |
| 232 | environment != nullptr; |
| 233 | environment = environment->GetParent()) { |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 234 | // Handle environment uses. See statements (b) and (c) of the |
| 235 | // SsaLivenessAnalysis. |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 236 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
| 237 | HInstruction* instruction = environment->GetInstructionAt(i); |
Mingyao Yang | 718493c | 2015-07-22 15:56:34 -0700 | [diff] [blame] | 238 | bool should_be_live = ShouldBeLiveForEnvironment(current, instruction); |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 239 | if (should_be_live) { |
| 240 | DCHECK(instruction->HasSsaIndex()); |
| 241 | live_in->SetBit(instruction->GetSsaIndex()); |
| 242 | } |
| 243 | if (instruction != nullptr) { |
| 244 | instruction->GetLiveInterval()->AddUse( |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 245 | current, environment, i, should_be_live); |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 250 | // All inputs of an instruction must be live. |
| 251 | for (size_t i = 0, e = current->InputCount(); i < e; ++i) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 252 | HInstruction* input = current->InputAt(i); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 253 | // Some instructions 'inline' their inputs, that is they do not need |
| 254 | // to be materialized. |
Nicolas Geoffray | 94015b9 | 2015-06-04 18:21:04 +0100 | [diff] [blame] | 255 | if (input->HasSsaIndex() && current->GetLocations()->InAt(i).IsValid()) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 256 | live_in->SetBit(input->GetSsaIndex()); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 257 | input->GetLiveInterval()->AddUse(current, /* environment */ nullptr, i); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 262 | // Kill phis defined in this block. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 263 | for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) { |
| 264 | HInstruction* current = inst_it.Current(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 265 | if (current->HasSsaIndex()) { |
| 266 | kill->SetBit(current->GetSsaIndex()); |
| 267 | live_in->ClearBit(current->GetSsaIndex()); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 268 | LiveInterval* interval = current->GetLiveInterval(); |
| 269 | DCHECK((interval->GetFirstRange() == nullptr) |
| 270 | || (interval->GetStart() == current->GetLifetimePosition())); |
| 271 | interval->SetFrom(current->GetLifetimePosition()); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 272 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 273 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 274 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 275 | if (block->IsLoopHeader()) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 276 | size_t last_position = block->GetLoopInformation()->GetLifetimeEnd(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 277 | // For all live_in instructions at the loop header, we need to create a range |
| 278 | // that covers the full loop. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 279 | for (uint32_t idx : live_in->Indexes()) { |
| 280 | HInstruction* current = instructions_from_ssa_index_.Get(idx); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 281 | current->GetLiveInterval()->AddLoopRange(block->GetLifetimeStart(), last_position); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() { |
| 288 | bool changed; |
| 289 | do { |
| 290 | changed = false; |
| 291 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 292 | for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 293 | const HBasicBlock& block = *it.Current(); |
| 294 | |
| 295 | // The live_in set depends on the kill set (which does not |
| 296 | // change in this loop), and the live_out set. If the live_out |
| 297 | // set does not change, there is no need to update the live_in set. |
| 298 | if (UpdateLiveOut(block) && UpdateLiveIn(block)) { |
| 299 | changed = true; |
| 300 | } |
| 301 | } |
| 302 | } while (changed); |
| 303 | } |
| 304 | |
| 305 | bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) { |
| 306 | BitVector* live_out = GetLiveOutSet(block); |
| 307 | bool changed = false; |
| 308 | // The live_out set of a block is the union of live_in sets of its successors. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 309 | for (HBasicBlock* successor : block.GetSuccessors()) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 310 | if (live_out->Union(GetLiveInSet(*successor))) { |
| 311 | changed = true; |
| 312 | } |
| 313 | } |
| 314 | return changed; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) { |
| 319 | BitVector* live_out = GetLiveOutSet(block); |
| 320 | BitVector* kill = GetKillSet(block); |
| 321 | BitVector* live_in = GetLiveInSet(block); |
| 322 | // If live_out is updated (because of backward branches), we need to make |
| 323 | // sure instructions in live_out are also in live_in, unless they are killed |
| 324 | // by this block. |
| 325 | return live_in->UnionIfNotIn(live_out, kill); |
| 326 | } |
| 327 | |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 328 | static int RegisterOrLowRegister(Location location) { |
| 329 | return location.IsPair() ? location.low() : location.reg(); |
| 330 | } |
| 331 | |
Nicolas Geoffray | fbda5f3 | 2015-04-29 14:16:00 +0100 | [diff] [blame] | 332 | int LiveInterval::FindFirstRegisterHint(size_t* free_until, |
| 333 | const SsaLivenessAnalysis& liveness) const { |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 334 | DCHECK(!IsHighInterval()); |
Nicolas Geoffray | f01d344 | 2015-03-27 17:15:49 +0000 | [diff] [blame] | 335 | if (IsTemp()) return kNoRegister; |
| 336 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 337 | if (GetParent() == this && defined_by_ != nullptr) { |
| 338 | // This is the first interval for the instruction. Try to find |
| 339 | // a register based on its definition. |
| 340 | DCHECK_EQ(defined_by_->GetLiveInterval(), this); |
| 341 | int hint = FindHintAtDefinition(); |
| 342 | if (hint != kNoRegister && free_until[hint] > GetStart()) { |
| 343 | return hint; |
| 344 | } |
| 345 | } |
| 346 | |
Nicolas Geoffray | fbda5f3 | 2015-04-29 14:16:00 +0100 | [diff] [blame] | 347 | if (IsSplit() && liveness.IsAtBlockBoundary(GetStart() / 2)) { |
| 348 | // If the start of this interval is at a block boundary, we look at the |
| 349 | // location of the interval in blocks preceding the block this interval |
| 350 | // starts at. If one location is a register we return it as a hint. This |
| 351 | // will avoid a move between the two blocks. |
| 352 | HBasicBlock* block = liveness.GetBlockFromPosition(GetStart() / 2); |
Nicolas Geoffray | 8272688 | 2015-06-01 13:51:57 +0100 | [diff] [blame] | 353 | size_t next_register_use = FirstRegisterUse(); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 354 | for (HBasicBlock* predecessor : block->GetPredecessors()) { |
| 355 | size_t position = predecessor->GetLifetimeEnd() - 1; |
Nicolas Geoffray | fbda5f3 | 2015-04-29 14:16:00 +0100 | [diff] [blame] | 356 | // We know positions above GetStart() do not have a location yet. |
| 357 | if (position < GetStart()) { |
| 358 | LiveInterval* existing = GetParent()->GetSiblingAt(position); |
| 359 | if (existing != nullptr |
| 360 | && existing->HasRegister() |
Nicolas Geoffray | 8272688 | 2015-06-01 13:51:57 +0100 | [diff] [blame] | 361 | // It's worth using that register if it is available until |
| 362 | // the next use. |
| 363 | && (free_until[existing->GetRegister()] >= next_register_use)) { |
Nicolas Geoffray | fbda5f3 | 2015-04-29 14:16:00 +0100 | [diff] [blame] | 364 | return existing->GetRegister(); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 370 | UsePosition* use = first_use_; |
| 371 | size_t start = GetStart(); |
| 372 | size_t end = GetEnd(); |
| 373 | while (use != nullptr && use->GetPosition() <= end) { |
| 374 | size_t use_position = use->GetPosition(); |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 375 | if (use_position >= start && !use->IsSynthesized()) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 376 | HInstruction* user = use->GetUser(); |
| 377 | size_t input_index = use->GetInputIndex(); |
| 378 | if (user->IsPhi()) { |
| 379 | // If the phi has a register, try to use the same. |
| 380 | Location phi_location = user->GetLiveInterval()->ToLocation(); |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 381 | if (phi_location.IsRegisterKind()) { |
| 382 | DCHECK(SameRegisterKind(phi_location)); |
| 383 | int reg = RegisterOrLowRegister(phi_location); |
| 384 | if (free_until[reg] >= use_position) { |
| 385 | return reg; |
| 386 | } |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 387 | } |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 388 | // If the instruction dies at the phi assignment, we can try having the |
| 389 | // same register. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 390 | if (end == user->GetBlock()->GetPredecessor(input_index)->GetLifetimeEnd()) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 391 | for (size_t i = 0, e = user->InputCount(); i < e; ++i) { |
| 392 | if (i == input_index) { |
| 393 | continue; |
| 394 | } |
| 395 | HInstruction* input = user->InputAt(i); |
| 396 | Location location = input->GetLiveInterval()->GetLocationAt( |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 397 | user->GetBlock()->GetPredecessor(i)->GetLifetimeEnd() - 1); |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 398 | if (location.IsRegisterKind()) { |
| 399 | int reg = RegisterOrLowRegister(location); |
| 400 | if (free_until[reg] >= use_position) { |
| 401 | return reg; |
| 402 | } |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | } |
| 406 | } else { |
| 407 | // If the instruction is expected in a register, try to use it. |
| 408 | LocationSummary* locations = user->GetLocations(); |
| 409 | Location expected = locations->InAt(use->GetInputIndex()); |
| 410 | // We use the user's lifetime position - 1 (and not `use_position`) because the |
| 411 | // register is blocked at the beginning of the user. |
| 412 | size_t position = user->GetLifetimePosition() - 1; |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 413 | if (expected.IsRegisterKind()) { |
| 414 | DCHECK(SameRegisterKind(expected)); |
| 415 | int reg = RegisterOrLowRegister(expected); |
| 416 | if (free_until[reg] >= position) { |
| 417 | return reg; |
| 418 | } |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | } |
| 422 | use = use->GetNext(); |
| 423 | } |
| 424 | |
| 425 | return kNoRegister; |
| 426 | } |
| 427 | |
| 428 | int LiveInterval::FindHintAtDefinition() const { |
| 429 | if (defined_by_->IsPhi()) { |
| 430 | // Try to use the same register as one of the inputs. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 431 | const ArenaVector<HBasicBlock*>& predecessors = defined_by_->GetBlock()->GetPredecessors(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 432 | for (size_t i = 0, e = defined_by_->InputCount(); i < e; ++i) { |
| 433 | HInstruction* input = defined_by_->InputAt(i); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 434 | DCHECK_LT(i, predecessors.size()); |
| 435 | size_t end = predecessors[i]->GetLifetimeEnd(); |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 436 | LiveInterval* input_interval = input->GetLiveInterval()->GetSiblingAt(end - 1); |
| 437 | if (input_interval->GetEnd() == end) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 438 | // If the input dies at the end of the predecessor, we know its register can |
| 439 | // be reused. |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 440 | Location input_location = input_interval->ToLocation(); |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 441 | if (input_location.IsRegisterKind()) { |
| 442 | DCHECK(SameRegisterKind(input_location)); |
| 443 | return RegisterOrLowRegister(input_location); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | } |
| 447 | } else { |
| 448 | LocationSummary* locations = GetDefinedBy()->GetLocations(); |
| 449 | Location out = locations->Out(); |
| 450 | if (out.IsUnallocated() && out.GetPolicy() == Location::kSameAsFirstInput) { |
| 451 | // Try to use the same register as the first input. |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 452 | LiveInterval* input_interval = |
| 453 | GetDefinedBy()->InputAt(0)->GetLiveInterval()->GetSiblingAt(GetStart() - 1); |
| 454 | if (input_interval->GetEnd() == GetStart()) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 455 | // If the input dies at the start of this instruction, we know its register can |
| 456 | // be reused. |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 457 | Location location = input_interval->ToLocation(); |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 458 | if (location.IsRegisterKind()) { |
| 459 | DCHECK(SameRegisterKind(location)); |
| 460 | return RegisterOrLowRegister(location); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | return kNoRegister; |
| 466 | } |
| 467 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 468 | bool LiveInterval::SameRegisterKind(Location other) const { |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 469 | if (IsFloatingPoint()) { |
| 470 | if (IsLowInterval() || IsHighInterval()) { |
| 471 | return other.IsFpuRegisterPair(); |
| 472 | } else { |
| 473 | return other.IsFpuRegister(); |
| 474 | } |
| 475 | } else { |
| 476 | if (IsLowInterval() || IsHighInterval()) { |
| 477 | return other.IsRegisterPair(); |
| 478 | } else { |
| 479 | return other.IsRegister(); |
| 480 | } |
| 481 | } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 482 | } |
| 483 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 484 | bool LiveInterval::NeedsTwoSpillSlots() const { |
| 485 | return type_ == Primitive::kPrimLong || type_ == Primitive::kPrimDouble; |
| 486 | } |
| 487 | |
| 488 | Location LiveInterval::ToLocation() const { |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 489 | DCHECK(!IsHighInterval()); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 490 | if (HasRegister()) { |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 491 | if (IsFloatingPoint()) { |
| 492 | if (HasHighInterval()) { |
| 493 | return Location::FpuRegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister()); |
| 494 | } else { |
| 495 | return Location::FpuRegisterLocation(GetRegister()); |
| 496 | } |
| 497 | } else { |
| 498 | if (HasHighInterval()) { |
| 499 | return Location::RegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister()); |
| 500 | } else { |
| 501 | return Location::RegisterLocation(GetRegister()); |
| 502 | } |
| 503 | } |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 504 | } else { |
| 505 | HInstruction* defined_by = GetParent()->GetDefinedBy(); |
| 506 | if (defined_by->IsConstant()) { |
| 507 | return defined_by->GetLocations()->Out(); |
| 508 | } else if (GetParent()->HasSpillSlot()) { |
| 509 | if (NeedsTwoSpillSlots()) { |
| 510 | return Location::DoubleStackSlot(GetParent()->GetSpillSlot()); |
| 511 | } else { |
| 512 | return Location::StackSlot(GetParent()->GetSpillSlot()); |
| 513 | } |
| 514 | } else { |
| 515 | return Location(); |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 520 | Location LiveInterval::GetLocationAt(size_t position) { |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 521 | LiveInterval* sibling = GetSiblingAt(position); |
| 522 | DCHECK(sibling != nullptr); |
| 523 | return sibling->ToLocation(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 524 | } |
| 525 | |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 526 | LiveInterval* LiveInterval::GetSiblingAt(size_t position) { |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 527 | LiveInterval* current = this; |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 528 | while (current != nullptr && !current->IsDefinedAt(position)) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 529 | current = current->GetNextSibling(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 530 | } |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 531 | return current; |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 534 | } // namespace art |