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