Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [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 "nodes.h" |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 18 | |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 19 | #include "code_generator.h" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 20 | #include "ssa_builder.h" |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 21 | #include "base/bit_vector-inl.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 22 | #include "base/bit_utils.h" |
David Brazdil | baf89b8 | 2015-09-15 11:36:54 +0100 | [diff] [blame] | 23 | #include "mirror/class-inl.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 24 | #include "utils/growable_array.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 25 | #include "scoped_thread_state_change.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | void HGraph::AddBlock(HBasicBlock* block) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 30 | block->SetBlockId(blocks_.size()); |
| 31 | blocks_.push_back(block); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 34 | void HGraph::FindBackEdges(ArenaBitVector* visited) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 35 | ArenaBitVector visiting(arena_, blocks_.size(), false); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 36 | VisitBlockForBackEdges(entry_block_, visited, &visiting); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 39 | static void RemoveAsUser(HInstruction* instruction) { |
| 40 | for (size_t i = 0; i < instruction->InputCount(); i++) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 41 | instruction->RemoveAsUserOfInput(i); |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 44 | for (HEnvironment* environment = instruction->GetEnvironment(); |
| 45 | environment != nullptr; |
| 46 | environment = environment->GetParent()) { |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 47 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 48 | if (environment->GetInstructionAt(i) != nullptr) { |
| 49 | environment->RemoveAsUserOfInput(i); |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void HGraph::RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 56 | for (size_t i = 0; i < blocks_.size(); ++i) { |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 57 | if (!visited.IsBitSet(i)) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 58 | HBasicBlock* block = GetBlock(i); |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 59 | DCHECK(block->GetPhis().IsEmpty()) << "Phis are not inserted at this stage"; |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 60 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 61 | RemoveAsUser(it.Current()); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 67 | void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 68 | for (size_t i = 0; i < blocks_.size(); ++i) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 69 | if (!visited.IsBitSet(i)) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 70 | HBasicBlock* block = GetBlock(i); |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 71 | // We only need to update the successor, which might be live. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 72 | for (HBasicBlock* successor : block->GetSuccessors()) { |
| 73 | successor->RemovePredecessor(block); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 74 | } |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 75 | // Remove the block from the list of blocks, so that further analyses |
| 76 | // never see it. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 77 | blocks_[i] = nullptr; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void HGraph::VisitBlockForBackEdges(HBasicBlock* block, |
| 83 | ArenaBitVector* visited, |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 84 | ArenaBitVector* visiting) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 85 | int id = block->GetBlockId(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 86 | if (visited->IsBitSet(id)) return; |
| 87 | |
| 88 | visited->SetBit(id); |
| 89 | visiting->SetBit(id); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 90 | for (HBasicBlock* successor : block->GetSuccessors()) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 91 | if (visiting->IsBitSet(successor->GetBlockId())) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 92 | successor->AddBackEdge(block); |
| 93 | } else { |
| 94 | VisitBlockForBackEdges(successor, visited, visiting); |
| 95 | } |
| 96 | } |
| 97 | visiting->ClearBit(id); |
| 98 | } |
| 99 | |
| 100 | void HGraph::BuildDominatorTree() { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 101 | // (1) Simplify the CFG so that catch blocks have only exceptional incoming |
| 102 | // edges. This invariant simplifies building SSA form because Phis cannot |
| 103 | // collect both normal- and exceptional-flow values at the same time. |
| 104 | SimplifyCatchBlocks(); |
| 105 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 106 | ArenaBitVector visited(arena_, blocks_.size(), false); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 107 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 108 | // (2) Find the back edges in the graph doing a DFS traversal. |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 109 | FindBackEdges(&visited); |
| 110 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 111 | // (3) Remove instructions and phis from blocks not visited during |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 112 | // the initial DFS as users from other instructions, so that |
| 113 | // users can be safely removed before uses later. |
| 114 | RemoveInstructionsAsUsersFromDeadBlocks(visited); |
| 115 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 116 | // (4) Remove blocks not visited during the initial DFS. |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 117 | // Step (4) requires dead blocks to be removed from the |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 118 | // predecessors list of live blocks. |
| 119 | RemoveDeadBlocks(visited); |
| 120 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 121 | // (5) Simplify the CFG now, so that we don't need to recompute |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 122 | // dominators and the reverse post order. |
| 123 | SimplifyCFG(); |
| 124 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 125 | // (6) Compute the dominance information and the reverse post order. |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 126 | ComputeDominanceInformation(); |
| 127 | } |
| 128 | |
| 129 | void HGraph::ClearDominanceInformation() { |
| 130 | for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) { |
| 131 | it.Current()->ClearDominanceInformation(); |
| 132 | } |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 133 | reverse_post_order_.clear(); |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void HBasicBlock::ClearDominanceInformation() { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 137 | dominated_blocks_.clear(); |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 138 | dominator_ = nullptr; |
| 139 | } |
| 140 | |
| 141 | void HGraph::ComputeDominanceInformation() { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 142 | DCHECK(reverse_post_order_.empty()); |
| 143 | reverse_post_order_.reserve(blocks_.size()); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 144 | reverse_post_order_.push_back(entry_block_); |
Vladimir Marko | d76d139 | 2015-09-23 16:07:14 +0100 | [diff] [blame^] | 145 | |
| 146 | // Number of visits of a given node, indexed by block id. |
| 147 | ArenaVector<size_t> visits(blocks_.size(), 0u, arena_->Adapter()); |
| 148 | // Number of successors visited from a given node, indexed by block id. |
| 149 | ArenaVector<size_t> successors_visited(blocks_.size(), 0u, arena_->Adapter()); |
| 150 | // Nodes for which we need to visit successors. |
| 151 | ArenaVector<HBasicBlock*> worklist(arena_->Adapter()); |
| 152 | constexpr size_t kDefaultWorklistSize = 8; |
| 153 | worklist.reserve(kDefaultWorklistSize); |
| 154 | worklist.push_back(entry_block_); |
| 155 | |
| 156 | while (!worklist.empty()) { |
| 157 | HBasicBlock* current = worklist.back(); |
| 158 | uint32_t current_id = current->GetBlockId(); |
| 159 | if (successors_visited[current_id] == current->GetSuccessors().size()) { |
| 160 | worklist.pop_back(); |
| 161 | } else { |
| 162 | DCHECK_LT(successors_visited[current_id], current->GetSuccessors().size()); |
| 163 | HBasicBlock* successor = current->GetSuccessors()[successors_visited[current_id]++]; |
| 164 | |
| 165 | if (successor->GetDominator() == nullptr) { |
| 166 | successor->SetDominator(current); |
| 167 | } else { |
| 168 | successor->SetDominator(FindCommonDominator(successor->GetDominator(), current)); |
| 169 | } |
| 170 | |
| 171 | // Once all the forward edges have been visited, we know the immediate |
| 172 | // dominator of the block. We can then start visiting its successors. |
| 173 | DCHECK_LT(successor->GetBlockId(), visits.size()); |
| 174 | if (++visits[successor->GetBlockId()] == |
| 175 | successor->GetPredecessors().size() - successor->NumberOfBackEdges()) { |
| 176 | successor->GetDominator()->AddDominatedBlock(successor); |
| 177 | reverse_post_order_.push_back(successor); |
| 178 | worklist.push_back(successor); |
| 179 | } |
| 180 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 185 | ArenaBitVector visited(arena_, blocks_.size(), false); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 186 | // Walk the dominator tree of the first block and mark the visited blocks. |
| 187 | while (first != nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 188 | visited.SetBit(first->GetBlockId()); |
| 189 | first = first->GetDominator(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 190 | } |
| 191 | // Walk the dominator tree of the second block until a marked block is found. |
| 192 | while (second != nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 193 | if (visited.IsBitSet(second->GetBlockId())) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 194 | return second; |
| 195 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 196 | second = second->GetDominator(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 197 | } |
| 198 | LOG(ERROR) << "Could not find common dominator"; |
| 199 | return nullptr; |
| 200 | } |
| 201 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 202 | void HGraph::TransformToSsa() { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 203 | DCHECK(!reverse_post_order_.empty()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 204 | SsaBuilder ssa_builder(this); |
| 205 | ssa_builder.BuildSsa(); |
| 206 | } |
| 207 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 208 | HBasicBlock* HGraph::SplitEdge(HBasicBlock* block, HBasicBlock* successor) { |
David Brazdil | 3e18738 | 2015-06-26 09:59:52 +0000 | [diff] [blame] | 209 | HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc()); |
| 210 | AddBlock(new_block); |
David Brazdil | 3e18738 | 2015-06-26 09:59:52 +0000 | [diff] [blame] | 211 | // Use `InsertBetween` to ensure the predecessor index and successor index of |
| 212 | // `block` and `successor` are preserved. |
| 213 | new_block->InsertBetween(block, successor); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 214 | return new_block; |
| 215 | } |
| 216 | |
| 217 | void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) { |
| 218 | // Insert a new node between `block` and `successor` to split the |
| 219 | // critical edge. |
| 220 | HBasicBlock* new_block = SplitEdge(block, successor); |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 221 | new_block->AddInstruction(new (arena_) HGoto(successor->GetDexPc())); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 222 | if (successor->IsLoopHeader()) { |
| 223 | // If we split at a back edge boundary, make the new block the back edge. |
| 224 | HLoopInformation* info = successor->GetLoopInformation(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 225 | if (info->IsBackEdge(*block)) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 226 | info->RemoveBackEdge(block); |
| 227 | info->AddBackEdge(new_block); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 232 | void HGraph::SimplifyLoop(HBasicBlock* header) { |
| 233 | HLoopInformation* info = header->GetLoopInformation(); |
| 234 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 235 | // Make sure the loop has only one pre header. This simplifies SSA building by having |
| 236 | // to just look at the pre header to know which locals are initialized at entry of the |
| 237 | // loop. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 238 | size_t number_of_incomings = header->GetPredecessors().size() - info->NumberOfBackEdges(); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 239 | if (number_of_incomings != 1) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 240 | HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc()); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 241 | AddBlock(pre_header); |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 242 | pre_header->AddInstruction(new (arena_) HGoto(header->GetDexPc())); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 243 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 244 | for (size_t pred = 0; pred < header->GetPredecessors().size(); ++pred) { |
| 245 | HBasicBlock* predecessor = header->GetPredecessor(pred); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 246 | if (!info->IsBackEdge(*predecessor)) { |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 247 | predecessor->ReplaceSuccessor(header, pre_header); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 248 | pred--; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | pre_header->AddSuccessor(header); |
| 252 | } |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 253 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 254 | // Make sure the first predecessor of a loop header is the incoming block. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 255 | if (info->IsBackEdge(*header->GetPredecessor(0))) { |
| 256 | HBasicBlock* to_swap = header->GetPredecessor(0); |
| 257 | for (size_t pred = 1, e = header->GetPredecessors().size(); pred < e; ++pred) { |
| 258 | HBasicBlock* predecessor = header->GetPredecessor(pred); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 259 | if (!info->IsBackEdge(*predecessor)) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 260 | header->predecessors_[pred] = to_swap; |
| 261 | header->predecessors_[0] = predecessor; |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 262 | break; |
| 263 | } |
| 264 | } |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 265 | } |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 266 | |
| 267 | // Place the suspend check at the beginning of the header, so that live registers |
| 268 | // will be known when allocating registers. Note that code generation can still |
| 269 | // generate the suspend check at the back edge, but needs to be careful with |
| 270 | // loop phi spill slots (which are not written to at back edge). |
| 271 | HInstruction* first_instruction = header->GetFirstInstruction(); |
| 272 | if (!first_instruction->IsSuspendCheck()) { |
| 273 | HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc()); |
| 274 | header->InsertInstructionBefore(check, first_instruction); |
| 275 | first_instruction = check; |
| 276 | } |
| 277 | info->SetSuspendCheck(first_instruction->AsSuspendCheck()); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 278 | } |
| 279 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 280 | static bool CheckIfPredecessorAtIsExceptional(const HBasicBlock& block, size_t pred_idx) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 281 | HBasicBlock* predecessor = block.GetPredecessor(pred_idx); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 282 | if (!predecessor->EndsWithTryBoundary()) { |
| 283 | // Only edges from HTryBoundary can be exceptional. |
| 284 | return false; |
| 285 | } |
| 286 | HTryBoundary* try_boundary = predecessor->GetLastInstruction()->AsTryBoundary(); |
| 287 | if (try_boundary->GetNormalFlowSuccessor() == &block) { |
| 288 | // This block is the normal-flow successor of `try_boundary`, but it could |
| 289 | // also be one of its exception handlers if catch blocks have not been |
| 290 | // simplified yet. Predecessors are unordered, so we will consider the first |
| 291 | // occurrence to be the normal edge and a possible second occurrence to be |
| 292 | // the exceptional edge. |
| 293 | return !block.IsFirstIndexOfPredecessor(predecessor, pred_idx); |
| 294 | } else { |
| 295 | // This is not the normal-flow successor of `try_boundary`, hence it must be |
| 296 | // one of its exception handlers. |
| 297 | DCHECK(try_boundary->HasExceptionHandler(block)); |
| 298 | return true; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | void HGraph::SimplifyCatchBlocks() { |
Vladimir Marko | b7d8e8c | 2015-09-17 15:47:05 +0100 | [diff] [blame] | 303 | // NOTE: We're appending new blocks inside the loop, so we need to use index because iterators |
| 304 | // can be invalidated. We remember the initial size to avoid iterating over the new blocks. |
| 305 | for (size_t block_id = 0u, end = blocks_.size(); block_id != end; ++block_id) { |
| 306 | HBasicBlock* catch_block = blocks_[block_id]; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 307 | if (!catch_block->IsCatchBlock()) { |
| 308 | continue; |
| 309 | } |
| 310 | |
| 311 | bool exceptional_predecessors_only = true; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 312 | for (size_t j = 0; j < catch_block->GetPredecessors().size(); ++j) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 313 | if (!CheckIfPredecessorAtIsExceptional(*catch_block, j)) { |
| 314 | exceptional_predecessors_only = false; |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | if (!exceptional_predecessors_only) { |
| 320 | // Catch block has normal-flow predecessors and needs to be simplified. |
| 321 | // Splitting the block before its first instruction moves all its |
| 322 | // instructions into `normal_block` and links the two blocks with a Goto. |
| 323 | // Afterwards, incoming normal-flow edges are re-linked to `normal_block`, |
| 324 | // leaving `catch_block` with the exceptional edges only. |
| 325 | // Note that catch blocks with normal-flow predecessors cannot begin with |
| 326 | // a MOVE_EXCEPTION instruction, as guaranteed by the verifier. |
| 327 | DCHECK(!catch_block->GetFirstInstruction()->IsLoadException()); |
| 328 | HBasicBlock* normal_block = catch_block->SplitBefore(catch_block->GetFirstInstruction()); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 329 | for (size_t j = 0; j < catch_block->GetPredecessors().size(); ++j) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 330 | if (!CheckIfPredecessorAtIsExceptional(*catch_block, j)) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 331 | catch_block->GetPredecessor(j)->ReplaceSuccessor(catch_block, normal_block); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 332 | --j; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | void HGraph::ComputeTryBlockInformation() { |
| 340 | // Iterate in reverse post order to propagate try membership information from |
| 341 | // predecessors to their successors. |
| 342 | for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) { |
| 343 | HBasicBlock* block = it.Current(); |
| 344 | if (block->IsEntryBlock() || block->IsCatchBlock()) { |
| 345 | // Catch blocks after simplification have only exceptional predecessors |
| 346 | // and hence are never in tries. |
| 347 | continue; |
| 348 | } |
| 349 | |
| 350 | // Infer try membership from the first predecessor. Having simplified loops, |
| 351 | // the first predecessor can never be a back edge and therefore it must have |
| 352 | // been visited already and had its try membership set. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 353 | HBasicBlock* first_predecessor = block->GetPredecessor(0); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 354 | DCHECK(!block->IsLoopHeader() || !block->GetLoopInformation()->IsBackEdge(*first_predecessor)); |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 355 | const HTryBoundary* try_entry = first_predecessor->ComputeTryEntryOfSuccessors(); |
| 356 | if (try_entry != nullptr) { |
| 357 | block->SetTryCatchInformation(new (arena_) TryCatchInformation(*try_entry)); |
| 358 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 362 | void HGraph::SimplifyCFG() { |
| 363 | // Simplify the CFG for future analysis, and code generation: |
| 364 | // (1): Split critical edges. |
| 365 | // (2): Simplify loops by having only one back edge, and one preheader. |
Vladimir Marko | b7d8e8c | 2015-09-17 15:47:05 +0100 | [diff] [blame] | 366 | // NOTE: We're appending new blocks inside the loop, so we need to use index because iterators |
| 367 | // can be invalidated. We remember the initial size to avoid iterating over the new blocks. |
| 368 | for (size_t block_id = 0u, end = blocks_.size(); block_id != end; ++block_id) { |
| 369 | HBasicBlock* block = blocks_[block_id]; |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 370 | if (block == nullptr) continue; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 371 | if (block->NumberOfNormalSuccessors() > 1) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 372 | for (size_t j = 0; j < block->GetSuccessors().size(); ++j) { |
| 373 | HBasicBlock* successor = block->GetSuccessor(j); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 374 | DCHECK(!successor->IsCatchBlock()); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 375 | if (successor->GetPredecessors().size() > 1) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 376 | SplitCriticalEdge(block, successor); |
| 377 | --j; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | if (block->IsLoopHeader()) { |
| 382 | SimplifyLoop(block); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 387 | bool HGraph::AnalyzeNaturalLoops() const { |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 388 | // Order does not matter. |
| 389 | for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) { |
| 390 | HBasicBlock* block = it.Current(); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 391 | if (block->IsLoopHeader()) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 392 | if (block->IsCatchBlock()) { |
| 393 | // TODO: Dealing with exceptional back edges could be tricky because |
| 394 | // they only approximate the real control flow. Bail out for now. |
| 395 | return false; |
| 396 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 397 | HLoopInformation* info = block->GetLoopInformation(); |
| 398 | if (!info->Populate()) { |
| 399 | // Abort if the loop is non natural. We currently bailout in such cases. |
| 400 | return false; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | return true; |
| 405 | } |
| 406 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 407 | void HGraph::InsertConstant(HConstant* constant) { |
| 408 | // New constants are inserted before the final control-flow instruction |
| 409 | // of the graph, or at its end if called from the graph builder. |
| 410 | if (entry_block_->EndsWithControlFlowInstruction()) { |
| 411 | entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction()); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 412 | } else { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 413 | entry_block_->AddInstruction(constant); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 417 | HNullConstant* HGraph::GetNullConstant(uint32_t dex_pc) { |
Nicolas Geoffray | 18e6873 | 2015-06-17 23:09:05 +0100 | [diff] [blame] | 418 | // For simplicity, don't bother reviving the cached null constant if it is |
| 419 | // not null and not in a block. Otherwise, we need to clear the instruction |
| 420 | // id and/or any invariants the graph is assuming when adding new instructions. |
| 421 | if ((cached_null_constant_ == nullptr) || (cached_null_constant_->GetBlock() == nullptr)) { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 422 | cached_null_constant_ = new (arena_) HNullConstant(dex_pc); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 423 | InsertConstant(cached_null_constant_); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 424 | } |
| 425 | return cached_null_constant_; |
| 426 | } |
| 427 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 428 | HCurrentMethod* HGraph::GetCurrentMethod() { |
Nicolas Geoffray | f78848f | 2015-06-17 11:57:56 +0100 | [diff] [blame] | 429 | // For simplicity, don't bother reviving the cached current method if it is |
| 430 | // not null and not in a block. Otherwise, we need to clear the instruction |
| 431 | // id and/or any invariants the graph is assuming when adding new instructions. |
| 432 | if ((cached_current_method_ == nullptr) || (cached_current_method_->GetBlock() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 433 | cached_current_method_ = new (arena_) HCurrentMethod( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 434 | Is64BitInstructionSet(instruction_set_) ? Primitive::kPrimLong : Primitive::kPrimInt, |
| 435 | entry_block_->GetDexPc()); |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 436 | if (entry_block_->GetFirstInstruction() == nullptr) { |
| 437 | entry_block_->AddInstruction(cached_current_method_); |
| 438 | } else { |
| 439 | entry_block_->InsertInstructionBefore( |
| 440 | cached_current_method_, entry_block_->GetFirstInstruction()); |
| 441 | } |
| 442 | } |
| 443 | return cached_current_method_; |
| 444 | } |
| 445 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 446 | HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 447 | switch (type) { |
| 448 | case Primitive::Type::kPrimBoolean: |
| 449 | DCHECK(IsUint<1>(value)); |
| 450 | FALLTHROUGH_INTENDED; |
| 451 | case Primitive::Type::kPrimByte: |
| 452 | case Primitive::Type::kPrimChar: |
| 453 | case Primitive::Type::kPrimShort: |
| 454 | case Primitive::Type::kPrimInt: |
| 455 | DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value)); |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 456 | return GetIntConstant(static_cast<int32_t>(value), dex_pc); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 457 | |
| 458 | case Primitive::Type::kPrimLong: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 459 | return GetLongConstant(value, dex_pc); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 460 | |
| 461 | default: |
| 462 | LOG(FATAL) << "Unsupported constant type"; |
| 463 | UNREACHABLE(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 464 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 467 | void HGraph::CacheFloatConstant(HFloatConstant* constant) { |
| 468 | int32_t value = bit_cast<int32_t, float>(constant->GetValue()); |
| 469 | DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end()); |
| 470 | cached_float_constants_.Overwrite(value, constant); |
| 471 | } |
| 472 | |
| 473 | void HGraph::CacheDoubleConstant(HDoubleConstant* constant) { |
| 474 | int64_t value = bit_cast<int64_t, double>(constant->GetValue()); |
| 475 | DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end()); |
| 476 | cached_double_constants_.Overwrite(value, constant); |
| 477 | } |
| 478 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 479 | void HLoopInformation::Add(HBasicBlock* block) { |
| 480 | blocks_.SetBit(block->GetBlockId()); |
| 481 | } |
| 482 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 483 | void HLoopInformation::Remove(HBasicBlock* block) { |
| 484 | blocks_.ClearBit(block->GetBlockId()); |
| 485 | } |
| 486 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 487 | void HLoopInformation::PopulateRecursive(HBasicBlock* block) { |
| 488 | if (blocks_.IsBitSet(block->GetBlockId())) { |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | blocks_.SetBit(block->GetBlockId()); |
| 493 | block->SetInLoop(this); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 494 | for (HBasicBlock* predecessor : block->GetPredecessors()) { |
| 495 | PopulateRecursive(predecessor); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
| 499 | bool HLoopInformation::Populate() { |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 500 | DCHECK_EQ(blocks_.NumSetBits(), 0u) << "Loop information has already been populated"; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 501 | for (HBasicBlock* back_edge : GetBackEdges()) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 502 | DCHECK(back_edge->GetDominator() != nullptr); |
| 503 | if (!header_->Dominates(back_edge)) { |
| 504 | // This loop is not natural. Do not bother going further. |
| 505 | return false; |
| 506 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 507 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 508 | // Populate this loop: starting with the back edge, recursively add predecessors |
| 509 | // that are not already part of that loop. Set the header as part of the loop |
| 510 | // to end the recursion. |
| 511 | // This is a recursive implementation of the algorithm described in |
| 512 | // "Advanced Compiler Design & Implementation" (Muchnick) p192. |
| 513 | blocks_.SetBit(header_->GetBlockId()); |
| 514 | PopulateRecursive(back_edge); |
| 515 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 516 | return true; |
| 517 | } |
| 518 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 519 | void HLoopInformation::Update() { |
| 520 | HGraph* graph = header_->GetGraph(); |
| 521 | for (uint32_t id : blocks_.Indexes()) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 522 | HBasicBlock* block = graph->GetBlock(id); |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 523 | // Reset loop information of non-header blocks inside the loop, except |
| 524 | // members of inner nested loops because those should already have been |
| 525 | // updated by their own LoopInformation. |
| 526 | if (block->GetLoopInformation() == this && block != header_) { |
| 527 | block->SetLoopInformation(nullptr); |
| 528 | } |
| 529 | } |
| 530 | blocks_.ClearAllBits(); |
| 531 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 532 | if (back_edges_.empty()) { |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 533 | // The loop has been dismantled, delete its suspend check and remove info |
| 534 | // from the header. |
| 535 | DCHECK(HasSuspendCheck()); |
| 536 | header_->RemoveInstruction(suspend_check_); |
| 537 | header_->SetLoopInformation(nullptr); |
| 538 | header_ = nullptr; |
| 539 | suspend_check_ = nullptr; |
| 540 | } else { |
| 541 | if (kIsDebugBuild) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 542 | for (HBasicBlock* back_edge : back_edges_) { |
| 543 | DCHECK(header_->Dominates(back_edge)); |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | // This loop still has reachable back edges. Repopulate the list of blocks. |
| 547 | bool populate_successful = Populate(); |
| 548 | DCHECK(populate_successful); |
| 549 | } |
| 550 | } |
| 551 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 552 | HBasicBlock* HLoopInformation::GetPreHeader() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 553 | return header_->GetDominator(); |
| 554 | } |
| 555 | |
| 556 | bool HLoopInformation::Contains(const HBasicBlock& block) const { |
| 557 | return blocks_.IsBitSet(block.GetBlockId()); |
| 558 | } |
| 559 | |
| 560 | bool HLoopInformation::IsIn(const HLoopInformation& other) const { |
| 561 | return other.blocks_.IsBitSet(header_->GetBlockId()); |
| 562 | } |
| 563 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 564 | size_t HLoopInformation::GetLifetimeEnd() const { |
| 565 | size_t last_position = 0; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 566 | for (HBasicBlock* back_edge : GetBackEdges()) { |
| 567 | last_position = std::max(back_edge->GetLifetimeEnd(), last_position); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 568 | } |
| 569 | return last_position; |
| 570 | } |
| 571 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 572 | bool HBasicBlock::Dominates(HBasicBlock* other) const { |
| 573 | // Walk up the dominator tree from `other`, to find out if `this` |
| 574 | // is an ancestor. |
| 575 | HBasicBlock* current = other; |
| 576 | while (current != nullptr) { |
| 577 | if (current == this) { |
| 578 | return true; |
| 579 | } |
| 580 | current = current->GetDominator(); |
| 581 | } |
| 582 | return false; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 583 | } |
| 584 | |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 585 | static void UpdateInputsUsers(HInstruction* instruction) { |
| 586 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
| 587 | instruction->InputAt(i)->AddUseAt(instruction, i); |
| 588 | } |
| 589 | // Environment should be created later. |
| 590 | DCHECK(!instruction->HasEnvironment()); |
| 591 | } |
| 592 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 593 | void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial, |
| 594 | HInstruction* replacement) { |
| 595 | DCHECK(initial->GetBlock() == this); |
| 596 | InsertInstructionBefore(replacement, initial); |
| 597 | initial->ReplaceWith(replacement); |
| 598 | RemoveInstruction(initial); |
| 599 | } |
| 600 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 601 | static void Add(HInstructionList* instruction_list, |
| 602 | HBasicBlock* block, |
| 603 | HInstruction* instruction) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 604 | DCHECK(instruction->GetBlock() == nullptr); |
Nicolas Geoffray | 43c8642 | 2014-03-18 11:58:24 +0000 | [diff] [blame] | 605 | DCHECK_EQ(instruction->GetId(), -1); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 606 | instruction->SetBlock(block); |
| 607 | instruction->SetId(block->GetGraph()->GetNextInstructionId()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 608 | UpdateInputsUsers(instruction); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 609 | instruction_list->AddInstruction(instruction); |
| 610 | } |
| 611 | |
| 612 | void HBasicBlock::AddInstruction(HInstruction* instruction) { |
| 613 | Add(&instructions_, this, instruction); |
| 614 | } |
| 615 | |
| 616 | void HBasicBlock::AddPhi(HPhi* phi) { |
| 617 | Add(&phis_, this, phi); |
| 618 | } |
| 619 | |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 620 | void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) { |
| 621 | DCHECK(!cursor->IsPhi()); |
| 622 | DCHECK(!instruction->IsPhi()); |
| 623 | DCHECK_EQ(instruction->GetId(), -1); |
| 624 | DCHECK_NE(cursor->GetId(), -1); |
| 625 | DCHECK_EQ(cursor->GetBlock(), this); |
| 626 | DCHECK(!instruction->IsControlFlow()); |
| 627 | instruction->SetBlock(this); |
| 628 | instruction->SetId(GetGraph()->GetNextInstructionId()); |
| 629 | UpdateInputsUsers(instruction); |
| 630 | instructions_.InsertInstructionBefore(instruction, cursor); |
| 631 | } |
| 632 | |
Guillaume "Vermeille" Sanchez | 2967ec6 | 2015-04-24 16:36:52 +0100 | [diff] [blame] | 633 | void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) { |
| 634 | DCHECK(!cursor->IsPhi()); |
| 635 | DCHECK(!instruction->IsPhi()); |
| 636 | DCHECK_EQ(instruction->GetId(), -1); |
| 637 | DCHECK_NE(cursor->GetId(), -1); |
| 638 | DCHECK_EQ(cursor->GetBlock(), this); |
| 639 | DCHECK(!instruction->IsControlFlow()); |
| 640 | DCHECK(!cursor->IsControlFlow()); |
| 641 | instruction->SetBlock(this); |
| 642 | instruction->SetId(GetGraph()->GetNextInstructionId()); |
| 643 | UpdateInputsUsers(instruction); |
| 644 | instructions_.InsertInstructionAfter(instruction, cursor); |
| 645 | } |
| 646 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 647 | void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) { |
| 648 | DCHECK_EQ(phi->GetId(), -1); |
| 649 | DCHECK_NE(cursor->GetId(), -1); |
| 650 | DCHECK_EQ(cursor->GetBlock(), this); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 651 | phi->SetBlock(this); |
| 652 | phi->SetId(GetGraph()->GetNextInstructionId()); |
| 653 | UpdateInputsUsers(phi); |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 654 | phis_.InsertInstructionAfter(phi, cursor); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 655 | } |
| 656 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 657 | static void Remove(HInstructionList* instruction_list, |
| 658 | HBasicBlock* block, |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 659 | HInstruction* instruction, |
| 660 | bool ensure_safety) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 661 | DCHECK_EQ(block, instruction->GetBlock()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 662 | instruction->SetBlock(nullptr); |
| 663 | instruction_list->RemoveInstruction(instruction); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 664 | if (ensure_safety) { |
| 665 | DCHECK(instruction->GetUses().IsEmpty()); |
| 666 | DCHECK(instruction->GetEnvUses().IsEmpty()); |
| 667 | RemoveAsUser(instruction); |
| 668 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 669 | } |
| 670 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 671 | void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) { |
David Brazdil | c7508e9 | 2015-04-27 13:28:57 +0100 | [diff] [blame] | 672 | DCHECK(!instruction->IsPhi()); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 673 | Remove(&instructions_, this, instruction, ensure_safety); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 674 | } |
| 675 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 676 | void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) { |
| 677 | Remove(&phis_, this, phi, ensure_safety); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 678 | } |
| 679 | |
David Brazdil | c7508e9 | 2015-04-27 13:28:57 +0100 | [diff] [blame] | 680 | void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) { |
| 681 | if (instruction->IsPhi()) { |
| 682 | RemovePhi(instruction->AsPhi(), ensure_safety); |
| 683 | } else { |
| 684 | RemoveInstruction(instruction, ensure_safety); |
| 685 | } |
| 686 | } |
| 687 | |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 688 | void HEnvironment::CopyFrom(const ArenaVector<HInstruction*>& locals) { |
| 689 | for (size_t i = 0; i < locals.size(); i++) { |
| 690 | HInstruction* instruction = locals[i]; |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 691 | SetRawEnvAt(i, instruction); |
| 692 | if (instruction != nullptr) { |
| 693 | instruction->AddEnvUseAt(this, i); |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 698 | void HEnvironment::CopyFrom(HEnvironment* env) { |
| 699 | for (size_t i = 0; i < env->Size(); i++) { |
| 700 | HInstruction* instruction = env->GetInstructionAt(i); |
| 701 | SetRawEnvAt(i, instruction); |
| 702 | if (instruction != nullptr) { |
| 703 | instruction->AddEnvUseAt(this, i); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 704 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 708 | void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env, |
| 709 | HBasicBlock* loop_header) { |
| 710 | DCHECK(loop_header->IsLoopHeader()); |
| 711 | for (size_t i = 0; i < env->Size(); i++) { |
| 712 | HInstruction* instruction = env->GetInstructionAt(i); |
| 713 | SetRawEnvAt(i, instruction); |
| 714 | if (instruction == nullptr) { |
| 715 | continue; |
| 716 | } |
| 717 | if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) { |
| 718 | // At the end of the loop pre-header, the corresponding value for instruction |
| 719 | // is the first input of the phi. |
| 720 | HInstruction* initial = instruction->AsPhi()->InputAt(0); |
| 721 | DCHECK(initial->GetBlock()->Dominates(loop_header)); |
| 722 | SetRawEnvAt(i, initial); |
| 723 | initial->AddEnvUseAt(this, i); |
| 724 | } else { |
| 725 | instruction->AddEnvUseAt(this, i); |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 730 | void HEnvironment::RemoveAsUserOfInput(size_t index) const { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 731 | DCHECK_LT(index, Size()); |
| 732 | const HUserRecord<HEnvironment*>& user_record = vregs_[index]; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 733 | user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 734 | } |
| 735 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 736 | HInstruction* HInstruction::GetNextDisregardingMoves() const { |
| 737 | HInstruction* next = GetNext(); |
| 738 | while (next != nullptr && next->IsParallelMove()) { |
| 739 | next = next->GetNext(); |
| 740 | } |
| 741 | return next; |
| 742 | } |
| 743 | |
| 744 | HInstruction* HInstruction::GetPreviousDisregardingMoves() const { |
| 745 | HInstruction* previous = GetPrevious(); |
| 746 | while (previous != nullptr && previous->IsParallelMove()) { |
| 747 | previous = previous->GetPrevious(); |
| 748 | } |
| 749 | return previous; |
| 750 | } |
| 751 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 752 | void HInstructionList::AddInstruction(HInstruction* instruction) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 753 | if (first_instruction_ == nullptr) { |
| 754 | DCHECK(last_instruction_ == nullptr); |
| 755 | first_instruction_ = last_instruction_ = instruction; |
| 756 | } else { |
| 757 | last_instruction_->next_ = instruction; |
| 758 | instruction->previous_ = last_instruction_; |
| 759 | last_instruction_ = instruction; |
| 760 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 761 | } |
| 762 | |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 763 | void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) { |
| 764 | DCHECK(Contains(cursor)); |
| 765 | if (cursor == first_instruction_) { |
| 766 | cursor->previous_ = instruction; |
| 767 | instruction->next_ = cursor; |
| 768 | first_instruction_ = instruction; |
| 769 | } else { |
| 770 | instruction->previous_ = cursor->previous_; |
| 771 | instruction->next_ = cursor; |
| 772 | cursor->previous_ = instruction; |
| 773 | instruction->previous_->next_ = instruction; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) { |
| 778 | DCHECK(Contains(cursor)); |
| 779 | if (cursor == last_instruction_) { |
| 780 | cursor->next_ = instruction; |
| 781 | instruction->previous_ = cursor; |
| 782 | last_instruction_ = instruction; |
| 783 | } else { |
| 784 | instruction->next_ = cursor->next_; |
| 785 | instruction->previous_ = cursor; |
| 786 | cursor->next_ = instruction; |
| 787 | instruction->next_->previous_ = instruction; |
| 788 | } |
| 789 | } |
| 790 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 791 | void HInstructionList::RemoveInstruction(HInstruction* instruction) { |
| 792 | if (instruction->previous_ != nullptr) { |
| 793 | instruction->previous_->next_ = instruction->next_; |
| 794 | } |
| 795 | if (instruction->next_ != nullptr) { |
| 796 | instruction->next_->previous_ = instruction->previous_; |
| 797 | } |
| 798 | if (instruction == first_instruction_) { |
| 799 | first_instruction_ = instruction->next_; |
| 800 | } |
| 801 | if (instruction == last_instruction_) { |
| 802 | last_instruction_ = instruction->previous_; |
| 803 | } |
| 804 | } |
| 805 | |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 806 | bool HInstructionList::Contains(HInstruction* instruction) const { |
| 807 | for (HInstructionIterator it(*this); !it.Done(); it.Advance()) { |
| 808 | if (it.Current() == instruction) { |
| 809 | return true; |
| 810 | } |
| 811 | } |
| 812 | return false; |
| 813 | } |
| 814 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 815 | bool HInstructionList::FoundBefore(const HInstruction* instruction1, |
| 816 | const HInstruction* instruction2) const { |
| 817 | DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock()); |
| 818 | for (HInstructionIterator it(*this); !it.Done(); it.Advance()) { |
| 819 | if (it.Current() == instruction1) { |
| 820 | return true; |
| 821 | } |
| 822 | if (it.Current() == instruction2) { |
| 823 | return false; |
| 824 | } |
| 825 | } |
| 826 | LOG(FATAL) << "Did not find an order between two instructions of the same block."; |
| 827 | return true; |
| 828 | } |
| 829 | |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 830 | bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const { |
| 831 | if (other_instruction == this) { |
| 832 | // An instruction does not strictly dominate itself. |
| 833 | return false; |
| 834 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 835 | HBasicBlock* block = GetBlock(); |
| 836 | HBasicBlock* other_block = other_instruction->GetBlock(); |
| 837 | if (block != other_block) { |
| 838 | return GetBlock()->Dominates(other_instruction->GetBlock()); |
| 839 | } else { |
| 840 | // If both instructions are in the same block, ensure this |
| 841 | // instruction comes before `other_instruction`. |
| 842 | if (IsPhi()) { |
| 843 | if (!other_instruction->IsPhi()) { |
| 844 | // Phis appear before non phi-instructions so this instruction |
| 845 | // dominates `other_instruction`. |
| 846 | return true; |
| 847 | } else { |
| 848 | // There is no order among phis. |
| 849 | LOG(FATAL) << "There is no dominance between phis of a same block."; |
| 850 | return false; |
| 851 | } |
| 852 | } else { |
| 853 | // `this` is not a phi. |
| 854 | if (other_instruction->IsPhi()) { |
| 855 | // Phis appear before non phi-instructions so this instruction |
| 856 | // does not dominate `other_instruction`. |
| 857 | return false; |
| 858 | } else { |
| 859 | // Check whether this instruction comes before |
| 860 | // `other_instruction` in the instruction list. |
| 861 | return block->GetInstructions().FoundBefore(this, other_instruction); |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 867 | void HInstruction::ReplaceWith(HInstruction* other) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 868 | DCHECK(other != nullptr); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 869 | for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) { |
| 870 | HUseListNode<HInstruction*>* current = it.Current(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 871 | HInstruction* user = current->GetUser(); |
| 872 | size_t input_index = current->GetIndex(); |
| 873 | user->SetRawInputAt(input_index, other); |
| 874 | other->AddUseAt(user, input_index); |
| 875 | } |
| 876 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 877 | for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) { |
| 878 | HUseListNode<HEnvironment*>* current = it.Current(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 879 | HEnvironment* user = current->GetUser(); |
| 880 | size_t input_index = current->GetIndex(); |
| 881 | user->SetRawEnvAt(input_index, other); |
| 882 | other->AddEnvUseAt(user, input_index); |
| 883 | } |
| 884 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 885 | uses_.Clear(); |
| 886 | env_uses_.Clear(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 887 | } |
| 888 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 889 | void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 890 | RemoveAsUserOfInput(index); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 891 | SetRawInputAt(index, replacement); |
| 892 | replacement->AddUseAt(this, index); |
| 893 | } |
| 894 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 895 | size_t HInstruction::EnvironmentSize() const { |
| 896 | return HasEnvironment() ? environment_->Size() : 0; |
| 897 | } |
| 898 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 899 | void HPhi::AddInput(HInstruction* input) { |
| 900 | DCHECK(input->GetBlock() != nullptr); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 901 | inputs_.push_back(HUserRecord<HInstruction*>(input)); |
| 902 | input->AddUseAt(this, inputs_.size() - 1); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 903 | } |
| 904 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 905 | void HPhi::RemoveInputAt(size_t index) { |
| 906 | RemoveAsUserOfInput(index); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 907 | inputs_.erase(inputs_.begin() + index); |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 908 | for (size_t i = index, e = InputCount(); i < e; ++i) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 909 | DCHECK_EQ(InputRecordAt(i).GetUseNode()->GetIndex(), i + 1u); |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 910 | InputRecordAt(i).GetUseNode()->SetIndex(i); |
| 911 | } |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 912 | } |
| 913 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 914 | #define DEFINE_ACCEPT(name, super) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 915 | void H##name::Accept(HGraphVisitor* visitor) { \ |
| 916 | visitor->Visit##name(this); \ |
| 917 | } |
| 918 | |
| 919 | FOR_EACH_INSTRUCTION(DEFINE_ACCEPT) |
| 920 | |
| 921 | #undef DEFINE_ACCEPT |
| 922 | |
| 923 | void HGraphVisitor::VisitInsertionOrder() { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 924 | const ArenaVector<HBasicBlock*>& blocks = graph_->GetBlocks(); |
| 925 | for (HBasicBlock* block : blocks) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 926 | if (block != nullptr) { |
| 927 | VisitBasicBlock(block); |
| 928 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 932 | void HGraphVisitor::VisitReversePostOrder() { |
| 933 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 934 | VisitBasicBlock(it.Current()); |
| 935 | } |
| 936 | } |
| 937 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 938 | void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 939 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 940 | it.Current()->Accept(this); |
| 941 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 942 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 943 | it.Current()->Accept(this); |
| 944 | } |
| 945 | } |
| 946 | |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 947 | HConstant* HTypeConversion::TryStaticEvaluation() const { |
| 948 | HGraph* graph = GetBlock()->GetGraph(); |
| 949 | if (GetInput()->IsIntConstant()) { |
| 950 | int32_t value = GetInput()->AsIntConstant()->GetValue(); |
| 951 | switch (GetResultType()) { |
| 952 | case Primitive::kPrimLong: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 953 | return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 954 | case Primitive::kPrimFloat: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 955 | return graph->GetFloatConstant(static_cast<float>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 956 | case Primitive::kPrimDouble: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 957 | return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 958 | default: |
| 959 | return nullptr; |
| 960 | } |
| 961 | } else if (GetInput()->IsLongConstant()) { |
| 962 | int64_t value = GetInput()->AsLongConstant()->GetValue(); |
| 963 | switch (GetResultType()) { |
| 964 | case Primitive::kPrimInt: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 965 | return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 966 | case Primitive::kPrimFloat: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 967 | return graph->GetFloatConstant(static_cast<float>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 968 | case Primitive::kPrimDouble: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 969 | return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 970 | default: |
| 971 | return nullptr; |
| 972 | } |
| 973 | } else if (GetInput()->IsFloatConstant()) { |
| 974 | float value = GetInput()->AsFloatConstant()->GetValue(); |
| 975 | switch (GetResultType()) { |
| 976 | case Primitive::kPrimInt: |
| 977 | if (std::isnan(value)) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 978 | return graph->GetIntConstant(0, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 979 | if (value >= kPrimIntMax) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 980 | return graph->GetIntConstant(kPrimIntMax, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 981 | if (value <= kPrimIntMin) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 982 | return graph->GetIntConstant(kPrimIntMin, GetDexPc()); |
| 983 | return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 984 | case Primitive::kPrimLong: |
| 985 | if (std::isnan(value)) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 986 | return graph->GetLongConstant(0, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 987 | if (value >= kPrimLongMax) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 988 | return graph->GetLongConstant(kPrimLongMax, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 989 | if (value <= kPrimLongMin) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 990 | return graph->GetLongConstant(kPrimLongMin, GetDexPc()); |
| 991 | return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 992 | case Primitive::kPrimDouble: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 993 | return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 994 | default: |
| 995 | return nullptr; |
| 996 | } |
| 997 | } else if (GetInput()->IsDoubleConstant()) { |
| 998 | double value = GetInput()->AsDoubleConstant()->GetValue(); |
| 999 | switch (GetResultType()) { |
| 1000 | case Primitive::kPrimInt: |
| 1001 | if (std::isnan(value)) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1002 | return graph->GetIntConstant(0, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1003 | if (value >= kPrimIntMax) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1004 | return graph->GetIntConstant(kPrimIntMax, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1005 | if (value <= kPrimLongMin) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1006 | return graph->GetIntConstant(kPrimIntMin, GetDexPc()); |
| 1007 | return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1008 | case Primitive::kPrimLong: |
| 1009 | if (std::isnan(value)) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1010 | return graph->GetLongConstant(0, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1011 | if (value >= kPrimLongMax) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1012 | return graph->GetLongConstant(kPrimLongMax, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1013 | if (value <= kPrimLongMin) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1014 | return graph->GetLongConstant(kPrimLongMin, GetDexPc()); |
| 1015 | return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1016 | case Primitive::kPrimFloat: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1017 | return graph->GetFloatConstant(static_cast<float>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1018 | default: |
| 1019 | return nullptr; |
| 1020 | } |
| 1021 | } |
| 1022 | return nullptr; |
| 1023 | } |
| 1024 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1025 | HConstant* HUnaryOperation::TryStaticEvaluation() const { |
| 1026 | if (GetInput()->IsIntConstant()) { |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 1027 | return Evaluate(GetInput()->AsIntConstant()); |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1028 | } else if (GetInput()->IsLongConstant()) { |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 1029 | return Evaluate(GetInput()->AsLongConstant()); |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1030 | } |
| 1031 | return nullptr; |
| 1032 | } |
| 1033 | |
| 1034 | HConstant* HBinaryOperation::TryStaticEvaluation() const { |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 1035 | if (GetLeft()->IsIntConstant()) { |
| 1036 | if (GetRight()->IsIntConstant()) { |
| 1037 | return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsIntConstant()); |
| 1038 | } else if (GetRight()->IsLongConstant()) { |
| 1039 | return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsLongConstant()); |
| 1040 | } |
| 1041 | } else if (GetLeft()->IsLongConstant()) { |
| 1042 | if (GetRight()->IsIntConstant()) { |
| 1043 | return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsIntConstant()); |
| 1044 | } else if (GetRight()->IsLongConstant()) { |
| 1045 | return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsLongConstant()); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 1046 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1047 | } |
| 1048 | return nullptr; |
| 1049 | } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1050 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1051 | HConstant* HBinaryOperation::GetConstantRight() const { |
| 1052 | if (GetRight()->IsConstant()) { |
| 1053 | return GetRight()->AsConstant(); |
| 1054 | } else if (IsCommutative() && GetLeft()->IsConstant()) { |
| 1055 | return GetLeft()->AsConstant(); |
| 1056 | } else { |
| 1057 | return nullptr; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | // If `GetConstantRight()` returns one of the input, this returns the other |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1062 | // one. Otherwise it returns null. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1063 | HInstruction* HBinaryOperation::GetLeastConstantLeft() const { |
| 1064 | HInstruction* most_constant_right = GetConstantRight(); |
| 1065 | if (most_constant_right == nullptr) { |
| 1066 | return nullptr; |
| 1067 | } else if (most_constant_right == GetLeft()) { |
| 1068 | return GetRight(); |
| 1069 | } else { |
| 1070 | return GetLeft(); |
| 1071 | } |
| 1072 | } |
| 1073 | |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1074 | bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const { |
| 1075 | return this == instruction->GetPreviousDisregardingMoves(); |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 1076 | } |
| 1077 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1078 | bool HInstruction::Equals(HInstruction* other) const { |
| 1079 | if (!InstructionTypeEquals(other)) return false; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1080 | DCHECK_EQ(GetKind(), other->GetKind()); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1081 | if (!InstructionDataEquals(other)) return false; |
| 1082 | if (GetType() != other->GetType()) return false; |
| 1083 | if (InputCount() != other->InputCount()) return false; |
| 1084 | |
| 1085 | for (size_t i = 0, e = InputCount(); i < e; ++i) { |
| 1086 | if (InputAt(i) != other->InputAt(i)) return false; |
| 1087 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1088 | DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode()); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1089 | return true; |
| 1090 | } |
| 1091 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1092 | std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) { |
| 1093 | #define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break; |
| 1094 | switch (rhs) { |
| 1095 | FOR_EACH_INSTRUCTION(DECLARE_CASE) |
| 1096 | default: |
| 1097 | os << "Unknown instruction kind " << static_cast<int>(rhs); |
| 1098 | break; |
| 1099 | } |
| 1100 | #undef DECLARE_CASE |
| 1101 | return os; |
| 1102 | } |
| 1103 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 1104 | void HInstruction::MoveBefore(HInstruction* cursor) { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1105 | next_->previous_ = previous_; |
| 1106 | if (previous_ != nullptr) { |
| 1107 | previous_->next_ = next_; |
| 1108 | } |
| 1109 | if (block_->instructions_.first_instruction_ == this) { |
| 1110 | block_->instructions_.first_instruction_ = next_; |
| 1111 | } |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 1112 | DCHECK_NE(block_->instructions_.last_instruction_, this); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1113 | |
| 1114 | previous_ = cursor->previous_; |
| 1115 | if (previous_ != nullptr) { |
| 1116 | previous_->next_ = this; |
| 1117 | } |
| 1118 | next_ = cursor; |
| 1119 | cursor->previous_ = this; |
| 1120 | block_ = cursor->block_; |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 1121 | |
| 1122 | if (block_->instructions_.first_instruction_ == cursor) { |
| 1123 | block_->instructions_.first_instruction_ = this; |
| 1124 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1127 | HBasicBlock* HBasicBlock::SplitBefore(HInstruction* cursor) { |
| 1128 | DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented"; |
| 1129 | DCHECK_EQ(cursor->GetBlock(), this); |
| 1130 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1131 | HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), |
| 1132 | cursor->GetDexPc()); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1133 | new_block->instructions_.first_instruction_ = cursor; |
| 1134 | new_block->instructions_.last_instruction_ = instructions_.last_instruction_; |
| 1135 | instructions_.last_instruction_ = cursor->previous_; |
| 1136 | if (cursor->previous_ == nullptr) { |
| 1137 | instructions_.first_instruction_ = nullptr; |
| 1138 | } else { |
| 1139 | cursor->previous_->next_ = nullptr; |
| 1140 | cursor->previous_ = nullptr; |
| 1141 | } |
| 1142 | |
| 1143 | new_block->instructions_.SetBlockOfInstructions(new_block); |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1144 | AddInstruction(new (GetGraph()->GetArena()) HGoto(new_block->GetDexPc())); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1145 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1146 | for (HBasicBlock* successor : GetSuccessors()) { |
| 1147 | new_block->successors_.push_back(successor); |
| 1148 | successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1149 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1150 | successors_.clear(); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1151 | AddSuccessor(new_block); |
| 1152 | |
David Brazdil | 56e1acc | 2015-06-30 15:41:36 +0100 | [diff] [blame] | 1153 | GetGraph()->AddBlock(new_block); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1154 | return new_block; |
| 1155 | } |
| 1156 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1157 | HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) { |
| 1158 | DCHECK(!cursor->IsControlFlow()); |
| 1159 | DCHECK_NE(instructions_.last_instruction_, cursor); |
| 1160 | DCHECK_EQ(cursor->GetBlock(), this); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1161 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1162 | HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc()); |
| 1163 | new_block->instructions_.first_instruction_ = cursor->GetNext(); |
| 1164 | new_block->instructions_.last_instruction_ = instructions_.last_instruction_; |
| 1165 | cursor->next_->previous_ = nullptr; |
| 1166 | cursor->next_ = nullptr; |
| 1167 | instructions_.last_instruction_ = cursor; |
| 1168 | |
| 1169 | new_block->instructions_.SetBlockOfInstructions(new_block); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1170 | for (HBasicBlock* successor : GetSuccessors()) { |
| 1171 | new_block->successors_.push_back(successor); |
| 1172 | successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block; |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1173 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1174 | successors_.clear(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1175 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1176 | for (HBasicBlock* dominated : GetDominatedBlocks()) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1177 | dominated->dominator_ = new_block; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1178 | new_block->dominated_blocks_.push_back(dominated); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1179 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1180 | dominated_blocks_.clear(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1181 | return new_block; |
| 1182 | } |
| 1183 | |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1184 | const HTryBoundary* HBasicBlock::ComputeTryEntryOfSuccessors() const { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1185 | if (EndsWithTryBoundary()) { |
| 1186 | HTryBoundary* try_boundary = GetLastInstruction()->AsTryBoundary(); |
| 1187 | if (try_boundary->IsEntry()) { |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1188 | DCHECK(!IsTryBlock()); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1189 | return try_boundary; |
| 1190 | } else { |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1191 | DCHECK(IsTryBlock()); |
| 1192 | DCHECK(try_catch_information_->GetTryEntry().HasSameExceptionHandlersAs(*try_boundary)); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1193 | return nullptr; |
| 1194 | } |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1195 | } else if (IsTryBlock()) { |
| 1196 | return &try_catch_information_->GetTryEntry(); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1197 | } else { |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1198 | return nullptr; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1199 | } |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | static bool HasOnlyOneInstruction(const HBasicBlock& block) { |
| 1203 | return block.GetPhis().IsEmpty() |
| 1204 | && !block.GetInstructions().IsEmpty() |
| 1205 | && block.GetFirstInstruction() == block.GetLastInstruction(); |
| 1206 | } |
| 1207 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1208 | bool HBasicBlock::IsSingleGoto() const { |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1209 | return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsGoto(); |
| 1210 | } |
| 1211 | |
| 1212 | bool HBasicBlock::IsSingleTryBoundary() const { |
| 1213 | return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsTryBoundary(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 1216 | bool HBasicBlock::EndsWithControlFlowInstruction() const { |
| 1217 | return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow(); |
| 1218 | } |
| 1219 | |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 1220 | bool HBasicBlock::EndsWithIf() const { |
| 1221 | return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf(); |
| 1222 | } |
| 1223 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1224 | bool HBasicBlock::EndsWithTryBoundary() const { |
| 1225 | return !GetInstructions().IsEmpty() && GetLastInstruction()->IsTryBoundary(); |
| 1226 | } |
| 1227 | |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 1228 | bool HBasicBlock::HasSinglePhi() const { |
| 1229 | return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr; |
| 1230 | } |
| 1231 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1232 | bool HTryBoundary::HasSameExceptionHandlersAs(const HTryBoundary& other) const { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1233 | if (GetBlock()->GetSuccessors().size() != other.GetBlock()->GetSuccessors().size()) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1234 | return false; |
| 1235 | } |
| 1236 | |
David Brazdil | b618ade | 2015-07-29 10:31:29 +0100 | [diff] [blame] | 1237 | // Exception handlers need to be stored in the same order. |
| 1238 | for (HExceptionHandlerIterator it1(*this), it2(other); |
| 1239 | !it1.Done(); |
| 1240 | it1.Advance(), it2.Advance()) { |
| 1241 | DCHECK(!it2.Done()); |
| 1242 | if (it1.Current() != it2.Current()) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1243 | return false; |
| 1244 | } |
| 1245 | } |
| 1246 | return true; |
| 1247 | } |
| 1248 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1249 | size_t HInstructionList::CountSize() const { |
| 1250 | size_t size = 0; |
| 1251 | HInstruction* current = first_instruction_; |
| 1252 | for (; current != nullptr; current = current->GetNext()) { |
| 1253 | size++; |
| 1254 | } |
| 1255 | return size; |
| 1256 | } |
| 1257 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1258 | void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const { |
| 1259 | for (HInstruction* current = first_instruction_; |
| 1260 | current != nullptr; |
| 1261 | current = current->GetNext()) { |
| 1262 | current->SetBlock(block); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) { |
| 1267 | DCHECK(Contains(cursor)); |
| 1268 | if (!instruction_list.IsEmpty()) { |
| 1269 | if (cursor == last_instruction_) { |
| 1270 | last_instruction_ = instruction_list.last_instruction_; |
| 1271 | } else { |
| 1272 | cursor->next_->previous_ = instruction_list.last_instruction_; |
| 1273 | } |
| 1274 | instruction_list.last_instruction_->next_ = cursor->next_; |
| 1275 | cursor->next_ = instruction_list.first_instruction_; |
| 1276 | instruction_list.first_instruction_->previous_ = cursor; |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | void HInstructionList::Add(const HInstructionList& instruction_list) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1281 | if (IsEmpty()) { |
| 1282 | first_instruction_ = instruction_list.first_instruction_; |
| 1283 | last_instruction_ = instruction_list.last_instruction_; |
| 1284 | } else { |
| 1285 | AddAfter(last_instruction_, instruction_list); |
| 1286 | } |
| 1287 | } |
| 1288 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1289 | void HBasicBlock::DisconnectAndDelete() { |
| 1290 | // Dominators must be removed after all the blocks they dominate. This way |
| 1291 | // a loop header is removed last, a requirement for correct loop information |
| 1292 | // iteration. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1293 | DCHECK(dominated_blocks_.empty()); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1294 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1295 | // Remove the block from all loops it is included in. |
| 1296 | for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) { |
| 1297 | HLoopInformation* loop_info = it.Current(); |
| 1298 | loop_info->Remove(this); |
| 1299 | if (loop_info->IsBackEdge(*this)) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 1300 | // If this was the last back edge of the loop, we deliberately leave the |
| 1301 | // loop in an inconsistent state and will fail SSAChecker unless the |
| 1302 | // entire loop is removed during the pass. |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1303 | loop_info->RemoveBackEdge(this); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | // Disconnect the block from its predecessors and update their control-flow |
| 1308 | // instructions. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1309 | for (HBasicBlock* predecessor : predecessors_) { |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1310 | HInstruction* last_instruction = predecessor->GetLastInstruction(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1311 | predecessor->RemoveSuccessor(this); |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 1312 | uint32_t num_pred_successors = predecessor->GetSuccessors().size(); |
| 1313 | if (num_pred_successors == 1u) { |
| 1314 | // If we have one successor after removing one, then we must have |
| 1315 | // had an HIf or HPackedSwitch, as they have more than one successor. |
| 1316 | // Replace those with a HGoto. |
| 1317 | DCHECK(last_instruction->IsIf() || last_instruction->IsPackedSwitch()); |
| 1318 | predecessor->RemoveInstruction(last_instruction); |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1319 | predecessor->AddInstruction(new (graph_->GetArena()) HGoto(last_instruction->GetDexPc())); |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 1320 | } else if (num_pred_successors == 0u) { |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1321 | // The predecessor has no remaining successors and therefore must be dead. |
| 1322 | // We deliberately leave it without a control-flow instruction so that the |
| 1323 | // SSAChecker fails unless it is not removed during the pass too. |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 1324 | predecessor->RemoveInstruction(last_instruction); |
| 1325 | } else { |
| 1326 | // There are multiple successors left. This must come from a HPackedSwitch |
| 1327 | // and we are in the middle of removing the HPackedSwitch. Like above, leave |
| 1328 | // this alone, and the SSAChecker will fail if it is not removed as well. |
| 1329 | DCHECK(last_instruction->IsPackedSwitch()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1330 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1331 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1332 | predecessors_.clear(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1333 | |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 1334 | // Disconnect the block from its successors and update their phis. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1335 | for (HBasicBlock* successor : successors_) { |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1336 | // Delete this block from the list of predecessors. |
| 1337 | size_t this_index = successor->GetPredecessorIndexOf(this); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1338 | successor->predecessors_.erase(successor->predecessors_.begin() + this_index); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1339 | |
| 1340 | // Check that `successor` has other predecessors, otherwise `this` is the |
| 1341 | // dominator of `successor` which violates the order DCHECKed at the top. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1342 | DCHECK(!successor->predecessors_.empty()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1343 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1344 | // Remove this block's entries in the successor's phis. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1345 | if (successor->predecessors_.size() == 1u) { |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1346 | // The successor has just one predecessor left. Replace phis with the only |
| 1347 | // remaining input. |
| 1348 | for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 1349 | HPhi* phi = phi_it.Current()->AsPhi(); |
| 1350 | phi->ReplaceWith(phi->InputAt(1 - this_index)); |
| 1351 | successor->RemovePhi(phi); |
| 1352 | } |
| 1353 | } else { |
| 1354 | for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 1355 | phi_it.Current()->AsPhi()->RemoveInputAt(this_index); |
| 1356 | } |
| 1357 | } |
| 1358 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1359 | successors_.clear(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1360 | |
| 1361 | // Disconnect from the dominator. |
| 1362 | dominator_->RemoveDominatedBlock(this); |
| 1363 | SetDominator(nullptr); |
| 1364 | |
| 1365 | // Delete from the graph. The function safely deletes remaining instructions |
| 1366 | // and updates the reverse post order. |
| 1367 | graph_->DeleteDeadBlock(this); |
| 1368 | SetGraph(nullptr); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | void HBasicBlock::MergeWith(HBasicBlock* other) { |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1372 | DCHECK_EQ(GetGraph(), other->GetGraph()); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1373 | DCHECK(ContainsElement(dominated_blocks_, other)); |
| 1374 | DCHECK_EQ(GetSingleSuccessor(), other); |
| 1375 | DCHECK_EQ(other->GetSinglePredecessor(), this); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1376 | DCHECK(other->GetPhis().IsEmpty()); |
| 1377 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1378 | // Move instructions from `other` to `this`. |
| 1379 | DCHECK(EndsWithControlFlowInstruction()); |
| 1380 | RemoveInstruction(GetLastInstruction()); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1381 | instructions_.Add(other->GetInstructions()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1382 | other->instructions_.SetBlockOfInstructions(this); |
| 1383 | other->instructions_.Clear(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1384 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1385 | // Remove `other` from the loops it is included in. |
| 1386 | for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) { |
| 1387 | HLoopInformation* loop_info = it.Current(); |
| 1388 | loop_info->Remove(other); |
| 1389 | if (loop_info->IsBackEdge(*other)) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 1390 | loop_info->ReplaceBackEdge(other, this); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | // Update links to the successors of `other`. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1395 | successors_.clear(); |
| 1396 | while (!other->successors_.empty()) { |
| 1397 | HBasicBlock* successor = other->GetSuccessor(0); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1398 | successor->ReplacePredecessor(other, this); |
| 1399 | } |
| 1400 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1401 | // Update the dominator tree. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1402 | RemoveDominatedBlock(other); |
| 1403 | for (HBasicBlock* dominated : other->GetDominatedBlocks()) { |
| 1404 | dominated_blocks_.push_back(dominated); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1405 | dominated->SetDominator(this); |
| 1406 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1407 | other->dominated_blocks_.clear(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1408 | other->dominator_ = nullptr; |
| 1409 | |
| 1410 | // Clear the list of predecessors of `other` in preparation of deleting it. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1411 | other->predecessors_.clear(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1412 | |
| 1413 | // Delete `other` from the graph. The function updates reverse post order. |
| 1414 | graph_->DeleteDeadBlock(other); |
| 1415 | other->SetGraph(nullptr); |
| 1416 | } |
| 1417 | |
| 1418 | void HBasicBlock::MergeWithInlined(HBasicBlock* other) { |
| 1419 | DCHECK_NE(GetGraph(), other->GetGraph()); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1420 | DCHECK(GetDominatedBlocks().empty()); |
| 1421 | DCHECK(GetSuccessors().empty()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1422 | DCHECK(!EndsWithControlFlowInstruction()); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1423 | DCHECK(other->GetSinglePredecessor()->IsEntryBlock()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1424 | DCHECK(other->GetPhis().IsEmpty()); |
| 1425 | DCHECK(!other->IsInLoop()); |
| 1426 | |
| 1427 | // Move instructions from `other` to `this`. |
| 1428 | instructions_.Add(other->GetInstructions()); |
| 1429 | other->instructions_.SetBlockOfInstructions(this); |
| 1430 | |
| 1431 | // Update links to the successors of `other`. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1432 | successors_.clear(); |
| 1433 | while (!other->successors_.empty()) { |
| 1434 | HBasicBlock* successor = other->GetSuccessor(0); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1435 | successor->ReplacePredecessor(other, this); |
| 1436 | } |
| 1437 | |
| 1438 | // Update the dominator tree. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1439 | for (HBasicBlock* dominated : other->GetDominatedBlocks()) { |
| 1440 | dominated_blocks_.push_back(dominated); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1441 | dominated->SetDominator(this); |
| 1442 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1443 | other->dominated_blocks_.clear(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1444 | other->dominator_ = nullptr; |
| 1445 | other->graph_ = nullptr; |
| 1446 | } |
| 1447 | |
| 1448 | void HBasicBlock::ReplaceWith(HBasicBlock* other) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1449 | while (!GetPredecessors().empty()) { |
| 1450 | HBasicBlock* predecessor = GetPredecessor(0); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1451 | predecessor->ReplaceSuccessor(this, other); |
| 1452 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1453 | while (!GetSuccessors().empty()) { |
| 1454 | HBasicBlock* successor = GetSuccessor(0); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1455 | successor->ReplacePredecessor(this, other); |
| 1456 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1457 | for (HBasicBlock* dominated : GetDominatedBlocks()) { |
| 1458 | other->AddDominatedBlock(dominated); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1459 | } |
| 1460 | GetDominator()->ReplaceDominatedBlock(this, other); |
| 1461 | other->SetDominator(GetDominator()); |
| 1462 | dominator_ = nullptr; |
| 1463 | graph_ = nullptr; |
| 1464 | } |
| 1465 | |
| 1466 | // Create space in `blocks` for adding `number_of_new_blocks` entries |
| 1467 | // starting at location `at`. Blocks after `at` are moved accordingly. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1468 | static void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks, |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1469 | size_t number_of_new_blocks, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1470 | size_t after) { |
| 1471 | DCHECK_LT(after, blocks->size()); |
| 1472 | size_t old_size = blocks->size(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1473 | size_t new_size = old_size + number_of_new_blocks; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1474 | blocks->resize(new_size); |
| 1475 | std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end()); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1478 | void HGraph::DeleteDeadBlock(HBasicBlock* block) { |
| 1479 | DCHECK_EQ(block->GetGraph(), this); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1480 | DCHECK(block->GetSuccessors().empty()); |
| 1481 | DCHECK(block->GetPredecessors().empty()); |
| 1482 | DCHECK(block->GetDominatedBlocks().empty()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1483 | DCHECK(block->GetDominator() == nullptr); |
| 1484 | |
| 1485 | for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 1486 | block->RemoveInstruction(it.Current()); |
| 1487 | } |
| 1488 | for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 1489 | block->RemovePhi(it.Current()->AsPhi()); |
| 1490 | } |
| 1491 | |
David Brazdil | c7af85d | 2015-05-26 12:05:55 +0100 | [diff] [blame] | 1492 | if (block->IsExitBlock()) { |
| 1493 | exit_block_ = nullptr; |
| 1494 | } |
| 1495 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1496 | RemoveElement(reverse_post_order_, block); |
| 1497 | blocks_[block->GetBlockId()] = nullptr; |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1498 | } |
| 1499 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1500 | HInstruction* HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) { |
David Brazdil | c7af85d | 2015-05-26 12:05:55 +0100 | [diff] [blame] | 1501 | DCHECK(HasExitBlock()) << "Unimplemented scenario"; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1502 | // Update the environments in this graph to have the invoke's environment |
| 1503 | // as parent. |
| 1504 | { |
| 1505 | HReversePostOrderIterator it(*this); |
| 1506 | it.Advance(); // Skip the entry block, we do not need to update the entry's suspend check. |
| 1507 | for (; !it.Done(); it.Advance()) { |
| 1508 | HBasicBlock* block = it.Current(); |
| 1509 | for (HInstructionIterator instr_it(block->GetInstructions()); |
| 1510 | !instr_it.Done(); |
| 1511 | instr_it.Advance()) { |
| 1512 | HInstruction* current = instr_it.Current(); |
| 1513 | if (current->NeedsEnvironment()) { |
| 1514 | current->GetEnvironment()->SetAndCopyParentChain( |
| 1515 | outer_graph->GetArena(), invoke->GetEnvironment()); |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | } |
| 1520 | outer_graph->UpdateMaximumNumberOfOutVRegs(GetMaximumNumberOfOutVRegs()); |
| 1521 | if (HasBoundsChecks()) { |
| 1522 | outer_graph->SetHasBoundsChecks(true); |
| 1523 | } |
| 1524 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1525 | HInstruction* return_value = nullptr; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1526 | if (GetBlocks().size() == 3) { |
Nicolas Geoffray | be31ff9 | 2015-02-04 14:52:20 +0000 | [diff] [blame] | 1527 | // Simple case of an entry block, a body block, and an exit block. |
| 1528 | // Put the body block's instruction into `invoke`'s block. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1529 | HBasicBlock* body = GetBlock(1); |
| 1530 | DCHECK(GetBlock(0)->IsEntryBlock()); |
| 1531 | DCHECK(GetBlock(2)->IsExitBlock()); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1532 | DCHECK(!body->IsExitBlock()); |
| 1533 | HInstruction* last = body->GetLastInstruction(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1534 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1535 | invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions()); |
| 1536 | body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1537 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1538 | // Replace the invoke with the return value of the inlined graph. |
| 1539 | if (last->IsReturn()) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1540 | return_value = last->InputAt(0); |
| 1541 | invoke->ReplaceWith(return_value); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1542 | } else { |
| 1543 | DCHECK(last->IsReturnVoid()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1544 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1545 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1546 | invoke->GetBlock()->RemoveInstruction(last); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1547 | } else { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1548 | // Need to inline multiple blocks. We split `invoke`'s block |
| 1549 | // into two blocks, merge the first block of the inlined graph into |
Nicolas Geoffray | be31ff9 | 2015-02-04 14:52:20 +0000 | [diff] [blame] | 1550 | // the first half, and replace the exit block of the inlined graph |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1551 | // with the second half. |
| 1552 | ArenaAllocator* allocator = outer_graph->GetArena(); |
| 1553 | HBasicBlock* at = invoke->GetBlock(); |
| 1554 | HBasicBlock* to = at->SplitAfter(invoke); |
| 1555 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1556 | HBasicBlock* first = entry_block_->GetSuccessor(0); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1557 | DCHECK(!first->IsInLoop()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1558 | at->MergeWithInlined(first); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1559 | exit_block_->ReplaceWith(to); |
| 1560 | |
| 1561 | // Update all predecessors of the exit block (now the `to` block) |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1562 | // to not `HReturn` but `HGoto` instead. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1563 | bool returns_void = to->GetPredecessor(0)->GetLastInstruction()->IsReturnVoid(); |
| 1564 | if (to->GetPredecessors().size() == 1) { |
| 1565 | HBasicBlock* predecessor = to->GetPredecessor(0); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1566 | HInstruction* last = predecessor->GetLastInstruction(); |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1567 | if (!returns_void) { |
| 1568 | return_value = last->InputAt(0); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1569 | } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1570 | predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc())); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1571 | predecessor->RemoveInstruction(last); |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1572 | } else { |
| 1573 | if (!returns_void) { |
| 1574 | // There will be multiple returns. |
Nicolas Geoffray | 4f1a384 | 2015-03-12 10:34:11 +0000 | [diff] [blame] | 1575 | return_value = new (allocator) HPhi( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1576 | allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()), to->GetDexPc()); |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1577 | to->AddPhi(return_value->AsPhi()); |
| 1578 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1579 | for (HBasicBlock* predecessor : to->GetPredecessors()) { |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1580 | HInstruction* last = predecessor->GetLastInstruction(); |
| 1581 | if (!returns_void) { |
| 1582 | return_value->AsPhi()->AddInput(last->InputAt(0)); |
| 1583 | } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1584 | predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc())); |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1585 | predecessor->RemoveInstruction(last); |
| 1586 | } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | if (return_value != nullptr) { |
| 1590 | invoke->ReplaceWith(return_value); |
| 1591 | } |
| 1592 | |
| 1593 | // Update the meta information surrounding blocks: |
| 1594 | // (1) the graph they are now in, |
| 1595 | // (2) the reverse post order of that graph, |
| 1596 | // (3) the potential loop information they are now in. |
| 1597 | |
| 1598 | // We don't add the entry block, the exit block, and the first block, which |
| 1599 | // has been merged with `at`. |
| 1600 | static constexpr int kNumberOfSkippedBlocksInCallee = 3; |
| 1601 | |
| 1602 | // We add the `to` block. |
| 1603 | static constexpr int kNumberOfNewBlocksInCaller = 1; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1604 | size_t blocks_added = (reverse_post_order_.size() - kNumberOfSkippedBlocksInCallee) |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1605 | + kNumberOfNewBlocksInCaller; |
| 1606 | |
| 1607 | // Find the location of `at` in the outer graph's reverse post order. The new |
| 1608 | // blocks will be added after it. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1609 | size_t index_of_at = IndexOfElement(outer_graph->reverse_post_order_, at); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1610 | MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at); |
| 1611 | |
| 1612 | // Do a reverse post order of the blocks in the callee and do (1), (2), |
| 1613 | // and (3) to the blocks that apply. |
| 1614 | HLoopInformation* info = at->GetLoopInformation(); |
| 1615 | for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) { |
| 1616 | HBasicBlock* current = it.Current(); |
| 1617 | if (current != exit_block_ && current != entry_block_ && current != first) { |
| 1618 | DCHECK(!current->IsInLoop()); |
| 1619 | DCHECK(current->GetGraph() == this); |
| 1620 | current->SetGraph(outer_graph); |
| 1621 | outer_graph->AddBlock(current); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1622 | outer_graph->reverse_post_order_[++index_of_at] = current; |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1623 | if (info != nullptr) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1624 | current->SetLoopInformation(info); |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 1625 | for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) { |
| 1626 | loop_it.Current()->Add(current); |
| 1627 | } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | // Do (1), (2), and (3) to `to`. |
| 1633 | to->SetGraph(outer_graph); |
| 1634 | outer_graph->AddBlock(to); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1635 | outer_graph->reverse_post_order_[++index_of_at] = to; |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1636 | if (info != nullptr) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1637 | to->SetLoopInformation(info); |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 1638 | for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) { |
| 1639 | loop_it.Current()->Add(to); |
| 1640 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1641 | if (info->IsBackEdge(*at)) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 1642 | // Only `to` can become a back edge, as the inlined blocks |
| 1643 | // are predecessors of `to`. |
| 1644 | info->ReplaceBackEdge(at, to); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1645 | } |
| 1646 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1647 | } |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 1648 | |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1649 | // Update the next instruction id of the outer graph, so that instructions |
| 1650 | // added later get bigger ids than those in the inner graph. |
| 1651 | outer_graph->SetCurrentInstructionId(GetNextInstructionId()); |
| 1652 | |
| 1653 | // Walk over the entry block and: |
| 1654 | // - Move constants from the entry block to the outer_graph's entry block, |
| 1655 | // - Replace HParameterValue instructions with their real value. |
| 1656 | // - Remove suspend checks, that hold an environment. |
| 1657 | // We must do this after the other blocks have been inlined, otherwise ids of |
| 1658 | // constants could overlap with the inner graph. |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 1659 | size_t parameter_index = 0; |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1660 | for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) { |
| 1661 | HInstruction* current = it.Current(); |
| 1662 | if (current->IsNullConstant()) { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1663 | current->ReplaceWith(outer_graph->GetNullConstant(current->GetDexPc())); |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1664 | } else if (current->IsIntConstant()) { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1665 | current->ReplaceWith(outer_graph->GetIntConstant( |
| 1666 | current->AsIntConstant()->GetValue(), current->GetDexPc())); |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1667 | } else if (current->IsLongConstant()) { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1668 | current->ReplaceWith(outer_graph->GetLongConstant( |
| 1669 | current->AsLongConstant()->GetValue(), current->GetDexPc())); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 1670 | } else if (current->IsFloatConstant()) { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1671 | current->ReplaceWith(outer_graph->GetFloatConstant( |
| 1672 | current->AsFloatConstant()->GetValue(), current->GetDexPc())); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 1673 | } else if (current->IsDoubleConstant()) { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1674 | current->ReplaceWith(outer_graph->GetDoubleConstant( |
| 1675 | current->AsDoubleConstant()->GetValue(), current->GetDexPc())); |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1676 | } else if (current->IsParameterValue()) { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 1677 | if (kIsDebugBuild |
| 1678 | && invoke->IsInvokeStaticOrDirect() |
| 1679 | && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) { |
| 1680 | // Ensure we do not use the last input of `invoke`, as it |
| 1681 | // contains a clinit check which is not an actual argument. |
| 1682 | size_t last_input_index = invoke->InputCount() - 1; |
| 1683 | DCHECK(parameter_index != last_input_index); |
| 1684 | } |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1685 | current->ReplaceWith(invoke->InputAt(parameter_index++)); |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 1686 | } else if (current->IsCurrentMethod()) { |
| 1687 | current->ReplaceWith(outer_graph->GetCurrentMethod()); |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1688 | } else { |
| 1689 | DCHECK(current->IsGoto() || current->IsSuspendCheck()); |
| 1690 | entry_block_->RemoveInstruction(current); |
| 1691 | } |
| 1692 | } |
| 1693 | |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 1694 | // Finally remove the invoke from the caller. |
| 1695 | invoke->GetBlock()->RemoveInstruction(invoke); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1696 | |
| 1697 | return return_value; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1700 | /* |
| 1701 | * Loop will be transformed to: |
| 1702 | * old_pre_header |
| 1703 | * | |
| 1704 | * if_block |
| 1705 | * / \ |
| 1706 | * dummy_block deopt_block |
| 1707 | * \ / |
| 1708 | * new_pre_header |
| 1709 | * | |
| 1710 | * header |
| 1711 | */ |
| 1712 | void HGraph::TransformLoopHeaderForBCE(HBasicBlock* header) { |
| 1713 | DCHECK(header->IsLoopHeader()); |
| 1714 | HBasicBlock* pre_header = header->GetDominator(); |
| 1715 | |
| 1716 | // Need this to avoid critical edge. |
| 1717 | HBasicBlock* if_block = new (arena_) HBasicBlock(this, header->GetDexPc()); |
| 1718 | // Need this to avoid critical edge. |
| 1719 | HBasicBlock* dummy_block = new (arena_) HBasicBlock(this, header->GetDexPc()); |
| 1720 | HBasicBlock* deopt_block = new (arena_) HBasicBlock(this, header->GetDexPc()); |
| 1721 | HBasicBlock* new_pre_header = new (arena_) HBasicBlock(this, header->GetDexPc()); |
| 1722 | AddBlock(if_block); |
| 1723 | AddBlock(dummy_block); |
| 1724 | AddBlock(deopt_block); |
| 1725 | AddBlock(new_pre_header); |
| 1726 | |
| 1727 | header->ReplacePredecessor(pre_header, new_pre_header); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1728 | pre_header->successors_.clear(); |
| 1729 | pre_header->dominated_blocks_.clear(); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1730 | |
| 1731 | pre_header->AddSuccessor(if_block); |
| 1732 | if_block->AddSuccessor(dummy_block); // True successor |
| 1733 | if_block->AddSuccessor(deopt_block); // False successor |
| 1734 | dummy_block->AddSuccessor(new_pre_header); |
| 1735 | deopt_block->AddSuccessor(new_pre_header); |
| 1736 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1737 | pre_header->dominated_blocks_.push_back(if_block); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1738 | if_block->SetDominator(pre_header); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1739 | if_block->dominated_blocks_.push_back(dummy_block); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1740 | dummy_block->SetDominator(if_block); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1741 | if_block->dominated_blocks_.push_back(deopt_block); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1742 | deopt_block->SetDominator(if_block); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1743 | if_block->dominated_blocks_.push_back(new_pre_header); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1744 | new_pre_header->SetDominator(if_block); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1745 | new_pre_header->dominated_blocks_.push_back(header); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1746 | header->SetDominator(new_pre_header); |
| 1747 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1748 | size_t index_of_header = IndexOfElement(reverse_post_order_, header); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1749 | MakeRoomFor(&reverse_post_order_, 4, index_of_header - 1); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1750 | reverse_post_order_[index_of_header++] = if_block; |
| 1751 | reverse_post_order_[index_of_header++] = dummy_block; |
| 1752 | reverse_post_order_[index_of_header++] = deopt_block; |
| 1753 | reverse_post_order_[index_of_header++] = new_pre_header; |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1754 | |
| 1755 | HLoopInformation* info = pre_header->GetLoopInformation(); |
| 1756 | if (info != nullptr) { |
| 1757 | if_block->SetLoopInformation(info); |
| 1758 | dummy_block->SetLoopInformation(info); |
| 1759 | deopt_block->SetLoopInformation(info); |
| 1760 | new_pre_header->SetLoopInformation(info); |
| 1761 | for (HLoopInformationOutwardIterator loop_it(*pre_header); |
| 1762 | !loop_it.Done(); |
| 1763 | loop_it.Advance()) { |
| 1764 | loop_it.Current()->Add(if_block); |
| 1765 | loop_it.Current()->Add(dummy_block); |
| 1766 | loop_it.Current()->Add(deopt_block); |
| 1767 | loop_it.Current()->Add(new_pre_header); |
| 1768 | } |
| 1769 | } |
| 1770 | } |
| 1771 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1772 | void HInstruction::SetReferenceTypeInfo(ReferenceTypeInfo rti) { |
| 1773 | if (kIsDebugBuild) { |
| 1774 | DCHECK_EQ(GetType(), Primitive::kPrimNot); |
| 1775 | ScopedObjectAccess soa(Thread::Current()); |
| 1776 | DCHECK(rti.IsValid()) << "Invalid RTI for " << DebugName(); |
| 1777 | if (IsBoundType()) { |
| 1778 | // Having the test here spares us from making the method virtual just for |
| 1779 | // the sake of a DCHECK. |
| 1780 | ReferenceTypeInfo upper_bound_rti = AsBoundType()->GetUpperBound(); |
| 1781 | DCHECK(upper_bound_rti.IsSupertypeOf(rti)) |
| 1782 | << " upper_bound_rti: " << upper_bound_rti |
| 1783 | << " rti: " << rti; |
David Brazdil | baf89b8 | 2015-09-15 11:36:54 +0100 | [diff] [blame] | 1784 | DCHECK(!upper_bound_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes() || rti.IsExact()); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1785 | } |
| 1786 | } |
| 1787 | reference_type_info_ = rti; |
| 1788 | } |
| 1789 | |
| 1790 | ReferenceTypeInfo::ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {} |
| 1791 | |
| 1792 | ReferenceTypeInfo::ReferenceTypeInfo(TypeHandle type_handle, bool is_exact) |
| 1793 | : type_handle_(type_handle), is_exact_(is_exact) { |
| 1794 | if (kIsDebugBuild) { |
| 1795 | ScopedObjectAccess soa(Thread::Current()); |
| 1796 | DCHECK(IsValidHandle(type_handle)); |
| 1797 | } |
| 1798 | } |
| 1799 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1800 | std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) { |
| 1801 | ScopedObjectAccess soa(Thread::Current()); |
| 1802 | os << "[" |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1803 | << " is_valid=" << rhs.IsValid() |
| 1804 | << " type=" << (!rhs.IsValid() ? "?" : PrettyClass(rhs.GetTypeHandle().Get())) |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1805 | << " is_exact=" << rhs.IsExact() |
| 1806 | << " ]"; |
| 1807 | return os; |
| 1808 | } |
| 1809 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1810 | bool HInstruction::HasAnyEnvironmentUseBefore(HInstruction* other) { |
| 1811 | // For now, assume that instructions in different blocks may use the |
| 1812 | // environment. |
| 1813 | // TODO: Use the control flow to decide if this is true. |
| 1814 | if (GetBlock() != other->GetBlock()) { |
| 1815 | return true; |
| 1816 | } |
| 1817 | |
| 1818 | // We know that we are in the same block. Walk from 'this' to 'other', |
| 1819 | // checking to see if there is any instruction with an environment. |
| 1820 | HInstruction* current = this; |
| 1821 | for (; current != other && current != nullptr; current = current->GetNext()) { |
| 1822 | // This is a conservative check, as the instruction result may not be in |
| 1823 | // the referenced environment. |
| 1824 | if (current->HasEnvironment()) { |
| 1825 | return true; |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | // We should have been called with 'this' before 'other' in the block. |
| 1830 | // Just confirm this. |
| 1831 | DCHECK(current != nullptr); |
| 1832 | return false; |
| 1833 | } |
| 1834 | |
| 1835 | void HInstruction::RemoveEnvironmentUsers() { |
| 1836 | for (HUseIterator<HEnvironment*> use_it(GetEnvUses()); !use_it.Done(); use_it.Advance()) { |
| 1837 | HUseListNode<HEnvironment*>* user_node = use_it.Current(); |
| 1838 | HEnvironment* user = user_node->GetUser(); |
| 1839 | user->SetRawEnvAt(user_node->GetIndex(), nullptr); |
| 1840 | } |
| 1841 | env_uses_.Clear(); |
| 1842 | } |
| 1843 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1844 | } // namespace art |