blob: 68fb0acf7f28761b8c828f7b964b44631a4c48bc [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();
David Brazdilfb552d72015-11-02 20:24:24 +0000369 if (try_entry != nullptr) {
David Brazdilec16f792015-08-19 15:04:01 +0100370 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) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001143 next_->previous_ = previous_;
1144 if (previous_ != nullptr) {
1145 previous_->next_ = next_;
1146 }
1147 if (block_->instructions_.first_instruction_ == this) {
1148 block_->instructions_.first_instruction_ = next_;
1149 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001150 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001151
1152 previous_ = cursor->previous_;
1153 if (previous_ != nullptr) {
1154 previous_->next_ = this;
1155 }
1156 next_ = cursor;
1157 cursor->previous_ = this;
1158 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001159
1160 if (block_->instructions_.first_instruction_ == cursor) {
1161 block_->instructions_.first_instruction_ = this;
1162 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001163}
1164
David Brazdilfc6a86a2015-06-26 10:33:45 +00001165HBasicBlock* HBasicBlock::SplitBefore(HInstruction* cursor) {
1166 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented";
1167 DCHECK_EQ(cursor->GetBlock(), this);
1168
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001169 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(),
1170 cursor->GetDexPc());
David Brazdilfc6a86a2015-06-26 10:33:45 +00001171 new_block->instructions_.first_instruction_ = cursor;
1172 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1173 instructions_.last_instruction_ = cursor->previous_;
1174 if (cursor->previous_ == nullptr) {
1175 instructions_.first_instruction_ = nullptr;
1176 } else {
1177 cursor->previous_->next_ = nullptr;
1178 cursor->previous_ = nullptr;
1179 }
1180
1181 new_block->instructions_.SetBlockOfInstructions(new_block);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001182 AddInstruction(new (GetGraph()->GetArena()) HGoto(new_block->GetDexPc()));
David Brazdilfc6a86a2015-06-26 10:33:45 +00001183
Vladimir Marko60584552015-09-03 13:35:12 +00001184 for (HBasicBlock* successor : GetSuccessors()) {
1185 new_block->successors_.push_back(successor);
1186 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001187 }
Vladimir Marko60584552015-09-03 13:35:12 +00001188 successors_.clear();
David Brazdilfc6a86a2015-06-26 10:33:45 +00001189 AddSuccessor(new_block);
1190
David Brazdil56e1acc2015-06-30 15:41:36 +01001191 GetGraph()->AddBlock(new_block);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001192 return new_block;
1193}
1194
David Brazdild7558da2015-09-22 13:04:14 +01001195HBasicBlock* HBasicBlock::CreateImmediateDominator() {
1196 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented";
1197 DCHECK(!IsCatchBlock()) << "Support for updating try/catch information not implemented.";
1198
1199 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
1200
1201 for (HBasicBlock* predecessor : GetPredecessors()) {
1202 new_block->predecessors_.push_back(predecessor);
1203 predecessor->successors_[predecessor->GetSuccessorIndexOf(this)] = new_block;
1204 }
1205 predecessors_.clear();
1206 AddPredecessor(new_block);
1207
1208 GetGraph()->AddBlock(new_block);
1209 return new_block;
1210}
1211
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001212HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
1213 DCHECK(!cursor->IsControlFlow());
1214 DCHECK_NE(instructions_.last_instruction_, cursor);
1215 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001216
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001217 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
1218 new_block->instructions_.first_instruction_ = cursor->GetNext();
1219 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1220 cursor->next_->previous_ = nullptr;
1221 cursor->next_ = nullptr;
1222 instructions_.last_instruction_ = cursor;
1223
1224 new_block->instructions_.SetBlockOfInstructions(new_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001225 for (HBasicBlock* successor : GetSuccessors()) {
1226 new_block->successors_.push_back(successor);
1227 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001228 }
Vladimir Marko60584552015-09-03 13:35:12 +00001229 successors_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001230
Vladimir Marko60584552015-09-03 13:35:12 +00001231 for (HBasicBlock* dominated : GetDominatedBlocks()) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001232 dominated->dominator_ = new_block;
Vladimir Marko60584552015-09-03 13:35:12 +00001233 new_block->dominated_blocks_.push_back(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001234 }
Vladimir Marko60584552015-09-03 13:35:12 +00001235 dominated_blocks_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001236 return new_block;
1237}
1238
David Brazdilec16f792015-08-19 15:04:01 +01001239const HTryBoundary* HBasicBlock::ComputeTryEntryOfSuccessors() const {
David Brazdilffee3d32015-07-06 11:48:53 +01001240 if (EndsWithTryBoundary()) {
1241 HTryBoundary* try_boundary = GetLastInstruction()->AsTryBoundary();
1242 if (try_boundary->IsEntry()) {
David Brazdilec16f792015-08-19 15:04:01 +01001243 DCHECK(!IsTryBlock());
David Brazdilffee3d32015-07-06 11:48:53 +01001244 return try_boundary;
1245 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001246 DCHECK(IsTryBlock());
1247 DCHECK(try_catch_information_->GetTryEntry().HasSameExceptionHandlersAs(*try_boundary));
David Brazdilffee3d32015-07-06 11:48:53 +01001248 return nullptr;
1249 }
David Brazdilec16f792015-08-19 15:04:01 +01001250 } else if (IsTryBlock()) {
1251 return &try_catch_information_->GetTryEntry();
David Brazdilffee3d32015-07-06 11:48:53 +01001252 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001253 return nullptr;
David Brazdilffee3d32015-07-06 11:48:53 +01001254 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00001255}
1256
David Brazdild7558da2015-09-22 13:04:14 +01001257bool HBasicBlock::HasThrowingInstructions() const {
1258 for (HInstructionIterator it(GetInstructions()); !it.Done(); it.Advance()) {
1259 if (it.Current()->CanThrow()) {
1260 return true;
1261 }
1262 }
1263 return false;
1264}
1265
David Brazdilfc6a86a2015-06-26 10:33:45 +00001266static bool HasOnlyOneInstruction(const HBasicBlock& block) {
1267 return block.GetPhis().IsEmpty()
1268 && !block.GetInstructions().IsEmpty()
1269 && block.GetFirstInstruction() == block.GetLastInstruction();
1270}
1271
David Brazdil46e2a392015-03-16 17:31:52 +00001272bool HBasicBlock::IsSingleGoto() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001273 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsGoto();
1274}
1275
1276bool HBasicBlock::IsSingleTryBoundary() const {
1277 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsTryBoundary();
David Brazdil46e2a392015-03-16 17:31:52 +00001278}
1279
David Brazdil8d5b8b22015-03-24 10:51:52 +00001280bool HBasicBlock::EndsWithControlFlowInstruction() const {
1281 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
1282}
1283
David Brazdilb2bd1c52015-03-25 11:17:37 +00001284bool HBasicBlock::EndsWithIf() const {
1285 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
1286}
1287
David Brazdilffee3d32015-07-06 11:48:53 +01001288bool HBasicBlock::EndsWithTryBoundary() const {
1289 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsTryBoundary();
1290}
1291
David Brazdilb2bd1c52015-03-25 11:17:37 +00001292bool HBasicBlock::HasSinglePhi() const {
1293 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
1294}
1295
David Brazdilffee3d32015-07-06 11:48:53 +01001296bool HTryBoundary::HasSameExceptionHandlersAs(const HTryBoundary& other) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001297 if (GetBlock()->GetSuccessors().size() != other.GetBlock()->GetSuccessors().size()) {
David Brazdilffee3d32015-07-06 11:48:53 +01001298 return false;
1299 }
1300
David Brazdilb618ade2015-07-29 10:31:29 +01001301 // Exception handlers need to be stored in the same order.
1302 for (HExceptionHandlerIterator it1(*this), it2(other);
1303 !it1.Done();
1304 it1.Advance(), it2.Advance()) {
1305 DCHECK(!it2.Done());
1306 if (it1.Current() != it2.Current()) {
David Brazdilffee3d32015-07-06 11:48:53 +01001307 return false;
1308 }
1309 }
1310 return true;
1311}
1312
David Brazdil2d7352b2015-04-20 14:52:42 +01001313size_t HInstructionList::CountSize() const {
1314 size_t size = 0;
1315 HInstruction* current = first_instruction_;
1316 for (; current != nullptr; current = current->GetNext()) {
1317 size++;
1318 }
1319 return size;
1320}
1321
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001322void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
1323 for (HInstruction* current = first_instruction_;
1324 current != nullptr;
1325 current = current->GetNext()) {
1326 current->SetBlock(block);
1327 }
1328}
1329
1330void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
1331 DCHECK(Contains(cursor));
1332 if (!instruction_list.IsEmpty()) {
1333 if (cursor == last_instruction_) {
1334 last_instruction_ = instruction_list.last_instruction_;
1335 } else {
1336 cursor->next_->previous_ = instruction_list.last_instruction_;
1337 }
1338 instruction_list.last_instruction_->next_ = cursor->next_;
1339 cursor->next_ = instruction_list.first_instruction_;
1340 instruction_list.first_instruction_->previous_ = cursor;
1341 }
1342}
1343
1344void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +00001345 if (IsEmpty()) {
1346 first_instruction_ = instruction_list.first_instruction_;
1347 last_instruction_ = instruction_list.last_instruction_;
1348 } else {
1349 AddAfter(last_instruction_, instruction_list);
1350 }
1351}
1352
David Brazdil2d7352b2015-04-20 14:52:42 +01001353void HBasicBlock::DisconnectAndDelete() {
1354 // Dominators must be removed after all the blocks they dominate. This way
1355 // a loop header is removed last, a requirement for correct loop information
1356 // iteration.
Vladimir Marko60584552015-09-03 13:35:12 +00001357 DCHECK(dominated_blocks_.empty());
David Brazdil46e2a392015-03-16 17:31:52 +00001358
David Brazdil2d7352b2015-04-20 14:52:42 +01001359 // Remove the block from all loops it is included in.
1360 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
1361 HLoopInformation* loop_info = it.Current();
1362 loop_info->Remove(this);
1363 if (loop_info->IsBackEdge(*this)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001364 // If this was the last back edge of the loop, we deliberately leave the
1365 // loop in an inconsistent state and will fail SSAChecker unless the
1366 // entire loop is removed during the pass.
David Brazdil2d7352b2015-04-20 14:52:42 +01001367 loop_info->RemoveBackEdge(this);
1368 }
1369 }
1370
1371 // Disconnect the block from its predecessors and update their control-flow
1372 // instructions.
Vladimir Marko60584552015-09-03 13:35:12 +00001373 for (HBasicBlock* predecessor : predecessors_) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001374 HInstruction* last_instruction = predecessor->GetLastInstruction();
David Brazdil2d7352b2015-04-20 14:52:42 +01001375 predecessor->RemoveSuccessor(this);
Mark Mendellfe57faa2015-09-18 09:26:15 -04001376 uint32_t num_pred_successors = predecessor->GetSuccessors().size();
1377 if (num_pred_successors == 1u) {
1378 // If we have one successor after removing one, then we must have
David Brazdilfb552d72015-11-02 20:24:24 +00001379 // had an HIf or HPackedSwitch, as they have more than one successor.
1380 // Replace those with a HGoto.
1381 DCHECK(last_instruction->IsIf() || last_instruction->IsPackedSwitch());
Mark Mendellfe57faa2015-09-18 09:26:15 -04001382 predecessor->RemoveInstruction(last_instruction);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001383 predecessor->AddInstruction(new (graph_->GetArena()) HGoto(last_instruction->GetDexPc()));
Mark Mendellfe57faa2015-09-18 09:26:15 -04001384 } else if (num_pred_successors == 0u) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001385 // The predecessor has no remaining successors and therefore must be dead.
1386 // We deliberately leave it without a control-flow instruction so that the
1387 // SSAChecker fails unless it is not removed during the pass too.
Mark Mendellfe57faa2015-09-18 09:26:15 -04001388 predecessor->RemoveInstruction(last_instruction);
1389 } else {
David Brazdilfb552d72015-11-02 20:24:24 +00001390 // There are multiple successors left. This must come from a HPackedSwitch
1391 // and we are in the middle of removing the HPackedSwitch. Like above, leave
1392 // this alone, and the SSAChecker will fail if it is not removed as well.
1393 DCHECK(last_instruction->IsPackedSwitch());
David Brazdil2d7352b2015-04-20 14:52:42 +01001394 }
David Brazdil46e2a392015-03-16 17:31:52 +00001395 }
Vladimir Marko60584552015-09-03 13:35:12 +00001396 predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001397
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001398 // Disconnect the block from its successors and update their phis.
Vladimir Marko60584552015-09-03 13:35:12 +00001399 for (HBasicBlock* successor : successors_) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001400 // Delete this block from the list of predecessors.
1401 size_t this_index = successor->GetPredecessorIndexOf(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001402 successor->predecessors_.erase(successor->predecessors_.begin() + this_index);
David Brazdil2d7352b2015-04-20 14:52:42 +01001403
1404 // Check that `successor` has other predecessors, otherwise `this` is the
1405 // dominator of `successor` which violates the order DCHECKed at the top.
Vladimir Marko60584552015-09-03 13:35:12 +00001406 DCHECK(!successor->predecessors_.empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001407
David Brazdil2d7352b2015-04-20 14:52:42 +01001408 // Remove this block's entries in the successor's phis.
Vladimir Marko60584552015-09-03 13:35:12 +00001409 if (successor->predecessors_.size() == 1u) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001410 // The successor has just one predecessor left. Replace phis with the only
1411 // remaining input.
1412 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1413 HPhi* phi = phi_it.Current()->AsPhi();
1414 phi->ReplaceWith(phi->InputAt(1 - this_index));
1415 successor->RemovePhi(phi);
1416 }
1417 } else {
1418 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1419 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1420 }
1421 }
1422 }
Vladimir Marko60584552015-09-03 13:35:12 +00001423 successors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001424
1425 // Disconnect from the dominator.
1426 dominator_->RemoveDominatedBlock(this);
1427 SetDominator(nullptr);
1428
1429 // Delete from the graph. The function safely deletes remaining instructions
1430 // and updates the reverse post order.
1431 graph_->DeleteDeadBlock(this);
1432 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001433}
1434
1435void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001436 DCHECK_EQ(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00001437 DCHECK(ContainsElement(dominated_blocks_, other));
1438 DCHECK_EQ(GetSingleSuccessor(), other);
1439 DCHECK_EQ(other->GetSinglePredecessor(), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001440 DCHECK(other->GetPhis().IsEmpty());
1441
David Brazdil2d7352b2015-04-20 14:52:42 +01001442 // Move instructions from `other` to `this`.
1443 DCHECK(EndsWithControlFlowInstruction());
1444 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001445 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001446 other->instructions_.SetBlockOfInstructions(this);
1447 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001448
David Brazdil2d7352b2015-04-20 14:52:42 +01001449 // Remove `other` from the loops it is included in.
1450 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1451 HLoopInformation* loop_info = it.Current();
1452 loop_info->Remove(other);
1453 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001454 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01001455 }
1456 }
1457
1458 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00001459 successors_.clear();
1460 while (!other->successors_.empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001461 HBasicBlock* successor = other->GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001462 successor->ReplacePredecessor(other, this);
1463 }
1464
David Brazdil2d7352b2015-04-20 14:52:42 +01001465 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00001466 RemoveDominatedBlock(other);
1467 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
1468 dominated_blocks_.push_back(dominated);
David Brazdil2d7352b2015-04-20 14:52:42 +01001469 dominated->SetDominator(this);
1470 }
Vladimir Marko60584552015-09-03 13:35:12 +00001471 other->dominated_blocks_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001472 other->dominator_ = nullptr;
1473
1474 // Clear the list of predecessors of `other` in preparation of deleting it.
Vladimir Marko60584552015-09-03 13:35:12 +00001475 other->predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001476
1477 // Delete `other` from the graph. The function updates reverse post order.
1478 graph_->DeleteDeadBlock(other);
1479 other->SetGraph(nullptr);
1480}
1481
1482void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1483 DCHECK_NE(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00001484 DCHECK(GetDominatedBlocks().empty());
1485 DCHECK(GetSuccessors().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001486 DCHECK(!EndsWithControlFlowInstruction());
Vladimir Marko60584552015-09-03 13:35:12 +00001487 DCHECK(other->GetSinglePredecessor()->IsEntryBlock());
David Brazdil2d7352b2015-04-20 14:52:42 +01001488 DCHECK(other->GetPhis().IsEmpty());
1489 DCHECK(!other->IsInLoop());
1490
1491 // Move instructions from `other` to `this`.
1492 instructions_.Add(other->GetInstructions());
1493 other->instructions_.SetBlockOfInstructions(this);
1494
1495 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00001496 successors_.clear();
1497 while (!other->successors_.empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001498 HBasicBlock* successor = other->GetSuccessors()[0];
David Brazdil2d7352b2015-04-20 14:52:42 +01001499 successor->ReplacePredecessor(other, this);
1500 }
1501
1502 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00001503 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
1504 dominated_blocks_.push_back(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001505 dominated->SetDominator(this);
1506 }
Vladimir Marko60584552015-09-03 13:35:12 +00001507 other->dominated_blocks_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001508 other->dominator_ = nullptr;
1509 other->graph_ = nullptr;
1510}
1511
1512void HBasicBlock::ReplaceWith(HBasicBlock* other) {
Vladimir Marko60584552015-09-03 13:35:12 +00001513 while (!GetPredecessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001514 HBasicBlock* predecessor = GetPredecessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001515 predecessor->ReplaceSuccessor(this, other);
1516 }
Vladimir Marko60584552015-09-03 13:35:12 +00001517 while (!GetSuccessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001518 HBasicBlock* successor = GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001519 successor->ReplacePredecessor(this, other);
1520 }
Vladimir Marko60584552015-09-03 13:35:12 +00001521 for (HBasicBlock* dominated : GetDominatedBlocks()) {
1522 other->AddDominatedBlock(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001523 }
1524 GetDominator()->ReplaceDominatedBlock(this, other);
1525 other->SetDominator(GetDominator());
1526 dominator_ = nullptr;
1527 graph_ = nullptr;
1528}
1529
1530// Create space in `blocks` for adding `number_of_new_blocks` entries
1531// starting at location `at`. Blocks after `at` are moved accordingly.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001532static void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001533 size_t number_of_new_blocks,
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001534 size_t after) {
1535 DCHECK_LT(after, blocks->size());
1536 size_t old_size = blocks->size();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001537 size_t new_size = old_size + number_of_new_blocks;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001538 blocks->resize(new_size);
1539 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001540}
1541
David Brazdil2d7352b2015-04-20 14:52:42 +01001542void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1543 DCHECK_EQ(block->GetGraph(), this);
Vladimir Marko60584552015-09-03 13:35:12 +00001544 DCHECK(block->GetSuccessors().empty());
1545 DCHECK(block->GetPredecessors().empty());
1546 DCHECK(block->GetDominatedBlocks().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001547 DCHECK(block->GetDominator() == nullptr);
1548
1549 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1550 block->RemoveInstruction(it.Current());
1551 }
1552 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1553 block->RemovePhi(it.Current()->AsPhi());
1554 }
1555
David Brazdilc7af85d2015-05-26 12:05:55 +01001556 if (block->IsExitBlock()) {
1557 exit_block_ = nullptr;
1558 }
1559
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001560 RemoveElement(reverse_post_order_, block);
1561 blocks_[block->GetBlockId()] = nullptr;
David Brazdil2d7352b2015-04-20 14:52:42 +01001562}
1563
Calin Juravle2e768302015-07-28 14:41:11 +00001564HInstruction* HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
David Brazdilc7af85d2015-05-26 12:05:55 +01001565 DCHECK(HasExitBlock()) << "Unimplemented scenario";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001566 // Update the environments in this graph to have the invoke's environment
1567 // as parent.
1568 {
1569 HReversePostOrderIterator it(*this);
1570 it.Advance(); // Skip the entry block, we do not need to update the entry's suspend check.
1571 for (; !it.Done(); it.Advance()) {
1572 HBasicBlock* block = it.Current();
1573 for (HInstructionIterator instr_it(block->GetInstructions());
1574 !instr_it.Done();
1575 instr_it.Advance()) {
1576 HInstruction* current = instr_it.Current();
1577 if (current->NeedsEnvironment()) {
1578 current->GetEnvironment()->SetAndCopyParentChain(
1579 outer_graph->GetArena(), invoke->GetEnvironment());
1580 }
1581 }
1582 }
1583 }
1584 outer_graph->UpdateMaximumNumberOfOutVRegs(GetMaximumNumberOfOutVRegs());
1585 if (HasBoundsChecks()) {
1586 outer_graph->SetHasBoundsChecks(true);
1587 }
1588
Calin Juravle2e768302015-07-28 14:41:11 +00001589 HInstruction* return_value = nullptr;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001590 if (GetBlocks().size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001591 // Simple case of an entry block, a body block, and an exit block.
1592 // Put the body block's instruction into `invoke`'s block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01001593 HBasicBlock* body = GetBlocks()[1];
1594 DCHECK(GetBlocks()[0]->IsEntryBlock());
1595 DCHECK(GetBlocks()[2]->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001596 DCHECK(!body->IsExitBlock());
1597 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001598
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001599 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1600 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001601
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001602 // Replace the invoke with the return value of the inlined graph.
1603 if (last->IsReturn()) {
Calin Juravle2e768302015-07-28 14:41:11 +00001604 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001605 } else {
1606 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001607 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001608
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001609 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001610 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001611 // Need to inline multiple blocks. We split `invoke`'s block
1612 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001613 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001614 // with the second half.
1615 ArenaAllocator* allocator = outer_graph->GetArena();
1616 HBasicBlock* at = invoke->GetBlock();
1617 HBasicBlock* to = at->SplitAfter(invoke);
1618
Vladimir Markoec7802a2015-10-01 20:57:57 +01001619 HBasicBlock* first = entry_block_->GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001620 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001621 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001622 exit_block_->ReplaceWith(to);
1623
1624 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001625 // to not `HReturn` but `HGoto` instead.
Vladimir Markoec7802a2015-10-01 20:57:57 +01001626 bool returns_void = to->GetPredecessors()[0]->GetLastInstruction()->IsReturnVoid();
Vladimir Marko60584552015-09-03 13:35:12 +00001627 if (to->GetPredecessors().size() == 1) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001628 HBasicBlock* predecessor = to->GetPredecessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001629 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001630 if (!returns_void) {
1631 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001632 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001633 predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc()));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001634 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001635 } else {
1636 if (!returns_void) {
1637 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001638 return_value = new (allocator) HPhi(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001639 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()), to->GetDexPc());
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001640 to->AddPhi(return_value->AsPhi());
1641 }
Vladimir Marko60584552015-09-03 13:35:12 +00001642 for (HBasicBlock* predecessor : to->GetPredecessors()) {
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001643 HInstruction* last = predecessor->GetLastInstruction();
1644 if (!returns_void) {
1645 return_value->AsPhi()->AddInput(last->InputAt(0));
1646 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001647 predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001648 predecessor->RemoveInstruction(last);
1649 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001650 }
1651
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001652 // Update the meta information surrounding blocks:
1653 // (1) the graph they are now in,
1654 // (2) the reverse post order of that graph,
David Brazdil95177982015-10-30 12:56:58 -05001655 // (3) the potential loop information they are now in,
1656 // (4) try block membership.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001657
1658 // We don't add the entry block, the exit block, and the first block, which
1659 // has been merged with `at`.
1660 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1661
1662 // We add the `to` block.
1663 static constexpr int kNumberOfNewBlocksInCaller = 1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001664 size_t blocks_added = (reverse_post_order_.size() - kNumberOfSkippedBlocksInCallee)
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001665 + kNumberOfNewBlocksInCaller;
1666
1667 // Find the location of `at` in the outer graph's reverse post order. The new
1668 // blocks will be added after it.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001669 size_t index_of_at = IndexOfElement(outer_graph->reverse_post_order_, at);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001670 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1671
David Brazdil95177982015-10-30 12:56:58 -05001672 HLoopInformation* loop_info = at->GetLoopInformation();
1673 // Copy TryCatchInformation if `at` is a try block, not if it is a catch block.
1674 TryCatchInformation* try_catch_info = at->IsTryBlock() ? at->GetTryCatchInformation() : nullptr;
1675
1676 // Do a reverse post order of the blocks in the callee and do (1), (2), (3)
1677 // and (4) to the blocks that apply.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001678 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1679 HBasicBlock* current = it.Current();
1680 if (current != exit_block_ && current != entry_block_ && current != first) {
1681 DCHECK(!current->IsInLoop());
David Brazdil95177982015-10-30 12:56:58 -05001682 DCHECK(current->GetTryCatchInformation() == nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001683 DCHECK(current->GetGraph() == this);
1684 current->SetGraph(outer_graph);
1685 outer_graph->AddBlock(current);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001686 outer_graph->reverse_post_order_[++index_of_at] = current;
David Brazdil95177982015-10-30 12:56:58 -05001687 if (loop_info != nullptr) {
1688 current->SetLoopInformation(loop_info);
David Brazdil7d275372015-04-21 16:36:35 +01001689 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1690 loop_it.Current()->Add(current);
1691 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001692 }
David Brazdil95177982015-10-30 12:56:58 -05001693 current->SetTryCatchInformation(try_catch_info);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001694 }
1695 }
1696
David Brazdil95177982015-10-30 12:56:58 -05001697 // Do (1), (2), (3) and (4) to `to`.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001698 to->SetGraph(outer_graph);
1699 outer_graph->AddBlock(to);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001700 outer_graph->reverse_post_order_[++index_of_at] = to;
David Brazdil95177982015-10-30 12:56:58 -05001701 if (loop_info != nullptr) {
1702 to->SetLoopInformation(loop_info);
David Brazdil7d275372015-04-21 16:36:35 +01001703 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1704 loop_it.Current()->Add(to);
1705 }
David Brazdil95177982015-10-30 12:56:58 -05001706 if (loop_info->IsBackEdge(*at)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001707 // Only `to` can become a back edge, as the inlined blocks
1708 // are predecessors of `to`.
David Brazdil95177982015-10-30 12:56:58 -05001709 loop_info->ReplaceBackEdge(at, to);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001710 }
1711 }
David Brazdil95177982015-10-30 12:56:58 -05001712 to->SetTryCatchInformation(try_catch_info);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001713 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001714
David Brazdil05144f42015-04-16 15:18:00 +01001715 // Update the next instruction id of the outer graph, so that instructions
1716 // added later get bigger ids than those in the inner graph.
1717 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1718
1719 // Walk over the entry block and:
1720 // - Move constants from the entry block to the outer_graph's entry block,
1721 // - Replace HParameterValue instructions with their real value.
1722 // - Remove suspend checks, that hold an environment.
1723 // We must do this after the other blocks have been inlined, otherwise ids of
1724 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001725 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001726 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1727 HInstruction* current = it.Current();
Calin Juravle214bbcd2015-10-20 14:54:07 +01001728 HInstruction* replacement = nullptr;
David Brazdil05144f42015-04-16 15:18:00 +01001729 if (current->IsNullConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001730 replacement = outer_graph->GetNullConstant(current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001731 } else if (current->IsIntConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001732 replacement = outer_graph->GetIntConstant(
1733 current->AsIntConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001734 } else if (current->IsLongConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001735 replacement = outer_graph->GetLongConstant(
1736 current->AsLongConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001737 } else if (current->IsFloatConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001738 replacement = outer_graph->GetFloatConstant(
1739 current->AsFloatConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001740 } else if (current->IsDoubleConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001741 replacement = outer_graph->GetDoubleConstant(
1742 current->AsDoubleConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001743 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001744 if (kIsDebugBuild
1745 && invoke->IsInvokeStaticOrDirect()
1746 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1747 // Ensure we do not use the last input of `invoke`, as it
1748 // contains a clinit check which is not an actual argument.
1749 size_t last_input_index = invoke->InputCount() - 1;
1750 DCHECK(parameter_index != last_input_index);
1751 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01001752 replacement = invoke->InputAt(parameter_index++);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001753 } else if (current->IsCurrentMethod()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001754 replacement = outer_graph->GetCurrentMethod();
David Brazdil05144f42015-04-16 15:18:00 +01001755 } else {
1756 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1757 entry_block_->RemoveInstruction(current);
1758 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01001759 if (replacement != nullptr) {
1760 current->ReplaceWith(replacement);
1761 // If the current is the return value then we need to update the latter.
1762 if (current == return_value) {
1763 DCHECK_EQ(entry_block_, return_value->GetBlock());
1764 return_value = replacement;
1765 }
1766 }
1767 }
1768
1769 if (return_value != nullptr) {
1770 invoke->ReplaceWith(return_value);
David Brazdil05144f42015-04-16 15:18:00 +01001771 }
1772
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001773 // Finally remove the invoke from the caller.
1774 invoke->GetBlock()->RemoveInstruction(invoke);
Calin Juravle2e768302015-07-28 14:41:11 +00001775
1776 return return_value;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001777}
1778
Mingyao Yang3584bce2015-05-19 16:01:59 -07001779/*
1780 * Loop will be transformed to:
1781 * old_pre_header
1782 * |
1783 * if_block
1784 * / \
1785 * dummy_block deopt_block
1786 * \ /
1787 * new_pre_header
1788 * |
1789 * header
1790 */
1791void HGraph::TransformLoopHeaderForBCE(HBasicBlock* header) {
1792 DCHECK(header->IsLoopHeader());
1793 HBasicBlock* pre_header = header->GetDominator();
1794
1795 // Need this to avoid critical edge.
1796 HBasicBlock* if_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1797 // Need this to avoid critical edge.
1798 HBasicBlock* dummy_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1799 HBasicBlock* deopt_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1800 HBasicBlock* new_pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
1801 AddBlock(if_block);
1802 AddBlock(dummy_block);
1803 AddBlock(deopt_block);
1804 AddBlock(new_pre_header);
1805
1806 header->ReplacePredecessor(pre_header, new_pre_header);
Vladimir Marko60584552015-09-03 13:35:12 +00001807 pre_header->successors_.clear();
1808 pre_header->dominated_blocks_.clear();
Mingyao Yang3584bce2015-05-19 16:01:59 -07001809
1810 pre_header->AddSuccessor(if_block);
1811 if_block->AddSuccessor(dummy_block); // True successor
1812 if_block->AddSuccessor(deopt_block); // False successor
1813 dummy_block->AddSuccessor(new_pre_header);
1814 deopt_block->AddSuccessor(new_pre_header);
1815
Vladimir Marko60584552015-09-03 13:35:12 +00001816 pre_header->dominated_blocks_.push_back(if_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001817 if_block->SetDominator(pre_header);
Vladimir Marko60584552015-09-03 13:35:12 +00001818 if_block->dominated_blocks_.push_back(dummy_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001819 dummy_block->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001820 if_block->dominated_blocks_.push_back(deopt_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001821 deopt_block->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001822 if_block->dominated_blocks_.push_back(new_pre_header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001823 new_pre_header->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001824 new_pre_header->dominated_blocks_.push_back(header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001825 header->SetDominator(new_pre_header);
1826
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001827 size_t index_of_header = IndexOfElement(reverse_post_order_, header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001828 MakeRoomFor(&reverse_post_order_, 4, index_of_header - 1);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001829 reverse_post_order_[index_of_header++] = if_block;
1830 reverse_post_order_[index_of_header++] = dummy_block;
1831 reverse_post_order_[index_of_header++] = deopt_block;
1832 reverse_post_order_[index_of_header++] = new_pre_header;
Mingyao Yang3584bce2015-05-19 16:01:59 -07001833
1834 HLoopInformation* info = pre_header->GetLoopInformation();
1835 if (info != nullptr) {
1836 if_block->SetLoopInformation(info);
1837 dummy_block->SetLoopInformation(info);
1838 deopt_block->SetLoopInformation(info);
1839 new_pre_header->SetLoopInformation(info);
1840 for (HLoopInformationOutwardIterator loop_it(*pre_header);
1841 !loop_it.Done();
1842 loop_it.Advance()) {
1843 loop_it.Current()->Add(if_block);
1844 loop_it.Current()->Add(dummy_block);
1845 loop_it.Current()->Add(deopt_block);
1846 loop_it.Current()->Add(new_pre_header);
1847 }
1848 }
1849}
1850
Calin Juravle2e768302015-07-28 14:41:11 +00001851void HInstruction::SetReferenceTypeInfo(ReferenceTypeInfo rti) {
1852 if (kIsDebugBuild) {
1853 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1854 ScopedObjectAccess soa(Thread::Current());
1855 DCHECK(rti.IsValid()) << "Invalid RTI for " << DebugName();
1856 if (IsBoundType()) {
1857 // Having the test here spares us from making the method virtual just for
1858 // the sake of a DCHECK.
1859 ReferenceTypeInfo upper_bound_rti = AsBoundType()->GetUpperBound();
1860 DCHECK(upper_bound_rti.IsSupertypeOf(rti))
1861 << " upper_bound_rti: " << upper_bound_rti
1862 << " rti: " << rti;
David Brazdilbaf89b82015-09-15 11:36:54 +01001863 DCHECK(!upper_bound_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes() || rti.IsExact());
Calin Juravle2e768302015-07-28 14:41:11 +00001864 }
1865 }
1866 reference_type_info_ = rti;
1867}
1868
1869ReferenceTypeInfo::ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
1870
1871ReferenceTypeInfo::ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
1872 : type_handle_(type_handle), is_exact_(is_exact) {
1873 if (kIsDebugBuild) {
1874 ScopedObjectAccess soa(Thread::Current());
1875 DCHECK(IsValidHandle(type_handle));
1876 }
1877}
1878
Calin Juravleacf735c2015-02-12 15:25:22 +00001879std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1880 ScopedObjectAccess soa(Thread::Current());
1881 os << "["
Calin Juravle2e768302015-07-28 14:41:11 +00001882 << " is_valid=" << rhs.IsValid()
1883 << " type=" << (!rhs.IsValid() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
Calin Juravleacf735c2015-02-12 15:25:22 +00001884 << " is_exact=" << rhs.IsExact()
1885 << " ]";
1886 return os;
1887}
1888
Mark Mendellc4701932015-04-10 13:18:51 -04001889bool HInstruction::HasAnyEnvironmentUseBefore(HInstruction* other) {
1890 // For now, assume that instructions in different blocks may use the
1891 // environment.
1892 // TODO: Use the control flow to decide if this is true.
1893 if (GetBlock() != other->GetBlock()) {
1894 return true;
1895 }
1896
1897 // We know that we are in the same block. Walk from 'this' to 'other',
1898 // checking to see if there is any instruction with an environment.
1899 HInstruction* current = this;
1900 for (; current != other && current != nullptr; current = current->GetNext()) {
1901 // This is a conservative check, as the instruction result may not be in
1902 // the referenced environment.
1903 if (current->HasEnvironment()) {
1904 return true;
1905 }
1906 }
1907
1908 // We should have been called with 'this' before 'other' in the block.
1909 // Just confirm this.
1910 DCHECK(current != nullptr);
1911 return false;
1912}
1913
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001914void HInvoke::SetIntrinsic(Intrinsics intrinsic,
1915 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
1916 intrinsic_ = intrinsic;
1917 IntrinsicOptimizations opt(this);
1918 if (needs_env_or_cache == kNoEnvironmentOrCache) {
1919 opt.SetDoesNotNeedDexCache();
1920 opt.SetDoesNotNeedEnvironment();
1921 }
1922}
1923
1924bool HInvoke::NeedsEnvironment() const {
1925 if (!IsIntrinsic()) {
1926 return true;
1927 }
1928 IntrinsicOptimizations opt(*this);
1929 return !opt.GetDoesNotNeedEnvironment();
1930}
1931
Vladimir Markodc151b22015-10-15 18:02:30 +01001932bool HInvokeStaticOrDirect::NeedsDexCacheOfDeclaringClass() const {
1933 if (GetMethodLoadKind() != MethodLoadKind::kDexCacheViaMethod) {
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001934 return false;
1935 }
1936 if (!IsIntrinsic()) {
1937 return true;
1938 }
1939 IntrinsicOptimizations opt(*this);
1940 return !opt.GetDoesNotNeedDexCache();
1941}
1942
Mark Mendellc4701932015-04-10 13:18:51 -04001943void HInstruction::RemoveEnvironmentUsers() {
1944 for (HUseIterator<HEnvironment*> use_it(GetEnvUses()); !use_it.Done(); use_it.Advance()) {
1945 HUseListNode<HEnvironment*>* user_node = use_it.Current();
1946 HEnvironment* user = user_node->GetUser();
1947 user->SetRawEnvAt(user_node->GetIndex(), nullptr);
1948 }
1949 env_uses_.Clear();
1950}
1951
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001952} // namespace art