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. |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 72 | GrowableArray<uint32_t> forward_predecessors(graph_->GetArena(), graph_->GetBlocks().Size()); |
| 73 | forward_predecessors.SetSize(graph_->GetBlocks().Size()); |
| 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(); |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 76 | size_t number_of_forward_predecessors = block->GetPredecessors().Size(); |
| 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. |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 87 | GrowableArray<HBasicBlock*> worklist(graph_->GetArena(), 1); |
| 88 | worklist.Add(graph_->GetEntryBlock()); |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 89 | do { |
| 90 | HBasicBlock* current = worklist.Pop(); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 91 | graph_->linear_order_.Add(current); |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 92 | for (size_t i = 0, e = current->GetSuccessors().Size(); i < e; ++i) { |
| 93 | HBasicBlock* successor = current->GetSuccessors().Get(i); |
| 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. |
| 188 | for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) { |
| 189 | HBasicBlock* successor = block->GetSuccessors().Get(i); |
| 190 | live_in->Union(GetLiveInSet(*successor)); |
| 191 | size_t phi_input_index = successor->GetPredecessorIndexOf(block); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 192 | for (HInstructionIterator inst_it(successor->GetPhis()); !inst_it.Done(); inst_it.Advance()) { |
| 193 | HInstruction* phi = inst_it.Current(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 194 | HInstruction* input = phi->InputAt(phi_input_index); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 195 | input->GetLiveInterval()->AddPhiUse(phi, phi_input_index, block); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 196 | // A phi input whose last user is the phi dies at the end of the predecessor block, |
| 197 | // and not at the phi's lifetime position. |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 198 | live_in->SetBit(input->GetSsaIndex()); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // 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] | 203 | // 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] | 204 | for (uint32_t idx : live_in->Indexes()) { |
| 205 | HInstruction* current = instructions_from_ssa_index_.Get(idx); |
| 206 | current->GetLiveInterval()->AddRange(block->GetLifetimeStart(), block->GetLifetimeEnd()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 209 | for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done(); |
| 210 | back_it.Advance()) { |
| 211 | HInstruction* current = back_it.Current(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 212 | if (current->HasSsaIndex()) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 213 | // Kill the instruction and shorten its interval. |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 214 | kill->SetBit(current->GetSsaIndex()); |
| 215 | live_in->ClearBit(current->GetSsaIndex()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 216 | current->GetLiveInterval()->SetFrom(current->GetLifetimePosition()); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 219 | // Process the environment first, because we know their uses come after |
| 220 | // or at the same liveness position of inputs. |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 221 | for (HEnvironment* environment = current->GetEnvironment(); |
| 222 | environment != nullptr; |
| 223 | environment = environment->GetParent()) { |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 224 | // Handle environment uses. See statements (b) and (c) of the |
| 225 | // SsaLivenessAnalysis. |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 226 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
| 227 | HInstruction* instruction = environment->GetInstructionAt(i); |
| 228 | bool should_be_live = ShouldBeLiveForEnvironment(instruction); |
| 229 | if (should_be_live) { |
| 230 | DCHECK(instruction->HasSsaIndex()); |
| 231 | live_in->SetBit(instruction->GetSsaIndex()); |
| 232 | } |
| 233 | if (instruction != nullptr) { |
| 234 | instruction->GetLiveInterval()->AddUse( |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 235 | current, environment, i, should_be_live); |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 240 | // All inputs of an instruction must be live. |
| 241 | for (size_t i = 0, e = current->InputCount(); i < e; ++i) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 242 | HInstruction* input = current->InputAt(i); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 243 | // Some instructions 'inline' their inputs, that is they do not need |
| 244 | // to be materialized. |
Nicolas Geoffray | 94015b9 | 2015-06-04 18:21:04 +0100 | [diff] [blame] | 245 | if (input->HasSsaIndex() && current->GetLocations()->InAt(i).IsValid()) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 246 | live_in->SetBit(input->GetSsaIndex()); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 247 | input->GetLiveInterval()->AddUse(current, /* environment */ nullptr, i); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 252 | // Kill phis defined in this block. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 253 | for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) { |
| 254 | HInstruction* current = inst_it.Current(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 255 | if (current->HasSsaIndex()) { |
| 256 | kill->SetBit(current->GetSsaIndex()); |
| 257 | live_in->ClearBit(current->GetSsaIndex()); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 258 | LiveInterval* interval = current->GetLiveInterval(); |
| 259 | DCHECK((interval->GetFirstRange() == nullptr) |
| 260 | || (interval->GetStart() == current->GetLifetimePosition())); |
| 261 | interval->SetFrom(current->GetLifetimePosition()); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 262 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 263 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 264 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 265 | if (block->IsLoopHeader()) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 266 | size_t last_position = block->GetLoopInformation()->GetLifetimeEnd(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 267 | // For all live_in instructions at the loop header, we need to create a range |
| 268 | // that covers the full loop. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 269 | for (uint32_t idx : live_in->Indexes()) { |
| 270 | HInstruction* current = instructions_from_ssa_index_.Get(idx); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 271 | current->GetLiveInterval()->AddLoopRange(block->GetLifetimeStart(), last_position); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() { |
| 278 | bool changed; |
| 279 | do { |
| 280 | changed = false; |
| 281 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 282 | for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 283 | const HBasicBlock& block = *it.Current(); |
| 284 | |
| 285 | // The live_in set depends on the kill set (which does not |
| 286 | // change in this loop), and the live_out set. If the live_out |
| 287 | // set does not change, there is no need to update the live_in set. |
| 288 | if (UpdateLiveOut(block) && UpdateLiveIn(block)) { |
| 289 | changed = true; |
| 290 | } |
| 291 | } |
| 292 | } while (changed); |
| 293 | } |
| 294 | |
| 295 | bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) { |
| 296 | BitVector* live_out = GetLiveOutSet(block); |
| 297 | bool changed = false; |
| 298 | // The live_out set of a block is the union of live_in sets of its successors. |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 299 | for (size_t i = 0, e = block.GetSuccessors().Size(); i < e; ++i) { |
| 300 | HBasicBlock* successor = block.GetSuccessors().Get(i); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 301 | if (live_out->Union(GetLiveInSet(*successor))) { |
| 302 | changed = true; |
| 303 | } |
| 304 | } |
| 305 | return changed; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) { |
| 310 | BitVector* live_out = GetLiveOutSet(block); |
| 311 | BitVector* kill = GetKillSet(block); |
| 312 | BitVector* live_in = GetLiveInSet(block); |
| 313 | // If live_out is updated (because of backward branches), we need to make |
| 314 | // sure instructions in live_out are also in live_in, unless they are killed |
| 315 | // by this block. |
| 316 | return live_in->UnionIfNotIn(live_out, kill); |
| 317 | } |
| 318 | |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 319 | static int RegisterOrLowRegister(Location location) { |
| 320 | return location.IsPair() ? location.low() : location.reg(); |
| 321 | } |
| 322 | |
Nicolas Geoffray | fbda5f3 | 2015-04-29 14:16:00 +0100 | [diff] [blame] | 323 | int LiveInterval::FindFirstRegisterHint(size_t* free_until, |
| 324 | const SsaLivenessAnalysis& liveness) const { |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 325 | DCHECK(!IsHighInterval()); |
Nicolas Geoffray | f01d344 | 2015-03-27 17:15:49 +0000 | [diff] [blame] | 326 | if (IsTemp()) return kNoRegister; |
| 327 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 328 | if (GetParent() == this && defined_by_ != nullptr) { |
| 329 | // This is the first interval for the instruction. Try to find |
| 330 | // a register based on its definition. |
| 331 | DCHECK_EQ(defined_by_->GetLiveInterval(), this); |
| 332 | int hint = FindHintAtDefinition(); |
| 333 | if (hint != kNoRegister && free_until[hint] > GetStart()) { |
| 334 | return hint; |
| 335 | } |
| 336 | } |
| 337 | |
Nicolas Geoffray | fbda5f3 | 2015-04-29 14:16:00 +0100 | [diff] [blame] | 338 | if (IsSplit() && liveness.IsAtBlockBoundary(GetStart() / 2)) { |
| 339 | // If the start of this interval is at a block boundary, we look at the |
| 340 | // location of the interval in blocks preceding the block this interval |
| 341 | // starts at. If one location is a register we return it as a hint. This |
| 342 | // will avoid a move between the two blocks. |
| 343 | HBasicBlock* block = liveness.GetBlockFromPosition(GetStart() / 2); |
Nicolas Geoffray | 8272688 | 2015-06-01 13:51:57 +0100 | [diff] [blame] | 344 | size_t next_register_use = FirstRegisterUse(); |
Nicolas Geoffray | fbda5f3 | 2015-04-29 14:16:00 +0100 | [diff] [blame] | 345 | for (size_t i = 0; i < block->GetPredecessors().Size(); ++i) { |
| 346 | size_t position = block->GetPredecessors().Get(i)->GetLifetimeEnd() - 1; |
| 347 | // We know positions above GetStart() do not have a location yet. |
| 348 | if (position < GetStart()) { |
| 349 | LiveInterval* existing = GetParent()->GetSiblingAt(position); |
| 350 | if (existing != nullptr |
| 351 | && existing->HasRegister() |
Nicolas Geoffray | 8272688 | 2015-06-01 13:51:57 +0100 | [diff] [blame] | 352 | // It's worth using that register if it is available until |
| 353 | // the next use. |
| 354 | && (free_until[existing->GetRegister()] >= next_register_use)) { |
Nicolas Geoffray | fbda5f3 | 2015-04-29 14:16:00 +0100 | [diff] [blame] | 355 | return existing->GetRegister(); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 361 | UsePosition* use = first_use_; |
| 362 | size_t start = GetStart(); |
| 363 | size_t end = GetEnd(); |
| 364 | while (use != nullptr && use->GetPosition() <= end) { |
| 365 | size_t use_position = use->GetPosition(); |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 366 | if (use_position >= start && !use->IsSynthesized()) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 367 | HInstruction* user = use->GetUser(); |
| 368 | size_t input_index = use->GetInputIndex(); |
| 369 | if (user->IsPhi()) { |
| 370 | // If the phi has a register, try to use the same. |
| 371 | Location phi_location = user->GetLiveInterval()->ToLocation(); |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 372 | if (phi_location.IsRegisterKind()) { |
| 373 | DCHECK(SameRegisterKind(phi_location)); |
| 374 | int reg = RegisterOrLowRegister(phi_location); |
| 375 | if (free_until[reg] >= use_position) { |
| 376 | return reg; |
| 377 | } |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 378 | } |
| 379 | const GrowableArray<HBasicBlock*>& predecessors = user->GetBlock()->GetPredecessors(); |
| 380 | // If the instruction dies at the phi assignment, we can try having the |
| 381 | // same register. |
| 382 | if (end == predecessors.Get(input_index)->GetLifetimeEnd()) { |
| 383 | for (size_t i = 0, e = user->InputCount(); i < e; ++i) { |
| 384 | if (i == input_index) { |
| 385 | continue; |
| 386 | } |
| 387 | HInstruction* input = user->InputAt(i); |
| 388 | Location location = input->GetLiveInterval()->GetLocationAt( |
| 389 | predecessors.Get(i)->GetLifetimeEnd() - 1); |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 390 | if (location.IsRegisterKind()) { |
| 391 | int reg = RegisterOrLowRegister(location); |
| 392 | if (free_until[reg] >= use_position) { |
| 393 | return reg; |
| 394 | } |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | } |
| 398 | } else { |
| 399 | // If the instruction is expected in a register, try to use it. |
| 400 | LocationSummary* locations = user->GetLocations(); |
| 401 | Location expected = locations->InAt(use->GetInputIndex()); |
| 402 | // We use the user's lifetime position - 1 (and not `use_position`) because the |
| 403 | // register is blocked at the beginning of the user. |
| 404 | size_t position = user->GetLifetimePosition() - 1; |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 405 | if (expected.IsRegisterKind()) { |
| 406 | DCHECK(SameRegisterKind(expected)); |
| 407 | int reg = RegisterOrLowRegister(expected); |
| 408 | if (free_until[reg] >= position) { |
| 409 | return reg; |
| 410 | } |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | } |
| 414 | use = use->GetNext(); |
| 415 | } |
| 416 | |
| 417 | return kNoRegister; |
| 418 | } |
| 419 | |
| 420 | int LiveInterval::FindHintAtDefinition() const { |
| 421 | if (defined_by_->IsPhi()) { |
| 422 | // Try to use the same register as one of the inputs. |
| 423 | const GrowableArray<HBasicBlock*>& predecessors = defined_by_->GetBlock()->GetPredecessors(); |
| 424 | for (size_t i = 0, e = defined_by_->InputCount(); i < e; ++i) { |
| 425 | HInstruction* input = defined_by_->InputAt(i); |
| 426 | size_t end = predecessors.Get(i)->GetLifetimeEnd(); |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 427 | LiveInterval* input_interval = input->GetLiveInterval()->GetSiblingAt(end - 1); |
| 428 | if (input_interval->GetEnd() == end) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 429 | // If the input dies at the end of the predecessor, we know its register can |
| 430 | // be reused. |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 431 | Location input_location = input_interval->ToLocation(); |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 432 | if (input_location.IsRegisterKind()) { |
| 433 | DCHECK(SameRegisterKind(input_location)); |
| 434 | return RegisterOrLowRegister(input_location); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | } |
| 438 | } else { |
| 439 | LocationSummary* locations = GetDefinedBy()->GetLocations(); |
| 440 | Location out = locations->Out(); |
| 441 | if (out.IsUnallocated() && out.GetPolicy() == Location::kSameAsFirstInput) { |
| 442 | // Try to use the same register as the first input. |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 443 | LiveInterval* input_interval = |
| 444 | GetDefinedBy()->InputAt(0)->GetLiveInterval()->GetSiblingAt(GetStart() - 1); |
| 445 | if (input_interval->GetEnd() == GetStart()) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 446 | // If the input dies at the start of this instruction, we know its register can |
| 447 | // be reused. |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 448 | Location location = input_interval->ToLocation(); |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 449 | if (location.IsRegisterKind()) { |
| 450 | DCHECK(SameRegisterKind(location)); |
| 451 | return RegisterOrLowRegister(location); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | return kNoRegister; |
| 457 | } |
| 458 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 459 | bool LiveInterval::SameRegisterKind(Location other) const { |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 460 | if (IsFloatingPoint()) { |
| 461 | if (IsLowInterval() || IsHighInterval()) { |
| 462 | return other.IsFpuRegisterPair(); |
| 463 | } else { |
| 464 | return other.IsFpuRegister(); |
| 465 | } |
| 466 | } else { |
| 467 | if (IsLowInterval() || IsHighInterval()) { |
| 468 | return other.IsRegisterPair(); |
| 469 | } else { |
| 470 | return other.IsRegister(); |
| 471 | } |
| 472 | } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 473 | } |
| 474 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 475 | bool LiveInterval::NeedsTwoSpillSlots() const { |
| 476 | return type_ == Primitive::kPrimLong || type_ == Primitive::kPrimDouble; |
| 477 | } |
| 478 | |
| 479 | Location LiveInterval::ToLocation() const { |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 480 | DCHECK(!IsHighInterval()); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 481 | if (HasRegister()) { |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 482 | if (IsFloatingPoint()) { |
| 483 | if (HasHighInterval()) { |
| 484 | return Location::FpuRegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister()); |
| 485 | } else { |
| 486 | return Location::FpuRegisterLocation(GetRegister()); |
| 487 | } |
| 488 | } else { |
| 489 | if (HasHighInterval()) { |
| 490 | return Location::RegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister()); |
| 491 | } else { |
| 492 | return Location::RegisterLocation(GetRegister()); |
| 493 | } |
| 494 | } |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 495 | } else { |
| 496 | HInstruction* defined_by = GetParent()->GetDefinedBy(); |
| 497 | if (defined_by->IsConstant()) { |
| 498 | return defined_by->GetLocations()->Out(); |
| 499 | } else if (GetParent()->HasSpillSlot()) { |
| 500 | if (NeedsTwoSpillSlots()) { |
| 501 | return Location::DoubleStackSlot(GetParent()->GetSpillSlot()); |
| 502 | } else { |
| 503 | return Location::StackSlot(GetParent()->GetSpillSlot()); |
| 504 | } |
| 505 | } else { |
| 506 | return Location(); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 511 | Location LiveInterval::GetLocationAt(size_t position) { |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 512 | LiveInterval* sibling = GetSiblingAt(position); |
| 513 | DCHECK(sibling != nullptr); |
| 514 | return sibling->ToLocation(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 515 | } |
| 516 | |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 517 | LiveInterval* LiveInterval::GetSiblingAt(size_t position) { |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 518 | LiveInterval* current = this; |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 519 | while (current != nullptr && !current->IsDefinedAt(position)) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 520 | current = current->GetNextSibling(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 521 | } |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 522 | return current; |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 523 | } |
| 524 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 525 | } // namespace art |