blob: f825e50fe1a701e28745bdcc956cd97d024bb03a [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
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 Juravle77520bc2015-01-12 18:45:46 +000018
Mark Mendelle82549b2015-05-06 10:55:34 -040019#include "code_generator.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010020#include "ssa_builder.h"
David Brazdila4b8c212015-05-07 09:59:30 +010021#include "base/bit_vector-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010022#include "base/bit_utils.h"
Vladimir Marko1f8695c2015-09-24 13:11:31 +010023#include "base/stl_util.h"
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010024#include "intrinsics.h"
David Brazdilbaf89b82015-09-15 11:36:54 +010025#include "mirror/class-inl.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026#include "scoped_thread_state_change.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000027
28namespace art {
29
30void HGraph::AddBlock(HBasicBlock* block) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010031 block->SetBlockId(blocks_.size());
32 blocks_.push_back(block);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033}
34
Nicolas Geoffray804d0932014-05-02 08:46:00 +010035void HGraph::FindBackEdges(ArenaBitVector* visited) {
Vladimir Marko1f8695c2015-09-24 13:11:31 +010036 // "visited" must be empty on entry, it's an output argument for all visited (i.e. live) blocks.
37 DCHECK_EQ(visited->GetHighestBitSet(), -1);
38
39 // Nodes that we're currently visiting, indexed by block id.
Vladimir Markofa6b93c2015-09-15 10:15:55 +010040 ArenaBitVector visiting(arena_, blocks_.size(), false);
Vladimir Marko1f8695c2015-09-24 13:11:31 +010041 // Number of successors visited from a given node, indexed by block id.
42 ArenaVector<size_t> successors_visited(blocks_.size(), 0u, arena_->Adapter());
43 // Stack of nodes that we're currently visiting (same as marked in "visiting" above).
44 ArenaVector<HBasicBlock*> worklist(arena_->Adapter());
45 constexpr size_t kDefaultWorklistSize = 8;
46 worklist.reserve(kDefaultWorklistSize);
47 visited->SetBit(entry_block_->GetBlockId());
48 visiting.SetBit(entry_block_->GetBlockId());
49 worklist.push_back(entry_block_);
50
51 while (!worklist.empty()) {
52 HBasicBlock* current = worklist.back();
53 uint32_t current_id = current->GetBlockId();
54 if (successors_visited[current_id] == current->GetSuccessors().size()) {
55 visiting.ClearBit(current_id);
56 worklist.pop_back();
57 } else {
Vladimir Marko1f8695c2015-09-24 13:11:31 +010058 HBasicBlock* successor = current->GetSuccessors()[successors_visited[current_id]++];
59 uint32_t successor_id = successor->GetBlockId();
60 if (visiting.IsBitSet(successor_id)) {
61 DCHECK(ContainsElement(worklist, successor));
62 successor->AddBackEdge(current);
63 } else if (!visited->IsBitSet(successor_id)) {
64 visited->SetBit(successor_id);
65 visiting.SetBit(successor_id);
66 worklist.push_back(successor);
67 }
68 }
69 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000070}
71
Roland Levillainfc600dc2014-12-02 17:16:31 +000072static void RemoveAsUser(HInstruction* instruction) {
73 for (size_t i = 0; i < instruction->InputCount(); i++) {
David Brazdil1abb4192015-02-17 18:33:36 +000074 instruction->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000075 }
76
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010077 for (HEnvironment* environment = instruction->GetEnvironment();
78 environment != nullptr;
79 environment = environment->GetParent()) {
Roland Levillainfc600dc2014-12-02 17:16:31 +000080 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
David Brazdil1abb4192015-02-17 18:33:36 +000081 if (environment->GetInstructionAt(i) != nullptr) {
82 environment->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000083 }
84 }
85 }
86}
87
88void HGraph::RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010089 for (size_t i = 0; i < blocks_.size(); ++i) {
Roland Levillainfc600dc2014-12-02 17:16:31 +000090 if (!visited.IsBitSet(i)) {
Vladimir Markoec7802a2015-10-01 20:57:57 +010091 HBasicBlock* block = blocks_[i];
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010092 DCHECK(block->GetPhis().IsEmpty()) << "Phis are not inserted at this stage";
Roland Levillainfc600dc2014-12-02 17:16:31 +000093 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
94 RemoveAsUser(it.Current());
95 }
96 }
97 }
98}
99
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100100void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100101 for (size_t i = 0; i < blocks_.size(); ++i) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000102 if (!visited.IsBitSet(i)) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100103 HBasicBlock* block = blocks_[i];
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100104 // We only need to update the successor, which might be live.
Vladimir Marko60584552015-09-03 13:35:12 +0000105 for (HBasicBlock* successor : block->GetSuccessors()) {
106 successor->RemovePredecessor(block);
David Brazdil1abb4192015-02-17 18:33:36 +0000107 }
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100108 // Remove the block from the list of blocks, so that further analyses
109 // never see it.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100110 blocks_[i] = nullptr;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000111 }
112 }
113}
114
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000115void HGraph::BuildDominatorTree() {
David Brazdilffee3d32015-07-06 11:48:53 +0100116 // (1) Simplify the CFG so that catch blocks have only exceptional incoming
117 // edges. This invariant simplifies building SSA form because Phis cannot
118 // collect both normal- and exceptional-flow values at the same time.
119 SimplifyCatchBlocks();
120
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100121 ArenaBitVector visited(arena_, blocks_.size(), false);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000122
David Brazdilffee3d32015-07-06 11:48:53 +0100123 // (2) Find the back edges in the graph doing a DFS traversal.
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000124 FindBackEdges(&visited);
125
David Brazdilffee3d32015-07-06 11:48:53 +0100126 // (3) Remove instructions and phis from blocks not visited during
Roland Levillainfc600dc2014-12-02 17:16:31 +0000127 // the initial DFS as users from other instructions, so that
128 // users can be safely removed before uses later.
129 RemoveInstructionsAsUsersFromDeadBlocks(visited);
130
David Brazdilffee3d32015-07-06 11:48:53 +0100131 // (4) Remove blocks not visited during the initial DFS.
Roland Levillainfc600dc2014-12-02 17:16:31 +0000132 // Step (4) requires dead blocks to be removed from the
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000133 // predecessors list of live blocks.
134 RemoveDeadBlocks(visited);
135
David Brazdilffee3d32015-07-06 11:48:53 +0100136 // (5) Simplify the CFG now, so that we don't need to recompute
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100137 // dominators and the reverse post order.
138 SimplifyCFG();
139
David Brazdilffee3d32015-07-06 11:48:53 +0100140 // (6) Compute the dominance information and the reverse post order.
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100141 ComputeDominanceInformation();
142}
143
144void HGraph::ClearDominanceInformation() {
145 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
146 it.Current()->ClearDominanceInformation();
147 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100148 reverse_post_order_.clear();
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100149}
150
151void HBasicBlock::ClearDominanceInformation() {
Vladimir Marko60584552015-09-03 13:35:12 +0000152 dominated_blocks_.clear();
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100153 dominator_ = nullptr;
154}
155
156void HGraph::ComputeDominanceInformation() {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100157 DCHECK(reverse_post_order_.empty());
158 reverse_post_order_.reserve(blocks_.size());
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100159 reverse_post_order_.push_back(entry_block_);
Vladimir Markod76d1392015-09-23 16:07:14 +0100160
161 // Number of visits of a given node, indexed by block id.
162 ArenaVector<size_t> visits(blocks_.size(), 0u, arena_->Adapter());
163 // Number of successors visited from a given node, indexed by block id.
164 ArenaVector<size_t> successors_visited(blocks_.size(), 0u, arena_->Adapter());
165 // Nodes for which we need to visit successors.
166 ArenaVector<HBasicBlock*> worklist(arena_->Adapter());
167 constexpr size_t kDefaultWorklistSize = 8;
168 worklist.reserve(kDefaultWorklistSize);
169 worklist.push_back(entry_block_);
170
171 while (!worklist.empty()) {
172 HBasicBlock* current = worklist.back();
173 uint32_t current_id = current->GetBlockId();
174 if (successors_visited[current_id] == current->GetSuccessors().size()) {
175 worklist.pop_back();
176 } else {
Vladimir Markod76d1392015-09-23 16:07:14 +0100177 HBasicBlock* successor = current->GetSuccessors()[successors_visited[current_id]++];
178
179 if (successor->GetDominator() == nullptr) {
180 successor->SetDominator(current);
181 } else {
182 successor->SetDominator(FindCommonDominator(successor->GetDominator(), current));
183 }
184
185 // Once all the forward edges have been visited, we know the immediate
186 // dominator of the block. We can then start visiting its successors.
Vladimir Markod76d1392015-09-23 16:07:14 +0100187 if (++visits[successor->GetBlockId()] ==
188 successor->GetPredecessors().size() - successor->NumberOfBackEdges()) {
189 successor->GetDominator()->AddDominatedBlock(successor);
190 reverse_post_order_.push_back(successor);
191 worklist.push_back(successor);
192 }
193 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000194 }
195}
196
197HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100198 ArenaBitVector visited(arena_, blocks_.size(), false);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000199 // Walk the dominator tree of the first block and mark the visited blocks.
200 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000201 visited.SetBit(first->GetBlockId());
202 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000203 }
204 // Walk the dominator tree of the second block until a marked block is found.
205 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000206 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000207 return second;
208 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000209 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000210 }
211 LOG(ERROR) << "Could not find common dominator";
212 return nullptr;
213}
214
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215void HGraph::TransformToSsa() {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100216 DCHECK(!reverse_post_order_.empty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100217 SsaBuilder ssa_builder(this);
218 ssa_builder.BuildSsa();
219}
220
David Brazdilfc6a86a2015-06-26 10:33:45 +0000221HBasicBlock* HGraph::SplitEdge(HBasicBlock* block, HBasicBlock* successor) {
David Brazdil3e187382015-06-26 09:59:52 +0000222 HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc());
223 AddBlock(new_block);
David Brazdil3e187382015-06-26 09:59:52 +0000224 // Use `InsertBetween` to ensure the predecessor index and successor index of
225 // `block` and `successor` are preserved.
226 new_block->InsertBetween(block, successor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000227 return new_block;
228}
229
230void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) {
231 // Insert a new node between `block` and `successor` to split the
232 // critical edge.
233 HBasicBlock* new_block = SplitEdge(block, successor);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600234 new_block->AddInstruction(new (arena_) HGoto(successor->GetDexPc()));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100235 if (successor->IsLoopHeader()) {
236 // If we split at a back edge boundary, make the new block the back edge.
237 HLoopInformation* info = successor->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000238 if (info->IsBackEdge(*block)) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100239 info->RemoveBackEdge(block);
240 info->AddBackEdge(new_block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100241 }
242 }
243}
244
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100245void HGraph::SimplifyLoop(HBasicBlock* header) {
246 HLoopInformation* info = header->GetLoopInformation();
247
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100248 // Make sure the loop has only one pre header. This simplifies SSA building by having
249 // to just look at the pre header to know which locals are initialized at entry of the
250 // loop.
Vladimir Marko60584552015-09-03 13:35:12 +0000251 size_t number_of_incomings = header->GetPredecessors().size() - info->NumberOfBackEdges();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100252 if (number_of_incomings != 1) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100253 HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100254 AddBlock(pre_header);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600255 pre_header->AddInstruction(new (arena_) HGoto(header->GetDexPc()));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100256
Vladimir Marko60584552015-09-03 13:35:12 +0000257 for (size_t pred = 0; pred < header->GetPredecessors().size(); ++pred) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100258 HBasicBlock* predecessor = header->GetPredecessors()[pred];
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100259 if (!info->IsBackEdge(*predecessor)) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100260 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100261 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100262 }
263 }
264 pre_header->AddSuccessor(header);
265 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100266
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100267 // Make sure the first predecessor of a loop header is the incoming block.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100268 if (info->IsBackEdge(*header->GetPredecessors()[0])) {
269 HBasicBlock* to_swap = header->GetPredecessors()[0];
Vladimir Marko60584552015-09-03 13:35:12 +0000270 for (size_t pred = 1, e = header->GetPredecessors().size(); pred < e; ++pred) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100271 HBasicBlock* predecessor = header->GetPredecessors()[pred];
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100272 if (!info->IsBackEdge(*predecessor)) {
Vladimir Marko60584552015-09-03 13:35:12 +0000273 header->predecessors_[pred] = to_swap;
274 header->predecessors_[0] = predecessor;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100275 break;
276 }
277 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100278 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100279
280 // Place the suspend check at the beginning of the header, so that live registers
281 // will be known when allocating registers. Note that code generation can still
282 // generate the suspend check at the back edge, but needs to be careful with
283 // loop phi spill slots (which are not written to at back edge).
284 HInstruction* first_instruction = header->GetFirstInstruction();
285 if (!first_instruction->IsSuspendCheck()) {
286 HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc());
287 header->InsertInstructionBefore(check, first_instruction);
288 first_instruction = check;
289 }
290 info->SetSuspendCheck(first_instruction->AsSuspendCheck());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100291}
292
David Brazdilffee3d32015-07-06 11:48:53 +0100293static bool CheckIfPredecessorAtIsExceptional(const HBasicBlock& block, size_t pred_idx) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100294 HBasicBlock* predecessor = block.GetPredecessors()[pred_idx];
David Brazdilffee3d32015-07-06 11:48:53 +0100295 if (!predecessor->EndsWithTryBoundary()) {
296 // Only edges from HTryBoundary can be exceptional.
297 return false;
298 }
299 HTryBoundary* try_boundary = predecessor->GetLastInstruction()->AsTryBoundary();
300 if (try_boundary->GetNormalFlowSuccessor() == &block) {
301 // This block is the normal-flow successor of `try_boundary`, but it could
302 // also be one of its exception handlers if catch blocks have not been
303 // simplified yet. Predecessors are unordered, so we will consider the first
304 // occurrence to be the normal edge and a possible second occurrence to be
305 // the exceptional edge.
306 return !block.IsFirstIndexOfPredecessor(predecessor, pred_idx);
307 } else {
308 // This is not the normal-flow successor of `try_boundary`, hence it must be
309 // one of its exception handlers.
310 DCHECK(try_boundary->HasExceptionHandler(block));
311 return true;
312 }
313}
314
315void HGraph::SimplifyCatchBlocks() {
Vladimir Markob7d8e8c2015-09-17 15:47:05 +0100316 // NOTE: We're appending new blocks inside the loop, so we need to use index because iterators
317 // can be invalidated. We remember the initial size to avoid iterating over the new blocks.
318 for (size_t block_id = 0u, end = blocks_.size(); block_id != end; ++block_id) {
319 HBasicBlock* catch_block = blocks_[block_id];
David Brazdilffee3d32015-07-06 11:48:53 +0100320 if (!catch_block->IsCatchBlock()) {
321 continue;
322 }
323
324 bool exceptional_predecessors_only = true;
Vladimir Marko60584552015-09-03 13:35:12 +0000325 for (size_t j = 0; j < catch_block->GetPredecessors().size(); ++j) {
David Brazdilffee3d32015-07-06 11:48:53 +0100326 if (!CheckIfPredecessorAtIsExceptional(*catch_block, j)) {
327 exceptional_predecessors_only = false;
328 break;
329 }
330 }
331
332 if (!exceptional_predecessors_only) {
333 // Catch block has normal-flow predecessors and needs to be simplified.
334 // Splitting the block before its first instruction moves all its
335 // instructions into `normal_block` and links the two blocks with a Goto.
336 // Afterwards, incoming normal-flow edges are re-linked to `normal_block`,
337 // leaving `catch_block` with the exceptional edges only.
338 // Note that catch blocks with normal-flow predecessors cannot begin with
339 // a MOVE_EXCEPTION instruction, as guaranteed by the verifier.
340 DCHECK(!catch_block->GetFirstInstruction()->IsLoadException());
341 HBasicBlock* normal_block = catch_block->SplitBefore(catch_block->GetFirstInstruction());
Vladimir Marko60584552015-09-03 13:35:12 +0000342 for (size_t j = 0; j < catch_block->GetPredecessors().size(); ++j) {
David Brazdilffee3d32015-07-06 11:48:53 +0100343 if (!CheckIfPredecessorAtIsExceptional(*catch_block, j)) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100344 catch_block->GetPredecessors()[j]->ReplaceSuccessor(catch_block, normal_block);
David Brazdilffee3d32015-07-06 11:48:53 +0100345 --j;
346 }
347 }
348 }
349 }
350}
351
352void HGraph::ComputeTryBlockInformation() {
353 // Iterate in reverse post order to propagate try membership information from
354 // predecessors to their successors.
355 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
356 HBasicBlock* block = it.Current();
357 if (block->IsEntryBlock() || block->IsCatchBlock()) {
358 // Catch blocks after simplification have only exceptional predecessors
359 // and hence are never in tries.
360 continue;
361 }
362
363 // Infer try membership from the first predecessor. Having simplified loops,
364 // the first predecessor can never be a back edge and therefore it must have
365 // been visited already and had its try membership set.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100366 HBasicBlock* first_predecessor = block->GetPredecessors()[0];
David Brazdilffee3d32015-07-06 11:48:53 +0100367 DCHECK(!block->IsLoopHeader() || !block->GetLoopInformation()->IsBackEdge(*first_predecessor));
David Brazdilec16f792015-08-19 15:04:01 +0100368 const HTryBoundary* try_entry = first_predecessor->ComputeTryEntryOfSuccessors();
369 if (try_entry != nullptr) {
370 block->SetTryCatchInformation(new (arena_) TryCatchInformation(*try_entry));
371 }
David Brazdilffee3d32015-07-06 11:48:53 +0100372 }
373}
374
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100375void HGraph::SimplifyCFG() {
376 // Simplify the CFG for future analysis, and code generation:
377 // (1): Split critical edges.
378 // (2): Simplify loops by having only one back edge, and one preheader.
Vladimir Markob7d8e8c2015-09-17 15:47:05 +0100379 // NOTE: We're appending new blocks inside the loop, so we need to use index because iterators
380 // can be invalidated. We remember the initial size to avoid iterating over the new blocks.
381 for (size_t block_id = 0u, end = blocks_.size(); block_id != end; ++block_id) {
382 HBasicBlock* block = blocks_[block_id];
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100383 if (block == nullptr) continue;
David Brazdilffee3d32015-07-06 11:48:53 +0100384 if (block->NumberOfNormalSuccessors() > 1) {
Vladimir Marko60584552015-09-03 13:35:12 +0000385 for (size_t j = 0; j < block->GetSuccessors().size(); ++j) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100386 HBasicBlock* successor = block->GetSuccessors()[j];
David Brazdilffee3d32015-07-06 11:48:53 +0100387 DCHECK(!successor->IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +0000388 if (successor->GetPredecessors().size() > 1) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100389 SplitCriticalEdge(block, successor);
390 --j;
391 }
392 }
393 }
394 if (block->IsLoopHeader()) {
395 SimplifyLoop(block);
396 }
397 }
398}
399
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000400bool HGraph::AnalyzeNaturalLoops() const {
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100401 // Order does not matter.
402 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
403 HBasicBlock* block = it.Current();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100404 if (block->IsLoopHeader()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100405 if (block->IsCatchBlock()) {
406 // TODO: Dealing with exceptional back edges could be tricky because
407 // they only approximate the real control flow. Bail out for now.
408 return false;
409 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100410 HLoopInformation* info = block->GetLoopInformation();
411 if (!info->Populate()) {
412 // Abort if the loop is non natural. We currently bailout in such cases.
413 return false;
414 }
415 }
416 }
417 return true;
418}
419
David Brazdil8d5b8b22015-03-24 10:51:52 +0000420void HGraph::InsertConstant(HConstant* constant) {
421 // New constants are inserted before the final control-flow instruction
422 // of the graph, or at its end if called from the graph builder.
423 if (entry_block_->EndsWithControlFlowInstruction()) {
424 entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction());
David Brazdil46e2a392015-03-16 17:31:52 +0000425 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000426 entry_block_->AddInstruction(constant);
David Brazdil46e2a392015-03-16 17:31:52 +0000427 }
428}
429
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600430HNullConstant* HGraph::GetNullConstant(uint32_t dex_pc) {
Nicolas Geoffray18e68732015-06-17 23:09:05 +0100431 // For simplicity, don't bother reviving the cached null constant if it is
432 // not null and not in a block. Otherwise, we need to clear the instruction
433 // id and/or any invariants the graph is assuming when adding new instructions.
434 if ((cached_null_constant_ == nullptr) || (cached_null_constant_->GetBlock() == nullptr)) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600435 cached_null_constant_ = new (arena_) HNullConstant(dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000436 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000437 }
438 return cached_null_constant_;
439}
440
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100441HCurrentMethod* HGraph::GetCurrentMethod() {
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100442 // For simplicity, don't bother reviving the cached current method if it is
443 // not null and not in a block. Otherwise, we need to clear the instruction
444 // id and/or any invariants the graph is assuming when adding new instructions.
445 if ((cached_current_method_ == nullptr) || (cached_current_method_->GetBlock() == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700446 cached_current_method_ = new (arena_) HCurrentMethod(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600447 Is64BitInstructionSet(instruction_set_) ? Primitive::kPrimLong : Primitive::kPrimInt,
448 entry_block_->GetDexPc());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100449 if (entry_block_->GetFirstInstruction() == nullptr) {
450 entry_block_->AddInstruction(cached_current_method_);
451 } else {
452 entry_block_->InsertInstructionBefore(
453 cached_current_method_, entry_block_->GetFirstInstruction());
454 }
455 }
456 return cached_current_method_;
457}
458
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600459HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000460 switch (type) {
461 case Primitive::Type::kPrimBoolean:
462 DCHECK(IsUint<1>(value));
463 FALLTHROUGH_INTENDED;
464 case Primitive::Type::kPrimByte:
465 case Primitive::Type::kPrimChar:
466 case Primitive::Type::kPrimShort:
467 case Primitive::Type::kPrimInt:
468 DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600469 return GetIntConstant(static_cast<int32_t>(value), dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000470
471 case Primitive::Type::kPrimLong:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600472 return GetLongConstant(value, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000473
474 default:
475 LOG(FATAL) << "Unsupported constant type";
476 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000477 }
David Brazdil46e2a392015-03-16 17:31:52 +0000478}
479
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000480void HGraph::CacheFloatConstant(HFloatConstant* constant) {
481 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
482 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
483 cached_float_constants_.Overwrite(value, constant);
484}
485
486void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
487 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
488 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
489 cached_double_constants_.Overwrite(value, constant);
490}
491
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000492void HLoopInformation::Add(HBasicBlock* block) {
493 blocks_.SetBit(block->GetBlockId());
494}
495
David Brazdil46e2a392015-03-16 17:31:52 +0000496void HLoopInformation::Remove(HBasicBlock* block) {
497 blocks_.ClearBit(block->GetBlockId());
498}
499
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100500void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
501 if (blocks_.IsBitSet(block->GetBlockId())) {
502 return;
503 }
504
505 blocks_.SetBit(block->GetBlockId());
506 block->SetInLoop(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000507 for (HBasicBlock* predecessor : block->GetPredecessors()) {
508 PopulateRecursive(predecessor);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100509 }
510}
511
512bool HLoopInformation::Populate() {
David Brazdila4b8c212015-05-07 09:59:30 +0100513 DCHECK_EQ(blocks_.NumSetBits(), 0u) << "Loop information has already been populated";
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100514 for (HBasicBlock* back_edge : GetBackEdges()) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100515 DCHECK(back_edge->GetDominator() != nullptr);
516 if (!header_->Dominates(back_edge)) {
517 // This loop is not natural. Do not bother going further.
518 return false;
519 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100520
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100521 // Populate this loop: starting with the back edge, recursively add predecessors
522 // that are not already part of that loop. Set the header as part of the loop
523 // to end the recursion.
524 // This is a recursive implementation of the algorithm described in
525 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
526 blocks_.SetBit(header_->GetBlockId());
527 PopulateRecursive(back_edge);
528 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100529 return true;
530}
531
David Brazdila4b8c212015-05-07 09:59:30 +0100532void HLoopInformation::Update() {
533 HGraph* graph = header_->GetGraph();
534 for (uint32_t id : blocks_.Indexes()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100535 HBasicBlock* block = graph->GetBlocks()[id];
David Brazdila4b8c212015-05-07 09:59:30 +0100536 // Reset loop information of non-header blocks inside the loop, except
537 // members of inner nested loops because those should already have been
538 // updated by their own LoopInformation.
539 if (block->GetLoopInformation() == this && block != header_) {
540 block->SetLoopInformation(nullptr);
541 }
542 }
543 blocks_.ClearAllBits();
544
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100545 if (back_edges_.empty()) {
David Brazdila4b8c212015-05-07 09:59:30 +0100546 // The loop has been dismantled, delete its suspend check and remove info
547 // from the header.
548 DCHECK(HasSuspendCheck());
549 header_->RemoveInstruction(suspend_check_);
550 header_->SetLoopInformation(nullptr);
551 header_ = nullptr;
552 suspend_check_ = nullptr;
553 } else {
554 if (kIsDebugBuild) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100555 for (HBasicBlock* back_edge : back_edges_) {
556 DCHECK(header_->Dominates(back_edge));
David Brazdila4b8c212015-05-07 09:59:30 +0100557 }
558 }
559 // This loop still has reachable back edges. Repopulate the list of blocks.
560 bool populate_successful = Populate();
561 DCHECK(populate_successful);
562 }
563}
564
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100565HBasicBlock* HLoopInformation::GetPreHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100566 return header_->GetDominator();
567}
568
569bool HLoopInformation::Contains(const HBasicBlock& block) const {
570 return blocks_.IsBitSet(block.GetBlockId());
571}
572
573bool HLoopInformation::IsIn(const HLoopInformation& other) const {
574 return other.blocks_.IsBitSet(header_->GetBlockId());
575}
576
Aart Bik73f1f3b2015-10-28 15:28:08 -0700577bool HLoopInformation::IsLoopInvariant(HInstruction* instruction, bool must_dominate) const {
578 HLoopInformation* other_loop = instruction->GetBlock()->GetLoopInformation();
579 if (other_loop != this && (other_loop == nullptr || !other_loop->IsIn(*this))) {
580 if (must_dominate) {
581 return instruction->GetBlock()->Dominates(GetHeader());
582 }
583 return true;
584 }
585 return false;
586}
587
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100588size_t HLoopInformation::GetLifetimeEnd() const {
589 size_t last_position = 0;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100590 for (HBasicBlock* back_edge : GetBackEdges()) {
591 last_position = std::max(back_edge->GetLifetimeEnd(), last_position);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100592 }
593 return last_position;
594}
595
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100596bool HBasicBlock::Dominates(HBasicBlock* other) const {
597 // Walk up the dominator tree from `other`, to find out if `this`
598 // is an ancestor.
599 HBasicBlock* current = other;
600 while (current != nullptr) {
601 if (current == this) {
602 return true;
603 }
604 current = current->GetDominator();
605 }
606 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100607}
608
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100609static void UpdateInputsUsers(HInstruction* instruction) {
610 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
611 instruction->InputAt(i)->AddUseAt(instruction, i);
612 }
613 // Environment should be created later.
614 DCHECK(!instruction->HasEnvironment());
615}
616
Roland Levillainccc07a92014-09-16 14:48:16 +0100617void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
618 HInstruction* replacement) {
619 DCHECK(initial->GetBlock() == this);
Mark Mendell805b3b52015-09-18 14:10:29 -0400620 if (initial->IsControlFlow()) {
621 // We can only replace a control flow instruction with another control flow instruction.
622 DCHECK(replacement->IsControlFlow());
623 DCHECK_EQ(replacement->GetId(), -1);
624 DCHECK_EQ(replacement->GetType(), Primitive::kPrimVoid);
625 DCHECK_EQ(initial->GetBlock(), this);
626 DCHECK_EQ(initial->GetType(), Primitive::kPrimVoid);
627 DCHECK(initial->GetUses().IsEmpty());
628 DCHECK(initial->GetEnvUses().IsEmpty());
629 replacement->SetBlock(this);
630 replacement->SetId(GetGraph()->GetNextInstructionId());
631 instructions_.InsertInstructionBefore(replacement, initial);
632 UpdateInputsUsers(replacement);
633 } else {
634 InsertInstructionBefore(replacement, initial);
635 initial->ReplaceWith(replacement);
636 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100637 RemoveInstruction(initial);
638}
639
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100640static void Add(HInstructionList* instruction_list,
641 HBasicBlock* block,
642 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000643 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000644 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100645 instruction->SetBlock(block);
646 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100647 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100648 instruction_list->AddInstruction(instruction);
649}
650
651void HBasicBlock::AddInstruction(HInstruction* instruction) {
652 Add(&instructions_, this, instruction);
653}
654
655void HBasicBlock::AddPhi(HPhi* phi) {
656 Add(&phis_, this, phi);
657}
658
David Brazdilc3d743f2015-04-22 13:40:50 +0100659void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
660 DCHECK(!cursor->IsPhi());
661 DCHECK(!instruction->IsPhi());
662 DCHECK_EQ(instruction->GetId(), -1);
663 DCHECK_NE(cursor->GetId(), -1);
664 DCHECK_EQ(cursor->GetBlock(), this);
665 DCHECK(!instruction->IsControlFlow());
666 instruction->SetBlock(this);
667 instruction->SetId(GetGraph()->GetNextInstructionId());
668 UpdateInputsUsers(instruction);
669 instructions_.InsertInstructionBefore(instruction, cursor);
670}
671
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100672void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
673 DCHECK(!cursor->IsPhi());
674 DCHECK(!instruction->IsPhi());
675 DCHECK_EQ(instruction->GetId(), -1);
676 DCHECK_NE(cursor->GetId(), -1);
677 DCHECK_EQ(cursor->GetBlock(), this);
678 DCHECK(!instruction->IsControlFlow());
679 DCHECK(!cursor->IsControlFlow());
680 instruction->SetBlock(this);
681 instruction->SetId(GetGraph()->GetNextInstructionId());
682 UpdateInputsUsers(instruction);
683 instructions_.InsertInstructionAfter(instruction, cursor);
684}
685
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100686void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
687 DCHECK_EQ(phi->GetId(), -1);
688 DCHECK_NE(cursor->GetId(), -1);
689 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100690 phi->SetBlock(this);
691 phi->SetId(GetGraph()->GetNextInstructionId());
692 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100693 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100694}
695
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100696static void Remove(HInstructionList* instruction_list,
697 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000698 HInstruction* instruction,
699 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100700 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100701 instruction->SetBlock(nullptr);
702 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000703 if (ensure_safety) {
704 DCHECK(instruction->GetUses().IsEmpty());
705 DCHECK(instruction->GetEnvUses().IsEmpty());
706 RemoveAsUser(instruction);
707 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100708}
709
David Brazdil1abb4192015-02-17 18:33:36 +0000710void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100711 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000712 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100713}
714
David Brazdil1abb4192015-02-17 18:33:36 +0000715void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
716 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100717}
718
David Brazdilc7508e92015-04-27 13:28:57 +0100719void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
720 if (instruction->IsPhi()) {
721 RemovePhi(instruction->AsPhi(), ensure_safety);
722 } else {
723 RemoveInstruction(instruction, ensure_safety);
724 }
725}
726
Vladimir Marko71bf8092015-09-15 15:33:14 +0100727void HEnvironment::CopyFrom(const ArenaVector<HInstruction*>& locals) {
728 for (size_t i = 0; i < locals.size(); i++) {
729 HInstruction* instruction = locals[i];
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +0100730 SetRawEnvAt(i, instruction);
731 if (instruction != nullptr) {
732 instruction->AddEnvUseAt(this, i);
733 }
734 }
735}
736
David Brazdiled596192015-01-23 10:39:45 +0000737void HEnvironment::CopyFrom(HEnvironment* env) {
738 for (size_t i = 0; i < env->Size(); i++) {
739 HInstruction* instruction = env->GetInstructionAt(i);
740 SetRawEnvAt(i, instruction);
741 if (instruction != nullptr) {
742 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100743 }
David Brazdiled596192015-01-23 10:39:45 +0000744 }
745}
746
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700747void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
748 HBasicBlock* loop_header) {
749 DCHECK(loop_header->IsLoopHeader());
750 for (size_t i = 0; i < env->Size(); i++) {
751 HInstruction* instruction = env->GetInstructionAt(i);
752 SetRawEnvAt(i, instruction);
753 if (instruction == nullptr) {
754 continue;
755 }
756 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
757 // At the end of the loop pre-header, the corresponding value for instruction
758 // is the first input of the phi.
759 HInstruction* initial = instruction->AsPhi()->InputAt(0);
760 DCHECK(initial->GetBlock()->Dominates(loop_header));
761 SetRawEnvAt(i, initial);
762 initial->AddEnvUseAt(this, i);
763 } else {
764 instruction->AddEnvUseAt(this, i);
765 }
766 }
767}
768
David Brazdil1abb4192015-02-17 18:33:36 +0000769void HEnvironment::RemoveAsUserOfInput(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100770 const HUserRecord<HEnvironment*>& user_record = vregs_[index];
David Brazdil1abb4192015-02-17 18:33:36 +0000771 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100772}
773
Calin Juravle77520bc2015-01-12 18:45:46 +0000774HInstruction* HInstruction::GetNextDisregardingMoves() const {
775 HInstruction* next = GetNext();
776 while (next != nullptr && next->IsParallelMove()) {
777 next = next->GetNext();
778 }
779 return next;
780}
781
782HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
783 HInstruction* previous = GetPrevious();
784 while (previous != nullptr && previous->IsParallelMove()) {
785 previous = previous->GetPrevious();
786 }
787 return previous;
788}
789
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100790void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000791 if (first_instruction_ == nullptr) {
792 DCHECK(last_instruction_ == nullptr);
793 first_instruction_ = last_instruction_ = instruction;
794 } else {
795 last_instruction_->next_ = instruction;
796 instruction->previous_ = last_instruction_;
797 last_instruction_ = instruction;
798 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000799}
800
David Brazdilc3d743f2015-04-22 13:40:50 +0100801void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
802 DCHECK(Contains(cursor));
803 if (cursor == first_instruction_) {
804 cursor->previous_ = instruction;
805 instruction->next_ = cursor;
806 first_instruction_ = instruction;
807 } else {
808 instruction->previous_ = cursor->previous_;
809 instruction->next_ = cursor;
810 cursor->previous_ = instruction;
811 instruction->previous_->next_ = instruction;
812 }
813}
814
815void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
816 DCHECK(Contains(cursor));
817 if (cursor == last_instruction_) {
818 cursor->next_ = instruction;
819 instruction->previous_ = cursor;
820 last_instruction_ = instruction;
821 } else {
822 instruction->next_ = cursor->next_;
823 instruction->previous_ = cursor;
824 cursor->next_ = instruction;
825 instruction->next_->previous_ = instruction;
826 }
827}
828
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100829void HInstructionList::RemoveInstruction(HInstruction* instruction) {
830 if (instruction->previous_ != nullptr) {
831 instruction->previous_->next_ = instruction->next_;
832 }
833 if (instruction->next_ != nullptr) {
834 instruction->next_->previous_ = instruction->previous_;
835 }
836 if (instruction == first_instruction_) {
837 first_instruction_ = instruction->next_;
838 }
839 if (instruction == last_instruction_) {
840 last_instruction_ = instruction->previous_;
841 }
842}
843
Roland Levillain6b469232014-09-25 10:10:38 +0100844bool HInstructionList::Contains(HInstruction* instruction) const {
845 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
846 if (it.Current() == instruction) {
847 return true;
848 }
849 }
850 return false;
851}
852
Roland Levillainccc07a92014-09-16 14:48:16 +0100853bool HInstructionList::FoundBefore(const HInstruction* instruction1,
854 const HInstruction* instruction2) const {
855 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
856 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
857 if (it.Current() == instruction1) {
858 return true;
859 }
860 if (it.Current() == instruction2) {
861 return false;
862 }
863 }
864 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
865 return true;
866}
867
Roland Levillain6c82d402014-10-13 16:10:27 +0100868bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
869 if (other_instruction == this) {
870 // An instruction does not strictly dominate itself.
871 return false;
872 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100873 HBasicBlock* block = GetBlock();
874 HBasicBlock* other_block = other_instruction->GetBlock();
875 if (block != other_block) {
876 return GetBlock()->Dominates(other_instruction->GetBlock());
877 } else {
878 // If both instructions are in the same block, ensure this
879 // instruction comes before `other_instruction`.
880 if (IsPhi()) {
881 if (!other_instruction->IsPhi()) {
882 // Phis appear before non phi-instructions so this instruction
883 // dominates `other_instruction`.
884 return true;
885 } else {
886 // There is no order among phis.
887 LOG(FATAL) << "There is no dominance between phis of a same block.";
888 return false;
889 }
890 } else {
891 // `this` is not a phi.
892 if (other_instruction->IsPhi()) {
893 // Phis appear before non phi-instructions so this instruction
894 // does not dominate `other_instruction`.
895 return false;
896 } else {
897 // Check whether this instruction comes before
898 // `other_instruction` in the instruction list.
899 return block->GetInstructions().FoundBefore(this, other_instruction);
900 }
901 }
902 }
903}
904
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100905void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100906 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000907 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
908 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100909 HInstruction* user = current->GetUser();
910 size_t input_index = current->GetIndex();
911 user->SetRawInputAt(input_index, other);
912 other->AddUseAt(user, input_index);
913 }
914
David Brazdiled596192015-01-23 10:39:45 +0000915 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
916 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100917 HEnvironment* user = current->GetUser();
918 size_t input_index = current->GetIndex();
919 user->SetRawEnvAt(input_index, other);
920 other->AddEnvUseAt(user, input_index);
921 }
922
David Brazdiled596192015-01-23 10:39:45 +0000923 uses_.Clear();
924 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100925}
926
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100927void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000928 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100929 SetRawInputAt(index, replacement);
930 replacement->AddUseAt(this, index);
931}
932
Nicolas Geoffray39468442014-09-02 15:17:15 +0100933size_t HInstruction::EnvironmentSize() const {
934 return HasEnvironment() ? environment_->Size() : 0;
935}
936
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100937void HPhi::AddInput(HInstruction* input) {
938 DCHECK(input->GetBlock() != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100939 inputs_.push_back(HUserRecord<HInstruction*>(input));
940 input->AddUseAt(this, inputs_.size() - 1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100941}
942
David Brazdil2d7352b2015-04-20 14:52:42 +0100943void HPhi::RemoveInputAt(size_t index) {
944 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100945 inputs_.erase(inputs_.begin() + index);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100946 for (size_t i = index, e = InputCount(); i < e; ++i) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100947 DCHECK_EQ(InputRecordAt(i).GetUseNode()->GetIndex(), i + 1u);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100948 InputRecordAt(i).GetUseNode()->SetIndex(i);
949 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100950}
951
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100952#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000953void H##name::Accept(HGraphVisitor* visitor) { \
954 visitor->Visit##name(this); \
955}
956
957FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
958
959#undef DEFINE_ACCEPT
960
961void HGraphVisitor::VisitInsertionOrder() {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100962 const ArenaVector<HBasicBlock*>& blocks = graph_->GetBlocks();
963 for (HBasicBlock* block : blocks) {
David Brazdil46e2a392015-03-16 17:31:52 +0000964 if (block != nullptr) {
965 VisitBasicBlock(block);
966 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967 }
968}
969
Roland Levillain633021e2014-10-01 14:12:25 +0100970void HGraphVisitor::VisitReversePostOrder() {
971 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
972 VisitBasicBlock(it.Current());
973 }
974}
975
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000976void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100977 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100978 it.Current()->Accept(this);
979 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100980 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000981 it.Current()->Accept(this);
982 }
983}
984
Mark Mendelle82549b2015-05-06 10:55:34 -0400985HConstant* HTypeConversion::TryStaticEvaluation() const {
986 HGraph* graph = GetBlock()->GetGraph();
987 if (GetInput()->IsIntConstant()) {
988 int32_t value = GetInput()->AsIntConstant()->GetValue();
989 switch (GetResultType()) {
990 case Primitive::kPrimLong:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600991 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -0400992 case Primitive::kPrimFloat:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600993 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -0400994 case Primitive::kPrimDouble:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600995 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -0400996 default:
997 return nullptr;
998 }
999 } else if (GetInput()->IsLongConstant()) {
1000 int64_t value = GetInput()->AsLongConstant()->GetValue();
1001 switch (GetResultType()) {
1002 case Primitive::kPrimInt:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001003 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001004 case Primitive::kPrimFloat:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001005 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001006 case Primitive::kPrimDouble:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001007 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001008 default:
1009 return nullptr;
1010 }
1011 } else if (GetInput()->IsFloatConstant()) {
1012 float value = GetInput()->AsFloatConstant()->GetValue();
1013 switch (GetResultType()) {
1014 case Primitive::kPrimInt:
1015 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001016 return graph->GetIntConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001017 if (value >= kPrimIntMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001018 return graph->GetIntConstant(kPrimIntMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001019 if (value <= kPrimIntMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001020 return graph->GetIntConstant(kPrimIntMin, GetDexPc());
1021 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001022 case Primitive::kPrimLong:
1023 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001024 return graph->GetLongConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001025 if (value >= kPrimLongMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001026 return graph->GetLongConstant(kPrimLongMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001027 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001028 return graph->GetLongConstant(kPrimLongMin, GetDexPc());
1029 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001030 case Primitive::kPrimDouble:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001031 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001032 default:
1033 return nullptr;
1034 }
1035 } else if (GetInput()->IsDoubleConstant()) {
1036 double value = GetInput()->AsDoubleConstant()->GetValue();
1037 switch (GetResultType()) {
1038 case Primitive::kPrimInt:
1039 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001040 return graph->GetIntConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001041 if (value >= kPrimIntMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001042 return graph->GetIntConstant(kPrimIntMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001043 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001044 return graph->GetIntConstant(kPrimIntMin, GetDexPc());
1045 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001046 case Primitive::kPrimLong:
1047 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001048 return graph->GetLongConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001049 if (value >= kPrimLongMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001050 return graph->GetLongConstant(kPrimLongMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001051 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001052 return graph->GetLongConstant(kPrimLongMin, GetDexPc());
1053 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001054 case Primitive::kPrimFloat:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001055 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001056 default:
1057 return nullptr;
1058 }
1059 }
1060 return nullptr;
1061}
1062
Roland Levillain9240d6a2014-10-20 16:47:04 +01001063HConstant* HUnaryOperation::TryStaticEvaluation() const {
1064 if (GetInput()->IsIntConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001065 return Evaluate(GetInput()->AsIntConstant());
Roland Levillain9240d6a2014-10-20 16:47:04 +01001066 } else if (GetInput()->IsLongConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001067 return Evaluate(GetInput()->AsLongConstant());
Roland Levillain9240d6a2014-10-20 16:47:04 +01001068 }
1069 return nullptr;
1070}
1071
1072HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain9867bc72015-08-05 10:21:34 +01001073 if (GetLeft()->IsIntConstant()) {
1074 if (GetRight()->IsIntConstant()) {
1075 return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsIntConstant());
1076 } else if (GetRight()->IsLongConstant()) {
1077 return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsLongConstant());
1078 }
1079 } else if (GetLeft()->IsLongConstant()) {
1080 if (GetRight()->IsIntConstant()) {
1081 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsIntConstant());
1082 } else if (GetRight()->IsLongConstant()) {
1083 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsLongConstant());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +00001084 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001085 }
1086 return nullptr;
1087}
Dave Allison20dfc792014-06-16 20:44:29 -07001088
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001089HConstant* HBinaryOperation::GetConstantRight() const {
1090 if (GetRight()->IsConstant()) {
1091 return GetRight()->AsConstant();
1092 } else if (IsCommutative() && GetLeft()->IsConstant()) {
1093 return GetLeft()->AsConstant();
1094 } else {
1095 return nullptr;
1096 }
1097}
1098
1099// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001100// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001101HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
1102 HInstruction* most_constant_right = GetConstantRight();
1103 if (most_constant_right == nullptr) {
1104 return nullptr;
1105 } else if (most_constant_right == GetLeft()) {
1106 return GetRight();
1107 } else {
1108 return GetLeft();
1109 }
1110}
1111
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001112bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
1113 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001114}
1115
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001116bool HInstruction::Equals(HInstruction* other) const {
1117 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001118 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001119 if (!InstructionDataEquals(other)) return false;
1120 if (GetType() != other->GetType()) return false;
1121 if (InputCount() != other->InputCount()) return false;
1122
1123 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1124 if (InputAt(i) != other->InputAt(i)) return false;
1125 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001126 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001127 return true;
1128}
1129
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001130std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
1131#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
1132 switch (rhs) {
1133 FOR_EACH_INSTRUCTION(DECLARE_CASE)
1134 default:
1135 os << "Unknown instruction kind " << static_cast<int>(rhs);
1136 break;
1137 }
1138#undef DECLARE_CASE
1139 return os;
1140}
1141
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001142void HInstruction::MoveBefore(HInstruction* cursor) {
David Brazdil39fabd62015-10-26 14:34:30 -05001143 if (kIsDebugBuild && CanThrowIntoCatchBlock()) {
1144 // Make sure we are not moving a throwing instruction out of its try block.
1145 DCHECK(cursor->GetBlock()->IsTryBlock());
1146 const HTryBoundary& current_try = block_->GetTryCatchInformation()->GetTryEntry();
1147 const HTryBoundary& cursor_try = cursor->GetBlock()->GetTryCatchInformation()->GetTryEntry();
1148 DCHECK(cursor_try.HasSameExceptionHandlersAs(current_try));
1149 }
1150
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001151 next_->previous_ = previous_;
1152 if (previous_ != nullptr) {
1153 previous_->next_ = next_;
1154 }
1155 if (block_->instructions_.first_instruction_ == this) {
1156 block_->instructions_.first_instruction_ = next_;
1157 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001158 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001159
1160 previous_ = cursor->previous_;
1161 if (previous_ != nullptr) {
1162 previous_->next_ = this;
1163 }
1164 next_ = cursor;
1165 cursor->previous_ = this;
1166 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001167
1168 if (block_->instructions_.first_instruction_ == cursor) {
1169 block_->instructions_.first_instruction_ = this;
1170 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001171}
1172
David Brazdilfc6a86a2015-06-26 10:33:45 +00001173HBasicBlock* HBasicBlock::SplitBefore(HInstruction* cursor) {
1174 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented";
1175 DCHECK_EQ(cursor->GetBlock(), this);
1176
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001177 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(),
1178 cursor->GetDexPc());
David Brazdilfc6a86a2015-06-26 10:33:45 +00001179 new_block->instructions_.first_instruction_ = cursor;
1180 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1181 instructions_.last_instruction_ = cursor->previous_;
1182 if (cursor->previous_ == nullptr) {
1183 instructions_.first_instruction_ = nullptr;
1184 } else {
1185 cursor->previous_->next_ = nullptr;
1186 cursor->previous_ = nullptr;
1187 }
1188
1189 new_block->instructions_.SetBlockOfInstructions(new_block);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001190 AddInstruction(new (GetGraph()->GetArena()) HGoto(new_block->GetDexPc()));
David Brazdilfc6a86a2015-06-26 10:33:45 +00001191
Vladimir Marko60584552015-09-03 13:35:12 +00001192 for (HBasicBlock* successor : GetSuccessors()) {
1193 new_block->successors_.push_back(successor);
1194 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001195 }
Vladimir Marko60584552015-09-03 13:35:12 +00001196 successors_.clear();
David Brazdilfc6a86a2015-06-26 10:33:45 +00001197 AddSuccessor(new_block);
1198
David Brazdil56e1acc2015-06-30 15:41:36 +01001199 GetGraph()->AddBlock(new_block);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001200 return new_block;
1201}
1202
David Brazdild7558da2015-09-22 13:04:14 +01001203HBasicBlock* HBasicBlock::CreateImmediateDominator() {
1204 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented";
1205 DCHECK(!IsCatchBlock()) << "Support for updating try/catch information not implemented.";
1206
1207 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
1208
1209 for (HBasicBlock* predecessor : GetPredecessors()) {
1210 new_block->predecessors_.push_back(predecessor);
1211 predecessor->successors_[predecessor->GetSuccessorIndexOf(this)] = new_block;
1212 }
1213 predecessors_.clear();
1214 AddPredecessor(new_block);
1215
1216 GetGraph()->AddBlock(new_block);
1217 return new_block;
1218}
1219
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001220HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
1221 DCHECK(!cursor->IsControlFlow());
1222 DCHECK_NE(instructions_.last_instruction_, cursor);
1223 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001224
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001225 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
1226 new_block->instructions_.first_instruction_ = cursor->GetNext();
1227 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1228 cursor->next_->previous_ = nullptr;
1229 cursor->next_ = nullptr;
1230 instructions_.last_instruction_ = cursor;
1231
1232 new_block->instructions_.SetBlockOfInstructions(new_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001233 for (HBasicBlock* successor : GetSuccessors()) {
1234 new_block->successors_.push_back(successor);
1235 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001236 }
Vladimir Marko60584552015-09-03 13:35:12 +00001237 successors_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001238
Vladimir Marko60584552015-09-03 13:35:12 +00001239 for (HBasicBlock* dominated : GetDominatedBlocks()) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001240 dominated->dominator_ = new_block;
Vladimir Marko60584552015-09-03 13:35:12 +00001241 new_block->dominated_blocks_.push_back(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001242 }
Vladimir Marko60584552015-09-03 13:35:12 +00001243 dominated_blocks_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001244 return new_block;
1245}
1246
David Brazdilec16f792015-08-19 15:04:01 +01001247const HTryBoundary* HBasicBlock::ComputeTryEntryOfSuccessors() const {
David Brazdilffee3d32015-07-06 11:48:53 +01001248 if (EndsWithTryBoundary()) {
1249 HTryBoundary* try_boundary = GetLastInstruction()->AsTryBoundary();
1250 if (try_boundary->IsEntry()) {
David Brazdilec16f792015-08-19 15:04:01 +01001251 DCHECK(!IsTryBlock());
David Brazdilffee3d32015-07-06 11:48:53 +01001252 return try_boundary;
1253 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001254 DCHECK(IsTryBlock());
1255 DCHECK(try_catch_information_->GetTryEntry().HasSameExceptionHandlersAs(*try_boundary));
David Brazdilffee3d32015-07-06 11:48:53 +01001256 return nullptr;
1257 }
David Brazdilec16f792015-08-19 15:04:01 +01001258 } else if (IsTryBlock()) {
1259 return &try_catch_information_->GetTryEntry();
David Brazdilffee3d32015-07-06 11:48:53 +01001260 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001261 return nullptr;
David Brazdilffee3d32015-07-06 11:48:53 +01001262 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00001263}
1264
David Brazdild7558da2015-09-22 13:04:14 +01001265bool HBasicBlock::HasThrowingInstructions() const {
1266 for (HInstructionIterator it(GetInstructions()); !it.Done(); it.Advance()) {
1267 if (it.Current()->CanThrow()) {
1268 return true;
1269 }
1270 }
1271 return false;
1272}
1273
David Brazdilfc6a86a2015-06-26 10:33:45 +00001274static bool HasOnlyOneInstruction(const HBasicBlock& block) {
1275 return block.GetPhis().IsEmpty()
1276 && !block.GetInstructions().IsEmpty()
1277 && block.GetFirstInstruction() == block.GetLastInstruction();
1278}
1279
David Brazdil46e2a392015-03-16 17:31:52 +00001280bool HBasicBlock::IsSingleGoto() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001281 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsGoto();
1282}
1283
1284bool HBasicBlock::IsSingleTryBoundary() const {
1285 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsTryBoundary();
David Brazdil46e2a392015-03-16 17:31:52 +00001286}
1287
David Brazdil8d5b8b22015-03-24 10:51:52 +00001288bool HBasicBlock::EndsWithControlFlowInstruction() const {
1289 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
1290}
1291
David Brazdilb2bd1c52015-03-25 11:17:37 +00001292bool HBasicBlock::EndsWithIf() const {
1293 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
1294}
1295
David Brazdilffee3d32015-07-06 11:48:53 +01001296bool HBasicBlock::EndsWithTryBoundary() const {
1297 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsTryBoundary();
1298}
1299
David Brazdilb2bd1c52015-03-25 11:17:37 +00001300bool HBasicBlock::HasSinglePhi() const {
1301 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
1302}
1303
David Brazdilffee3d32015-07-06 11:48:53 +01001304bool HTryBoundary::HasSameExceptionHandlersAs(const HTryBoundary& other) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001305 if (GetBlock()->GetSuccessors().size() != other.GetBlock()->GetSuccessors().size()) {
David Brazdilffee3d32015-07-06 11:48:53 +01001306 return false;
1307 }
1308
David Brazdilb618ade2015-07-29 10:31:29 +01001309 // Exception handlers need to be stored in the same order.
1310 for (HExceptionHandlerIterator it1(*this), it2(other);
1311 !it1.Done();
1312 it1.Advance(), it2.Advance()) {
1313 DCHECK(!it2.Done());
1314 if (it1.Current() != it2.Current()) {
David Brazdilffee3d32015-07-06 11:48:53 +01001315 return false;
1316 }
1317 }
1318 return true;
1319}
1320
David Brazdil2d7352b2015-04-20 14:52:42 +01001321size_t HInstructionList::CountSize() const {
1322 size_t size = 0;
1323 HInstruction* current = first_instruction_;
1324 for (; current != nullptr; current = current->GetNext()) {
1325 size++;
1326 }
1327 return size;
1328}
1329
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001330void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
1331 for (HInstruction* current = first_instruction_;
1332 current != nullptr;
1333 current = current->GetNext()) {
1334 current->SetBlock(block);
1335 }
1336}
1337
1338void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
1339 DCHECK(Contains(cursor));
1340 if (!instruction_list.IsEmpty()) {
1341 if (cursor == last_instruction_) {
1342 last_instruction_ = instruction_list.last_instruction_;
1343 } else {
1344 cursor->next_->previous_ = instruction_list.last_instruction_;
1345 }
1346 instruction_list.last_instruction_->next_ = cursor->next_;
1347 cursor->next_ = instruction_list.first_instruction_;
1348 instruction_list.first_instruction_->previous_ = cursor;
1349 }
1350}
1351
1352void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +00001353 if (IsEmpty()) {
1354 first_instruction_ = instruction_list.first_instruction_;
1355 last_instruction_ = instruction_list.last_instruction_;
1356 } else {
1357 AddAfter(last_instruction_, instruction_list);
1358 }
1359}
1360
David Brazdil2d7352b2015-04-20 14:52:42 +01001361void HBasicBlock::DisconnectAndDelete() {
1362 // Dominators must be removed after all the blocks they dominate. This way
1363 // a loop header is removed last, a requirement for correct loop information
1364 // iteration.
Vladimir Marko60584552015-09-03 13:35:12 +00001365 DCHECK(dominated_blocks_.empty());
David Brazdil46e2a392015-03-16 17:31:52 +00001366
David Brazdil2d7352b2015-04-20 14:52:42 +01001367 // Remove the block from all loops it is included in.
1368 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
1369 HLoopInformation* loop_info = it.Current();
1370 loop_info->Remove(this);
1371 if (loop_info->IsBackEdge(*this)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001372 // If this was the last back edge of the loop, we deliberately leave the
1373 // loop in an inconsistent state and will fail SSAChecker unless the
1374 // entire loop is removed during the pass.
David Brazdil2d7352b2015-04-20 14:52:42 +01001375 loop_info->RemoveBackEdge(this);
1376 }
1377 }
1378
1379 // Disconnect the block from its predecessors and update their control-flow
1380 // instructions.
Vladimir Marko60584552015-09-03 13:35:12 +00001381 for (HBasicBlock* predecessor : predecessors_) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001382 HInstruction* last_instruction = predecessor->GetLastInstruction();
David Brazdil2d7352b2015-04-20 14:52:42 +01001383 predecessor->RemoveSuccessor(this);
Mark Mendellfe57faa2015-09-18 09:26:15 -04001384 uint32_t num_pred_successors = predecessor->GetSuccessors().size();
1385 if (num_pred_successors == 1u) {
1386 // If we have one successor after removing one, then we must have
1387 // had an HIf or HPackedSwitch, as they have more than one successor.
1388 // Replace those with a HGoto.
1389 DCHECK(last_instruction->IsIf() || last_instruction->IsPackedSwitch());
1390 predecessor->RemoveInstruction(last_instruction);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001391 predecessor->AddInstruction(new (graph_->GetArena()) HGoto(last_instruction->GetDexPc()));
Mark Mendellfe57faa2015-09-18 09:26:15 -04001392 } else if (num_pred_successors == 0u) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001393 // The predecessor has no remaining successors and therefore must be dead.
1394 // We deliberately leave it without a control-flow instruction so that the
1395 // SSAChecker fails unless it is not removed during the pass too.
Mark Mendellfe57faa2015-09-18 09:26:15 -04001396 predecessor->RemoveInstruction(last_instruction);
1397 } else {
1398 // There are multiple successors left. This must come from a HPackedSwitch
1399 // and we are in the middle of removing the HPackedSwitch. Like above, leave
1400 // this alone, and the SSAChecker will fail if it is not removed as well.
1401 DCHECK(last_instruction->IsPackedSwitch());
David Brazdil2d7352b2015-04-20 14:52:42 +01001402 }
David Brazdil46e2a392015-03-16 17:31:52 +00001403 }
Vladimir Marko60584552015-09-03 13:35:12 +00001404 predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001405
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001406 // Disconnect the block from its successors and update their phis.
Vladimir Marko60584552015-09-03 13:35:12 +00001407 for (HBasicBlock* successor : successors_) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001408 // Delete this block from the list of predecessors.
1409 size_t this_index = successor->GetPredecessorIndexOf(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001410 successor->predecessors_.erase(successor->predecessors_.begin() + this_index);
David Brazdil2d7352b2015-04-20 14:52:42 +01001411
1412 // Check that `successor` has other predecessors, otherwise `this` is the
1413 // dominator of `successor` which violates the order DCHECKed at the top.
Vladimir Marko60584552015-09-03 13:35:12 +00001414 DCHECK(!successor->predecessors_.empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001415
David Brazdil2d7352b2015-04-20 14:52:42 +01001416 // Remove this block's entries in the successor's phis.
Vladimir Marko60584552015-09-03 13:35:12 +00001417 if (successor->predecessors_.size() == 1u) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001418 // The successor has just one predecessor left. Replace phis with the only
1419 // remaining input.
1420 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1421 HPhi* phi = phi_it.Current()->AsPhi();
1422 phi->ReplaceWith(phi->InputAt(1 - this_index));
1423 successor->RemovePhi(phi);
1424 }
1425 } else {
1426 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1427 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1428 }
1429 }
1430 }
Vladimir Marko60584552015-09-03 13:35:12 +00001431 successors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001432
1433 // Disconnect from the dominator.
1434 dominator_->RemoveDominatedBlock(this);
1435 SetDominator(nullptr);
1436
1437 // Delete from the graph. The function safely deletes remaining instructions
1438 // and updates the reverse post order.
1439 graph_->DeleteDeadBlock(this);
1440 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001441}
1442
1443void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001444 DCHECK_EQ(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00001445 DCHECK(ContainsElement(dominated_blocks_, other));
1446 DCHECK_EQ(GetSingleSuccessor(), other);
1447 DCHECK_EQ(other->GetSinglePredecessor(), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001448 DCHECK(other->GetPhis().IsEmpty());
1449
David Brazdil2d7352b2015-04-20 14:52:42 +01001450 // Move instructions from `other` to `this`.
1451 DCHECK(EndsWithControlFlowInstruction());
1452 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001453 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001454 other->instructions_.SetBlockOfInstructions(this);
1455 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001456
David Brazdil2d7352b2015-04-20 14:52:42 +01001457 // Remove `other` from the loops it is included in.
1458 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1459 HLoopInformation* loop_info = it.Current();
1460 loop_info->Remove(other);
1461 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001462 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01001463 }
1464 }
1465
1466 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00001467 successors_.clear();
1468 while (!other->successors_.empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001469 HBasicBlock* successor = other->GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001470 successor->ReplacePredecessor(other, this);
1471 }
1472
David Brazdil2d7352b2015-04-20 14:52:42 +01001473 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00001474 RemoveDominatedBlock(other);
1475 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
1476 dominated_blocks_.push_back(dominated);
David Brazdil2d7352b2015-04-20 14:52:42 +01001477 dominated->SetDominator(this);
1478 }
Vladimir Marko60584552015-09-03 13:35:12 +00001479 other->dominated_blocks_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001480 other->dominator_ = nullptr;
1481
1482 // Clear the list of predecessors of `other` in preparation of deleting it.
Vladimir Marko60584552015-09-03 13:35:12 +00001483 other->predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001484
1485 // Delete `other` from the graph. The function updates reverse post order.
1486 graph_->DeleteDeadBlock(other);
1487 other->SetGraph(nullptr);
1488}
1489
1490void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1491 DCHECK_NE(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00001492 DCHECK(GetDominatedBlocks().empty());
1493 DCHECK(GetSuccessors().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001494 DCHECK(!EndsWithControlFlowInstruction());
Vladimir Marko60584552015-09-03 13:35:12 +00001495 DCHECK(other->GetSinglePredecessor()->IsEntryBlock());
David Brazdil2d7352b2015-04-20 14:52:42 +01001496 DCHECK(other->GetPhis().IsEmpty());
1497 DCHECK(!other->IsInLoop());
1498
1499 // Move instructions from `other` to `this`.
1500 instructions_.Add(other->GetInstructions());
1501 other->instructions_.SetBlockOfInstructions(this);
1502
1503 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00001504 successors_.clear();
1505 while (!other->successors_.empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001506 HBasicBlock* successor = other->GetSuccessors()[0];
David Brazdil2d7352b2015-04-20 14:52:42 +01001507 successor->ReplacePredecessor(other, this);
1508 }
1509
1510 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00001511 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
1512 dominated_blocks_.push_back(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001513 dominated->SetDominator(this);
1514 }
Vladimir Marko60584552015-09-03 13:35:12 +00001515 other->dominated_blocks_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001516 other->dominator_ = nullptr;
1517 other->graph_ = nullptr;
1518}
1519
1520void HBasicBlock::ReplaceWith(HBasicBlock* other) {
Vladimir Marko60584552015-09-03 13:35:12 +00001521 while (!GetPredecessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001522 HBasicBlock* predecessor = GetPredecessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001523 predecessor->ReplaceSuccessor(this, other);
1524 }
Vladimir Marko60584552015-09-03 13:35:12 +00001525 while (!GetSuccessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001526 HBasicBlock* successor = GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001527 successor->ReplacePredecessor(this, other);
1528 }
Vladimir Marko60584552015-09-03 13:35:12 +00001529 for (HBasicBlock* dominated : GetDominatedBlocks()) {
1530 other->AddDominatedBlock(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001531 }
1532 GetDominator()->ReplaceDominatedBlock(this, other);
1533 other->SetDominator(GetDominator());
1534 dominator_ = nullptr;
1535 graph_ = nullptr;
1536}
1537
1538// Create space in `blocks` for adding `number_of_new_blocks` entries
1539// starting at location `at`. Blocks after `at` are moved accordingly.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001540static void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001541 size_t number_of_new_blocks,
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001542 size_t after) {
1543 DCHECK_LT(after, blocks->size());
1544 size_t old_size = blocks->size();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001545 size_t new_size = old_size + number_of_new_blocks;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001546 blocks->resize(new_size);
1547 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001548}
1549
David Brazdil2d7352b2015-04-20 14:52:42 +01001550void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1551 DCHECK_EQ(block->GetGraph(), this);
Vladimir Marko60584552015-09-03 13:35:12 +00001552 DCHECK(block->GetSuccessors().empty());
1553 DCHECK(block->GetPredecessors().empty());
1554 DCHECK(block->GetDominatedBlocks().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001555 DCHECK(block->GetDominator() == nullptr);
1556
1557 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1558 block->RemoveInstruction(it.Current());
1559 }
1560 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1561 block->RemovePhi(it.Current()->AsPhi());
1562 }
1563
David Brazdilc7af85d2015-05-26 12:05:55 +01001564 if (block->IsExitBlock()) {
1565 exit_block_ = nullptr;
1566 }
1567
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001568 RemoveElement(reverse_post_order_, block);
1569 blocks_[block->GetBlockId()] = nullptr;
David Brazdil2d7352b2015-04-20 14:52:42 +01001570}
1571
Calin Juravle2e768302015-07-28 14:41:11 +00001572HInstruction* HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
David Brazdilc7af85d2015-05-26 12:05:55 +01001573 DCHECK(HasExitBlock()) << "Unimplemented scenario";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001574 // Update the environments in this graph to have the invoke's environment
1575 // as parent.
1576 {
1577 HReversePostOrderIterator it(*this);
1578 it.Advance(); // Skip the entry block, we do not need to update the entry's suspend check.
1579 for (; !it.Done(); it.Advance()) {
1580 HBasicBlock* block = it.Current();
1581 for (HInstructionIterator instr_it(block->GetInstructions());
1582 !instr_it.Done();
1583 instr_it.Advance()) {
1584 HInstruction* current = instr_it.Current();
1585 if (current->NeedsEnvironment()) {
1586 current->GetEnvironment()->SetAndCopyParentChain(
1587 outer_graph->GetArena(), invoke->GetEnvironment());
1588 }
1589 }
1590 }
1591 }
1592 outer_graph->UpdateMaximumNumberOfOutVRegs(GetMaximumNumberOfOutVRegs());
1593 if (HasBoundsChecks()) {
1594 outer_graph->SetHasBoundsChecks(true);
1595 }
1596
Calin Juravle2e768302015-07-28 14:41:11 +00001597 HInstruction* return_value = nullptr;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001598 if (GetBlocks().size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001599 // Simple case of an entry block, a body block, and an exit block.
1600 // Put the body block's instruction into `invoke`'s block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01001601 HBasicBlock* body = GetBlocks()[1];
1602 DCHECK(GetBlocks()[0]->IsEntryBlock());
1603 DCHECK(GetBlocks()[2]->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001604 DCHECK(!body->IsExitBlock());
1605 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001606
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001607 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1608 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001609
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001610 // Replace the invoke with the return value of the inlined graph.
1611 if (last->IsReturn()) {
Calin Juravle2e768302015-07-28 14:41:11 +00001612 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001613 } else {
1614 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001615 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001616
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001617 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001618 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001619 // Need to inline multiple blocks. We split `invoke`'s block
1620 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001621 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001622 // with the second half.
1623 ArenaAllocator* allocator = outer_graph->GetArena();
1624 HBasicBlock* at = invoke->GetBlock();
1625 HBasicBlock* to = at->SplitAfter(invoke);
1626
Vladimir Markoec7802a2015-10-01 20:57:57 +01001627 HBasicBlock* first = entry_block_->GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001628 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001629 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001630 exit_block_->ReplaceWith(to);
1631
1632 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001633 // to not `HReturn` but `HGoto` instead.
Vladimir Markoec7802a2015-10-01 20:57:57 +01001634 bool returns_void = to->GetPredecessors()[0]->GetLastInstruction()->IsReturnVoid();
Vladimir Marko60584552015-09-03 13:35:12 +00001635 if (to->GetPredecessors().size() == 1) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001636 HBasicBlock* predecessor = to->GetPredecessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001637 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001638 if (!returns_void) {
1639 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001640 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001641 predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc()));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001642 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001643 } else {
1644 if (!returns_void) {
1645 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001646 return_value = new (allocator) HPhi(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001647 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()), to->GetDexPc());
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001648 to->AddPhi(return_value->AsPhi());
1649 }
Vladimir Marko60584552015-09-03 13:35:12 +00001650 for (HBasicBlock* predecessor : to->GetPredecessors()) {
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001651 HInstruction* last = predecessor->GetLastInstruction();
1652 if (!returns_void) {
1653 return_value->AsPhi()->AddInput(last->InputAt(0));
1654 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001655 predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001656 predecessor->RemoveInstruction(last);
1657 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001658 }
1659
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001660 // Update the meta information surrounding blocks:
1661 // (1) the graph they are now in,
1662 // (2) the reverse post order of that graph,
1663 // (3) the potential loop information they are now in.
1664
1665 // We don't add the entry block, the exit block, and the first block, which
1666 // has been merged with `at`.
1667 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1668
1669 // We add the `to` block.
1670 static constexpr int kNumberOfNewBlocksInCaller = 1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001671 size_t blocks_added = (reverse_post_order_.size() - kNumberOfSkippedBlocksInCallee)
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001672 + kNumberOfNewBlocksInCaller;
1673
1674 // Find the location of `at` in the outer graph's reverse post order. The new
1675 // blocks will be added after it.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001676 size_t index_of_at = IndexOfElement(outer_graph->reverse_post_order_, at);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001677 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1678
1679 // Do a reverse post order of the blocks in the callee and do (1), (2),
1680 // and (3) to the blocks that apply.
1681 HLoopInformation* info = at->GetLoopInformation();
1682 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1683 HBasicBlock* current = it.Current();
1684 if (current != exit_block_ && current != entry_block_ && current != first) {
1685 DCHECK(!current->IsInLoop());
1686 DCHECK(current->GetGraph() == this);
1687 current->SetGraph(outer_graph);
1688 outer_graph->AddBlock(current);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001689 outer_graph->reverse_post_order_[++index_of_at] = current;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001690 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001691 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001692 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1693 loop_it.Current()->Add(current);
1694 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001695 }
1696 }
1697 }
1698
1699 // Do (1), (2), and (3) to `to`.
1700 to->SetGraph(outer_graph);
1701 outer_graph->AddBlock(to);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001702 outer_graph->reverse_post_order_[++index_of_at] = to;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001703 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001704 to->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001705 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1706 loop_it.Current()->Add(to);
1707 }
David Brazdil46e2a392015-03-16 17:31:52 +00001708 if (info->IsBackEdge(*at)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001709 // Only `to` can become a back edge, as the inlined blocks
1710 // are predecessors of `to`.
1711 info->ReplaceBackEdge(at, to);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001712 }
1713 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001714 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001715
David Brazdil05144f42015-04-16 15:18:00 +01001716 // Update the next instruction id of the outer graph, so that instructions
1717 // added later get bigger ids than those in the inner graph.
1718 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1719
1720 // Walk over the entry block and:
1721 // - Move constants from the entry block to the outer_graph's entry block,
1722 // - Replace HParameterValue instructions with their real value.
1723 // - Remove suspend checks, that hold an environment.
1724 // We must do this after the other blocks have been inlined, otherwise ids of
1725 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001726 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001727 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1728 HInstruction* current = it.Current();
Calin Juravle214bbcd2015-10-20 14:54:07 +01001729 HInstruction* replacement = nullptr;
David Brazdil05144f42015-04-16 15:18:00 +01001730 if (current->IsNullConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001731 replacement = outer_graph->GetNullConstant(current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001732 } else if (current->IsIntConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001733 replacement = outer_graph->GetIntConstant(
1734 current->AsIntConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001735 } else if (current->IsLongConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001736 replacement = outer_graph->GetLongConstant(
1737 current->AsLongConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001738 } else if (current->IsFloatConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001739 replacement = outer_graph->GetFloatConstant(
1740 current->AsFloatConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001741 } else if (current->IsDoubleConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001742 replacement = outer_graph->GetDoubleConstant(
1743 current->AsDoubleConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001744 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001745 if (kIsDebugBuild
1746 && invoke->IsInvokeStaticOrDirect()
1747 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1748 // Ensure we do not use the last input of `invoke`, as it
1749 // contains a clinit check which is not an actual argument.
1750 size_t last_input_index = invoke->InputCount() - 1;
1751 DCHECK(parameter_index != last_input_index);
1752 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01001753 replacement = invoke->InputAt(parameter_index++);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001754 } else if (current->IsCurrentMethod()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001755 replacement = outer_graph->GetCurrentMethod();
David Brazdil05144f42015-04-16 15:18:00 +01001756 } else {
1757 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1758 entry_block_->RemoveInstruction(current);
1759 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01001760 if (replacement != nullptr) {
1761 current->ReplaceWith(replacement);
1762 // If the current is the return value then we need to update the latter.
1763 if (current == return_value) {
1764 DCHECK_EQ(entry_block_, return_value->GetBlock());
1765 return_value = replacement;
1766 }
1767 }
1768 }
1769
1770 if (return_value != nullptr) {
1771 invoke->ReplaceWith(return_value);
David Brazdil05144f42015-04-16 15:18:00 +01001772 }
1773
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001774 // Finally remove the invoke from the caller.
1775 invoke->GetBlock()->RemoveInstruction(invoke);
Calin Juravle2e768302015-07-28 14:41:11 +00001776
1777 return return_value;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001778}
1779
Mingyao Yang3584bce2015-05-19 16:01:59 -07001780/*
1781 * Loop will be transformed to:
1782 * old_pre_header
1783 * |
1784 * if_block
1785 * / \
1786 * dummy_block deopt_block
1787 * \ /
1788 * new_pre_header
1789 * |
1790 * header
1791 */
1792void HGraph::TransformLoopHeaderForBCE(HBasicBlock* header) {
1793 DCHECK(header->IsLoopHeader());
1794 HBasicBlock* pre_header = header->GetDominator();
1795
1796 // Need this to avoid critical edge.
1797 HBasicBlock* if_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1798 // Need this to avoid critical edge.
1799 HBasicBlock* dummy_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1800 HBasicBlock* deopt_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1801 HBasicBlock* new_pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
1802 AddBlock(if_block);
1803 AddBlock(dummy_block);
1804 AddBlock(deopt_block);
1805 AddBlock(new_pre_header);
1806
1807 header->ReplacePredecessor(pre_header, new_pre_header);
Vladimir Marko60584552015-09-03 13:35:12 +00001808 pre_header->successors_.clear();
1809 pre_header->dominated_blocks_.clear();
Mingyao Yang3584bce2015-05-19 16:01:59 -07001810
1811 pre_header->AddSuccessor(if_block);
1812 if_block->AddSuccessor(dummy_block); // True successor
1813 if_block->AddSuccessor(deopt_block); // False successor
1814 dummy_block->AddSuccessor(new_pre_header);
1815 deopt_block->AddSuccessor(new_pre_header);
1816
Vladimir Marko60584552015-09-03 13:35:12 +00001817 pre_header->dominated_blocks_.push_back(if_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001818 if_block->SetDominator(pre_header);
Vladimir Marko60584552015-09-03 13:35:12 +00001819 if_block->dominated_blocks_.push_back(dummy_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001820 dummy_block->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001821 if_block->dominated_blocks_.push_back(deopt_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001822 deopt_block->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001823 if_block->dominated_blocks_.push_back(new_pre_header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001824 new_pre_header->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001825 new_pre_header->dominated_blocks_.push_back(header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001826 header->SetDominator(new_pre_header);
1827
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001828 size_t index_of_header = IndexOfElement(reverse_post_order_, header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001829 MakeRoomFor(&reverse_post_order_, 4, index_of_header - 1);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001830 reverse_post_order_[index_of_header++] = if_block;
1831 reverse_post_order_[index_of_header++] = dummy_block;
1832 reverse_post_order_[index_of_header++] = deopt_block;
1833 reverse_post_order_[index_of_header++] = new_pre_header;
Mingyao Yang3584bce2015-05-19 16:01:59 -07001834
1835 HLoopInformation* info = pre_header->GetLoopInformation();
1836 if (info != nullptr) {
1837 if_block->SetLoopInformation(info);
1838 dummy_block->SetLoopInformation(info);
1839 deopt_block->SetLoopInformation(info);
1840 new_pre_header->SetLoopInformation(info);
1841 for (HLoopInformationOutwardIterator loop_it(*pre_header);
1842 !loop_it.Done();
1843 loop_it.Advance()) {
1844 loop_it.Current()->Add(if_block);
1845 loop_it.Current()->Add(dummy_block);
1846 loop_it.Current()->Add(deopt_block);
1847 loop_it.Current()->Add(new_pre_header);
1848 }
1849 }
1850}
1851
Calin Juravle2e768302015-07-28 14:41:11 +00001852void HInstruction::SetReferenceTypeInfo(ReferenceTypeInfo rti) {
1853 if (kIsDebugBuild) {
1854 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1855 ScopedObjectAccess soa(Thread::Current());
1856 DCHECK(rti.IsValid()) << "Invalid RTI for " << DebugName();
1857 if (IsBoundType()) {
1858 // Having the test here spares us from making the method virtual just for
1859 // the sake of a DCHECK.
1860 ReferenceTypeInfo upper_bound_rti = AsBoundType()->GetUpperBound();
1861 DCHECK(upper_bound_rti.IsSupertypeOf(rti))
1862 << " upper_bound_rti: " << upper_bound_rti
1863 << " rti: " << rti;
David Brazdilbaf89b82015-09-15 11:36:54 +01001864 DCHECK(!upper_bound_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes() || rti.IsExact());
Calin Juravle2e768302015-07-28 14:41:11 +00001865 }
1866 }
1867 reference_type_info_ = rti;
1868}
1869
1870ReferenceTypeInfo::ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
1871
1872ReferenceTypeInfo::ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
1873 : type_handle_(type_handle), is_exact_(is_exact) {
1874 if (kIsDebugBuild) {
1875 ScopedObjectAccess soa(Thread::Current());
1876 DCHECK(IsValidHandle(type_handle));
1877 }
1878}
1879
Calin Juravleacf735c2015-02-12 15:25:22 +00001880std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1881 ScopedObjectAccess soa(Thread::Current());
1882 os << "["
Calin Juravle2e768302015-07-28 14:41:11 +00001883 << " is_valid=" << rhs.IsValid()
1884 << " type=" << (!rhs.IsValid() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
Calin Juravleacf735c2015-02-12 15:25:22 +00001885 << " is_exact=" << rhs.IsExact()
1886 << " ]";
1887 return os;
1888}
1889
Mark Mendellc4701932015-04-10 13:18:51 -04001890bool HInstruction::HasAnyEnvironmentUseBefore(HInstruction* other) {
1891 // For now, assume that instructions in different blocks may use the
1892 // environment.
1893 // TODO: Use the control flow to decide if this is true.
1894 if (GetBlock() != other->GetBlock()) {
1895 return true;
1896 }
1897
1898 // We know that we are in the same block. Walk from 'this' to 'other',
1899 // checking to see if there is any instruction with an environment.
1900 HInstruction* current = this;
1901 for (; current != other && current != nullptr; current = current->GetNext()) {
1902 // This is a conservative check, as the instruction result may not be in
1903 // the referenced environment.
1904 if (current->HasEnvironment()) {
1905 return true;
1906 }
1907 }
1908
1909 // We should have been called with 'this' before 'other' in the block.
1910 // Just confirm this.
1911 DCHECK(current != nullptr);
1912 return false;
1913}
1914
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001915void HInvoke::SetIntrinsic(Intrinsics intrinsic,
1916 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
1917 intrinsic_ = intrinsic;
1918 IntrinsicOptimizations opt(this);
1919 if (needs_env_or_cache == kNoEnvironmentOrCache) {
1920 opt.SetDoesNotNeedDexCache();
1921 opt.SetDoesNotNeedEnvironment();
1922 }
1923}
1924
1925bool HInvoke::NeedsEnvironment() const {
1926 if (!IsIntrinsic()) {
1927 return true;
1928 }
1929 IntrinsicOptimizations opt(*this);
1930 return !opt.GetDoesNotNeedEnvironment();
1931}
1932
Vladimir Markodc151b22015-10-15 18:02:30 +01001933bool HInvokeStaticOrDirect::NeedsDexCacheOfDeclaringClass() const {
1934 if (GetMethodLoadKind() != MethodLoadKind::kDexCacheViaMethod) {
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001935 return false;
1936 }
1937 if (!IsIntrinsic()) {
1938 return true;
1939 }
1940 IntrinsicOptimizations opt(*this);
1941 return !opt.GetDoesNotNeedDexCache();
1942}
1943
Mark Mendellc4701932015-04-10 13:18:51 -04001944void HInstruction::RemoveEnvironmentUsers() {
1945 for (HUseIterator<HEnvironment*> use_it(GetEnvUses()); !use_it.Done(); use_it.Advance()) {
1946 HUseListNode<HEnvironment*>* user_node = use_it.Current();
1947 HEnvironment* user = user_node->GetUser();
1948 user->SetRawEnvAt(user_node->GetIndex(), nullptr);
1949 }
1950 env_uses_.Clear();
1951}
1952
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001953} // namespace art