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