blob: 3e137ff17ca894529b1b1bbf9434efdbae19835c [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.
David Brazdil9bc43612015-11-05 21:25:24 +0000338 //
David Brazdilffee3d32015-07-06 11:48:53 +0100339 // Note that catch blocks with normal-flow predecessors cannot begin with
David Brazdil9bc43612015-11-05 21:25:24 +0000340 // a move-exception instruction, as guaranteed by the verifier. However,
341 // trivially dead predecessors are ignored by the verifier and such code
342 // has not been removed at this stage. We therefore ignore the assumption
343 // and rely on GraphChecker to enforce it after initial DCE is run (b/25492628).
344 HBasicBlock* normal_block = catch_block->SplitCatchBlockAfterMoveException();
345 if (normal_block == nullptr) {
346 // Catch block is either empty or only contains a move-exception. It must
347 // therefore be dead and will be removed during initial DCE. Do nothing.
348 DCHECK(!catch_block->EndsWithControlFlowInstruction());
349 } else {
350 // Catch block was split. Re-link normal-flow edges to the new block.
351 for (size_t j = 0; j < catch_block->GetPredecessors().size(); ++j) {
352 if (!CheckIfPredecessorAtIsExceptional(*catch_block, j)) {
353 catch_block->GetPredecessors()[j]->ReplaceSuccessor(catch_block, normal_block);
354 --j;
355 }
David Brazdilffee3d32015-07-06 11:48:53 +0100356 }
357 }
358 }
359 }
360}
361
362void HGraph::ComputeTryBlockInformation() {
363 // Iterate in reverse post order to propagate try membership information from
364 // predecessors to their successors.
365 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
366 HBasicBlock* block = it.Current();
367 if (block->IsEntryBlock() || block->IsCatchBlock()) {
368 // Catch blocks after simplification have only exceptional predecessors
369 // and hence are never in tries.
370 continue;
371 }
372
373 // Infer try membership from the first predecessor. Having simplified loops,
374 // the first predecessor can never be a back edge and therefore it must have
375 // been visited already and had its try membership set.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100376 HBasicBlock* first_predecessor = block->GetPredecessors()[0];
David Brazdilffee3d32015-07-06 11:48:53 +0100377 DCHECK(!block->IsLoopHeader() || !block->GetLoopInformation()->IsBackEdge(*first_predecessor));
David Brazdilec16f792015-08-19 15:04:01 +0100378 const HTryBoundary* try_entry = first_predecessor->ComputeTryEntryOfSuccessors();
David Brazdilfb552d72015-11-02 20:24:24 +0000379 if (try_entry != nullptr) {
David Brazdilec16f792015-08-19 15:04:01 +0100380 block->SetTryCatchInformation(new (arena_) TryCatchInformation(*try_entry));
381 }
David Brazdilffee3d32015-07-06 11:48:53 +0100382 }
383}
384
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100385void HGraph::SimplifyCFG() {
David Brazdildb51efb2015-11-06 01:36:20 +0000386// Simplify the CFG for future analysis, and code generation:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100387 // (1): Split critical edges.
David Brazdildb51efb2015-11-06 01:36:20 +0000388 // (2): Simplify loops by having only one preheader.
Vladimir Markob7d8e8c2015-09-17 15:47:05 +0100389 // NOTE: We're appending new blocks inside the loop, so we need to use index because iterators
390 // can be invalidated. We remember the initial size to avoid iterating over the new blocks.
391 for (size_t block_id = 0u, end = blocks_.size(); block_id != end; ++block_id) {
392 HBasicBlock* block = blocks_[block_id];
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100393 if (block == nullptr) continue;
David Brazdildb51efb2015-11-06 01:36:20 +0000394 if (block->GetSuccessors().size() > 1) {
395 // Only split normal-flow edges. We cannot split exceptional edges as they
396 // are synthesized (approximate real control flow), and we do not need to
397 // anyway. Moves that would be inserted there are performed by the runtime.
398 for (size_t j = 0, e = block->NumberOfNormalSuccessors(); j < e; ++j) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100399 HBasicBlock* successor = block->GetSuccessors()[j];
David Brazdilffee3d32015-07-06 11:48:53 +0100400 DCHECK(!successor->IsCatchBlock());
David Brazdildb51efb2015-11-06 01:36:20 +0000401 if (successor == exit_block_) {
402 // Throw->TryBoundary->Exit. Special case which we do not want to split
403 // because Goto->Exit is not allowed.
404 DCHECK(block->IsSingleTryBoundary());
405 DCHECK(block->GetSinglePredecessor()->GetLastInstruction()->IsThrow());
406 } else if (successor->GetPredecessors().size() > 1) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100407 SplitCriticalEdge(block, successor);
408 --j;
409 }
410 }
411 }
412 if (block->IsLoopHeader()) {
413 SimplifyLoop(block);
414 }
415 }
416}
417
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000418bool HGraph::AnalyzeNaturalLoops() const {
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100419 // Order does not matter.
420 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
421 HBasicBlock* block = it.Current();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 if (block->IsLoopHeader()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100423 if (block->IsCatchBlock()) {
424 // TODO: Dealing with exceptional back edges could be tricky because
425 // they only approximate the real control flow. Bail out for now.
426 return false;
427 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100428 HLoopInformation* info = block->GetLoopInformation();
429 if (!info->Populate()) {
430 // Abort if the loop is non natural. We currently bailout in such cases.
431 return false;
432 }
433 }
434 }
435 return true;
436}
437
David Brazdil8d5b8b22015-03-24 10:51:52 +0000438void HGraph::InsertConstant(HConstant* constant) {
439 // New constants are inserted before the final control-flow instruction
440 // of the graph, or at its end if called from the graph builder.
441 if (entry_block_->EndsWithControlFlowInstruction()) {
442 entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction());
David Brazdil46e2a392015-03-16 17:31:52 +0000443 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000444 entry_block_->AddInstruction(constant);
David Brazdil46e2a392015-03-16 17:31:52 +0000445 }
446}
447
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600448HNullConstant* HGraph::GetNullConstant(uint32_t dex_pc) {
Nicolas Geoffray18e68732015-06-17 23:09:05 +0100449 // For simplicity, don't bother reviving the cached null constant if it is
450 // not null and not in a block. Otherwise, we need to clear the instruction
451 // id and/or any invariants the graph is assuming when adding new instructions.
452 if ((cached_null_constant_ == nullptr) || (cached_null_constant_->GetBlock() == nullptr)) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600453 cached_null_constant_ = new (arena_) HNullConstant(dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000454 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000455 }
456 return cached_null_constant_;
457}
458
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100459HCurrentMethod* HGraph::GetCurrentMethod() {
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100460 // For simplicity, don't bother reviving the cached current method if it is
461 // not null and not in a block. Otherwise, we need to clear the instruction
462 // id and/or any invariants the graph is assuming when adding new instructions.
463 if ((cached_current_method_ == nullptr) || (cached_current_method_->GetBlock() == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700464 cached_current_method_ = new (arena_) HCurrentMethod(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600465 Is64BitInstructionSet(instruction_set_) ? Primitive::kPrimLong : Primitive::kPrimInt,
466 entry_block_->GetDexPc());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100467 if (entry_block_->GetFirstInstruction() == nullptr) {
468 entry_block_->AddInstruction(cached_current_method_);
469 } else {
470 entry_block_->InsertInstructionBefore(
471 cached_current_method_, entry_block_->GetFirstInstruction());
472 }
473 }
474 return cached_current_method_;
475}
476
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600477HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000478 switch (type) {
479 case Primitive::Type::kPrimBoolean:
480 DCHECK(IsUint<1>(value));
481 FALLTHROUGH_INTENDED;
482 case Primitive::Type::kPrimByte:
483 case Primitive::Type::kPrimChar:
484 case Primitive::Type::kPrimShort:
485 case Primitive::Type::kPrimInt:
486 DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600487 return GetIntConstant(static_cast<int32_t>(value), dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000488
489 case Primitive::Type::kPrimLong:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600490 return GetLongConstant(value, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000491
492 default:
493 LOG(FATAL) << "Unsupported constant type";
494 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000495 }
David Brazdil46e2a392015-03-16 17:31:52 +0000496}
497
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000498void HGraph::CacheFloatConstant(HFloatConstant* constant) {
499 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
500 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
501 cached_float_constants_.Overwrite(value, constant);
502}
503
504void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
505 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
506 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
507 cached_double_constants_.Overwrite(value, constant);
508}
509
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000510void HLoopInformation::Add(HBasicBlock* block) {
511 blocks_.SetBit(block->GetBlockId());
512}
513
David Brazdil46e2a392015-03-16 17:31:52 +0000514void HLoopInformation::Remove(HBasicBlock* block) {
515 blocks_.ClearBit(block->GetBlockId());
516}
517
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100518void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
519 if (blocks_.IsBitSet(block->GetBlockId())) {
520 return;
521 }
522
523 blocks_.SetBit(block->GetBlockId());
524 block->SetInLoop(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000525 for (HBasicBlock* predecessor : block->GetPredecessors()) {
526 PopulateRecursive(predecessor);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100527 }
528}
529
530bool HLoopInformation::Populate() {
David Brazdila4b8c212015-05-07 09:59:30 +0100531 DCHECK_EQ(blocks_.NumSetBits(), 0u) << "Loop information has already been populated";
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100532 for (HBasicBlock* back_edge : GetBackEdges()) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100533 DCHECK(back_edge->GetDominator() != nullptr);
534 if (!header_->Dominates(back_edge)) {
535 // This loop is not natural. Do not bother going further.
536 return false;
537 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100538
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100539 // Populate this loop: starting with the back edge, recursively add predecessors
540 // that are not already part of that loop. Set the header as part of the loop
541 // to end the recursion.
542 // This is a recursive implementation of the algorithm described in
543 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
544 blocks_.SetBit(header_->GetBlockId());
545 PopulateRecursive(back_edge);
546 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100547 return true;
548}
549
David Brazdila4b8c212015-05-07 09:59:30 +0100550void HLoopInformation::Update() {
551 HGraph* graph = header_->GetGraph();
552 for (uint32_t id : blocks_.Indexes()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100553 HBasicBlock* block = graph->GetBlocks()[id];
David Brazdila4b8c212015-05-07 09:59:30 +0100554 // Reset loop information of non-header blocks inside the loop, except
555 // members of inner nested loops because those should already have been
556 // updated by their own LoopInformation.
557 if (block->GetLoopInformation() == this && block != header_) {
558 block->SetLoopInformation(nullptr);
559 }
560 }
561 blocks_.ClearAllBits();
562
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100563 if (back_edges_.empty()) {
David Brazdila4b8c212015-05-07 09:59:30 +0100564 // The loop has been dismantled, delete its suspend check and remove info
565 // from the header.
566 DCHECK(HasSuspendCheck());
567 header_->RemoveInstruction(suspend_check_);
568 header_->SetLoopInformation(nullptr);
569 header_ = nullptr;
570 suspend_check_ = nullptr;
571 } else {
572 if (kIsDebugBuild) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100573 for (HBasicBlock* back_edge : back_edges_) {
574 DCHECK(header_->Dominates(back_edge));
David Brazdila4b8c212015-05-07 09:59:30 +0100575 }
576 }
577 // This loop still has reachable back edges. Repopulate the list of blocks.
578 bool populate_successful = Populate();
579 DCHECK(populate_successful);
580 }
581}
582
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100583HBasicBlock* HLoopInformation::GetPreHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100584 return header_->GetDominator();
585}
586
587bool HLoopInformation::Contains(const HBasicBlock& block) const {
588 return blocks_.IsBitSet(block.GetBlockId());
589}
590
591bool HLoopInformation::IsIn(const HLoopInformation& other) const {
592 return other.blocks_.IsBitSet(header_->GetBlockId());
593}
594
Aart Bik73f1f3b2015-10-28 15:28:08 -0700595bool HLoopInformation::IsLoopInvariant(HInstruction* instruction, bool must_dominate) const {
596 HLoopInformation* other_loop = instruction->GetBlock()->GetLoopInformation();
597 if (other_loop != this && (other_loop == nullptr || !other_loop->IsIn(*this))) {
598 if (must_dominate) {
599 return instruction->GetBlock()->Dominates(GetHeader());
600 }
601 return true;
602 }
603 return false;
604}
605
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100606size_t HLoopInformation::GetLifetimeEnd() const {
607 size_t last_position = 0;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100608 for (HBasicBlock* back_edge : GetBackEdges()) {
609 last_position = std::max(back_edge->GetLifetimeEnd(), last_position);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100610 }
611 return last_position;
612}
613
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100614bool HBasicBlock::Dominates(HBasicBlock* other) const {
615 // Walk up the dominator tree from `other`, to find out if `this`
616 // is an ancestor.
617 HBasicBlock* current = other;
618 while (current != nullptr) {
619 if (current == this) {
620 return true;
621 }
622 current = current->GetDominator();
623 }
624 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100625}
626
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100627static void UpdateInputsUsers(HInstruction* instruction) {
628 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
629 instruction->InputAt(i)->AddUseAt(instruction, i);
630 }
631 // Environment should be created later.
632 DCHECK(!instruction->HasEnvironment());
633}
634
Roland Levillainccc07a92014-09-16 14:48:16 +0100635void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
636 HInstruction* replacement) {
637 DCHECK(initial->GetBlock() == this);
Mark Mendell805b3b52015-09-18 14:10:29 -0400638 if (initial->IsControlFlow()) {
639 // We can only replace a control flow instruction with another control flow instruction.
640 DCHECK(replacement->IsControlFlow());
641 DCHECK_EQ(replacement->GetId(), -1);
642 DCHECK_EQ(replacement->GetType(), Primitive::kPrimVoid);
643 DCHECK_EQ(initial->GetBlock(), this);
644 DCHECK_EQ(initial->GetType(), Primitive::kPrimVoid);
645 DCHECK(initial->GetUses().IsEmpty());
646 DCHECK(initial->GetEnvUses().IsEmpty());
647 replacement->SetBlock(this);
648 replacement->SetId(GetGraph()->GetNextInstructionId());
649 instructions_.InsertInstructionBefore(replacement, initial);
650 UpdateInputsUsers(replacement);
651 } else {
652 InsertInstructionBefore(replacement, initial);
653 initial->ReplaceWith(replacement);
654 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100655 RemoveInstruction(initial);
656}
657
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100658static void Add(HInstructionList* instruction_list,
659 HBasicBlock* block,
660 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000661 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000662 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100663 instruction->SetBlock(block);
664 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100665 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100666 instruction_list->AddInstruction(instruction);
667}
668
669void HBasicBlock::AddInstruction(HInstruction* instruction) {
670 Add(&instructions_, this, instruction);
671}
672
673void HBasicBlock::AddPhi(HPhi* phi) {
674 Add(&phis_, this, phi);
675}
676
David Brazdilc3d743f2015-04-22 13:40:50 +0100677void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
678 DCHECK(!cursor->IsPhi());
679 DCHECK(!instruction->IsPhi());
680 DCHECK_EQ(instruction->GetId(), -1);
681 DCHECK_NE(cursor->GetId(), -1);
682 DCHECK_EQ(cursor->GetBlock(), this);
683 DCHECK(!instruction->IsControlFlow());
684 instruction->SetBlock(this);
685 instruction->SetId(GetGraph()->GetNextInstructionId());
686 UpdateInputsUsers(instruction);
687 instructions_.InsertInstructionBefore(instruction, cursor);
688}
689
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100690void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
691 DCHECK(!cursor->IsPhi());
692 DCHECK(!instruction->IsPhi());
693 DCHECK_EQ(instruction->GetId(), -1);
694 DCHECK_NE(cursor->GetId(), -1);
695 DCHECK_EQ(cursor->GetBlock(), this);
696 DCHECK(!instruction->IsControlFlow());
697 DCHECK(!cursor->IsControlFlow());
698 instruction->SetBlock(this);
699 instruction->SetId(GetGraph()->GetNextInstructionId());
700 UpdateInputsUsers(instruction);
701 instructions_.InsertInstructionAfter(instruction, cursor);
702}
703
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100704void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
705 DCHECK_EQ(phi->GetId(), -1);
706 DCHECK_NE(cursor->GetId(), -1);
707 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100708 phi->SetBlock(this);
709 phi->SetId(GetGraph()->GetNextInstructionId());
710 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100711 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100712}
713
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100714static void Remove(HInstructionList* instruction_list,
715 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000716 HInstruction* instruction,
717 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100718 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100719 instruction->SetBlock(nullptr);
720 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000721 if (ensure_safety) {
722 DCHECK(instruction->GetUses().IsEmpty());
723 DCHECK(instruction->GetEnvUses().IsEmpty());
724 RemoveAsUser(instruction);
725 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100726}
727
David Brazdil1abb4192015-02-17 18:33:36 +0000728void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100729 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000730 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100731}
732
David Brazdil1abb4192015-02-17 18:33:36 +0000733void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
734 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100735}
736
David Brazdilc7508e92015-04-27 13:28:57 +0100737void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
738 if (instruction->IsPhi()) {
739 RemovePhi(instruction->AsPhi(), ensure_safety);
740 } else {
741 RemoveInstruction(instruction, ensure_safety);
742 }
743}
744
Vladimir Marko71bf8092015-09-15 15:33:14 +0100745void HEnvironment::CopyFrom(const ArenaVector<HInstruction*>& locals) {
746 for (size_t i = 0; i < locals.size(); i++) {
747 HInstruction* instruction = locals[i];
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +0100748 SetRawEnvAt(i, instruction);
749 if (instruction != nullptr) {
750 instruction->AddEnvUseAt(this, i);
751 }
752 }
753}
754
David Brazdiled596192015-01-23 10:39:45 +0000755void HEnvironment::CopyFrom(HEnvironment* env) {
756 for (size_t i = 0; i < env->Size(); i++) {
757 HInstruction* instruction = env->GetInstructionAt(i);
758 SetRawEnvAt(i, instruction);
759 if (instruction != nullptr) {
760 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100761 }
David Brazdiled596192015-01-23 10:39:45 +0000762 }
763}
764
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700765void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
766 HBasicBlock* loop_header) {
767 DCHECK(loop_header->IsLoopHeader());
768 for (size_t i = 0; i < env->Size(); i++) {
769 HInstruction* instruction = env->GetInstructionAt(i);
770 SetRawEnvAt(i, instruction);
771 if (instruction == nullptr) {
772 continue;
773 }
774 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
775 // At the end of the loop pre-header, the corresponding value for instruction
776 // is the first input of the phi.
777 HInstruction* initial = instruction->AsPhi()->InputAt(0);
778 DCHECK(initial->GetBlock()->Dominates(loop_header));
779 SetRawEnvAt(i, initial);
780 initial->AddEnvUseAt(this, i);
781 } else {
782 instruction->AddEnvUseAt(this, i);
783 }
784 }
785}
786
David Brazdil1abb4192015-02-17 18:33:36 +0000787void HEnvironment::RemoveAsUserOfInput(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100788 const HUserRecord<HEnvironment*>& user_record = vregs_[index];
David Brazdil1abb4192015-02-17 18:33:36 +0000789 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100790}
791
Calin Juravle77520bc2015-01-12 18:45:46 +0000792HInstruction* HInstruction::GetNextDisregardingMoves() const {
793 HInstruction* next = GetNext();
794 while (next != nullptr && next->IsParallelMove()) {
795 next = next->GetNext();
796 }
797 return next;
798}
799
800HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
801 HInstruction* previous = GetPrevious();
802 while (previous != nullptr && previous->IsParallelMove()) {
803 previous = previous->GetPrevious();
804 }
805 return previous;
806}
807
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100808void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000809 if (first_instruction_ == nullptr) {
810 DCHECK(last_instruction_ == nullptr);
811 first_instruction_ = last_instruction_ = instruction;
812 } else {
813 last_instruction_->next_ = instruction;
814 instruction->previous_ = last_instruction_;
815 last_instruction_ = instruction;
816 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000817}
818
David Brazdilc3d743f2015-04-22 13:40:50 +0100819void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
820 DCHECK(Contains(cursor));
821 if (cursor == first_instruction_) {
822 cursor->previous_ = instruction;
823 instruction->next_ = cursor;
824 first_instruction_ = instruction;
825 } else {
826 instruction->previous_ = cursor->previous_;
827 instruction->next_ = cursor;
828 cursor->previous_ = instruction;
829 instruction->previous_->next_ = instruction;
830 }
831}
832
833void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
834 DCHECK(Contains(cursor));
835 if (cursor == last_instruction_) {
836 cursor->next_ = instruction;
837 instruction->previous_ = cursor;
838 last_instruction_ = instruction;
839 } else {
840 instruction->next_ = cursor->next_;
841 instruction->previous_ = cursor;
842 cursor->next_ = instruction;
843 instruction->next_->previous_ = instruction;
844 }
845}
846
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100847void HInstructionList::RemoveInstruction(HInstruction* instruction) {
848 if (instruction->previous_ != nullptr) {
849 instruction->previous_->next_ = instruction->next_;
850 }
851 if (instruction->next_ != nullptr) {
852 instruction->next_->previous_ = instruction->previous_;
853 }
854 if (instruction == first_instruction_) {
855 first_instruction_ = instruction->next_;
856 }
857 if (instruction == last_instruction_) {
858 last_instruction_ = instruction->previous_;
859 }
860}
861
Roland Levillain6b469232014-09-25 10:10:38 +0100862bool HInstructionList::Contains(HInstruction* instruction) const {
863 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
864 if (it.Current() == instruction) {
865 return true;
866 }
867 }
868 return false;
869}
870
Roland Levillainccc07a92014-09-16 14:48:16 +0100871bool HInstructionList::FoundBefore(const HInstruction* instruction1,
872 const HInstruction* instruction2) const {
873 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
874 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
875 if (it.Current() == instruction1) {
876 return true;
877 }
878 if (it.Current() == instruction2) {
879 return false;
880 }
881 }
882 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
883 return true;
884}
885
Roland Levillain6c82d402014-10-13 16:10:27 +0100886bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
887 if (other_instruction == this) {
888 // An instruction does not strictly dominate itself.
889 return false;
890 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100891 HBasicBlock* block = GetBlock();
892 HBasicBlock* other_block = other_instruction->GetBlock();
893 if (block != other_block) {
894 return GetBlock()->Dominates(other_instruction->GetBlock());
895 } else {
896 // If both instructions are in the same block, ensure this
897 // instruction comes before `other_instruction`.
898 if (IsPhi()) {
899 if (!other_instruction->IsPhi()) {
900 // Phis appear before non phi-instructions so this instruction
901 // dominates `other_instruction`.
902 return true;
903 } else {
904 // There is no order among phis.
905 LOG(FATAL) << "There is no dominance between phis of a same block.";
906 return false;
907 }
908 } else {
909 // `this` is not a phi.
910 if (other_instruction->IsPhi()) {
911 // Phis appear before non phi-instructions so this instruction
912 // does not dominate `other_instruction`.
913 return false;
914 } else {
915 // Check whether this instruction comes before
916 // `other_instruction` in the instruction list.
917 return block->GetInstructions().FoundBefore(this, other_instruction);
918 }
919 }
920 }
921}
922
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100923void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100924 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000925 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
926 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100927 HInstruction* user = current->GetUser();
928 size_t input_index = current->GetIndex();
929 user->SetRawInputAt(input_index, other);
930 other->AddUseAt(user, input_index);
931 }
932
David Brazdiled596192015-01-23 10:39:45 +0000933 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
934 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100935 HEnvironment* user = current->GetUser();
936 size_t input_index = current->GetIndex();
937 user->SetRawEnvAt(input_index, other);
938 other->AddEnvUseAt(user, input_index);
939 }
940
David Brazdiled596192015-01-23 10:39:45 +0000941 uses_.Clear();
942 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100943}
944
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100945void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000946 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100947 SetRawInputAt(index, replacement);
948 replacement->AddUseAt(this, index);
949}
950
Nicolas Geoffray39468442014-09-02 15:17:15 +0100951size_t HInstruction::EnvironmentSize() const {
952 return HasEnvironment() ? environment_->Size() : 0;
953}
954
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100955void HPhi::AddInput(HInstruction* input) {
956 DCHECK(input->GetBlock() != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100957 inputs_.push_back(HUserRecord<HInstruction*>(input));
958 input->AddUseAt(this, inputs_.size() - 1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100959}
960
David Brazdil2d7352b2015-04-20 14:52:42 +0100961void HPhi::RemoveInputAt(size_t index) {
962 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100963 inputs_.erase(inputs_.begin() + index);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100964 for (size_t i = index, e = InputCount(); i < e; ++i) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100965 DCHECK_EQ(InputRecordAt(i).GetUseNode()->GetIndex(), i + 1u);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100966 InputRecordAt(i).GetUseNode()->SetIndex(i);
967 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100968}
969
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100970#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000971void H##name::Accept(HGraphVisitor* visitor) { \
972 visitor->Visit##name(this); \
973}
974
975FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
976
977#undef DEFINE_ACCEPT
978
979void HGraphVisitor::VisitInsertionOrder() {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100980 const ArenaVector<HBasicBlock*>& blocks = graph_->GetBlocks();
981 for (HBasicBlock* block : blocks) {
David Brazdil46e2a392015-03-16 17:31:52 +0000982 if (block != nullptr) {
983 VisitBasicBlock(block);
984 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000985 }
986}
987
Roland Levillain633021e2014-10-01 14:12:25 +0100988void HGraphVisitor::VisitReversePostOrder() {
989 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
990 VisitBasicBlock(it.Current());
991 }
992}
993
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000994void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100995 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100996 it.Current()->Accept(this);
997 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100998 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000999 it.Current()->Accept(this);
1000 }
1001}
1002
Mark Mendelle82549b2015-05-06 10:55:34 -04001003HConstant* HTypeConversion::TryStaticEvaluation() const {
1004 HGraph* graph = GetBlock()->GetGraph();
1005 if (GetInput()->IsIntConstant()) {
1006 int32_t value = GetInput()->AsIntConstant()->GetValue();
1007 switch (GetResultType()) {
1008 case Primitive::kPrimLong:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001009 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001010 case Primitive::kPrimFloat:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001011 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001012 case Primitive::kPrimDouble:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001013 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001014 default:
1015 return nullptr;
1016 }
1017 } else if (GetInput()->IsLongConstant()) {
1018 int64_t value = GetInput()->AsLongConstant()->GetValue();
1019 switch (GetResultType()) {
1020 case Primitive::kPrimInt:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001021 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001022 case Primitive::kPrimFloat:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001023 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001024 case Primitive::kPrimDouble:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001025 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001026 default:
1027 return nullptr;
1028 }
1029 } else if (GetInput()->IsFloatConstant()) {
1030 float value = GetInput()->AsFloatConstant()->GetValue();
1031 switch (GetResultType()) {
1032 case Primitive::kPrimInt:
1033 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001034 return graph->GetIntConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001035 if (value >= kPrimIntMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001036 return graph->GetIntConstant(kPrimIntMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001037 if (value <= kPrimIntMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001038 return graph->GetIntConstant(kPrimIntMin, GetDexPc());
1039 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001040 case Primitive::kPrimLong:
1041 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001042 return graph->GetLongConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001043 if (value >= kPrimLongMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001044 return graph->GetLongConstant(kPrimLongMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001045 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001046 return graph->GetLongConstant(kPrimLongMin, GetDexPc());
1047 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001048 case Primitive::kPrimDouble:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001049 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001050 default:
1051 return nullptr;
1052 }
1053 } else if (GetInput()->IsDoubleConstant()) {
1054 double value = GetInput()->AsDoubleConstant()->GetValue();
1055 switch (GetResultType()) {
1056 case Primitive::kPrimInt:
1057 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001058 return graph->GetIntConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001059 if (value >= kPrimIntMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001060 return graph->GetIntConstant(kPrimIntMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001061 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001062 return graph->GetIntConstant(kPrimIntMin, GetDexPc());
1063 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001064 case Primitive::kPrimLong:
1065 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001066 return graph->GetLongConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001067 if (value >= kPrimLongMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001068 return graph->GetLongConstant(kPrimLongMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001069 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001070 return graph->GetLongConstant(kPrimLongMin, GetDexPc());
1071 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001072 case Primitive::kPrimFloat:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001073 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001074 default:
1075 return nullptr;
1076 }
1077 }
1078 return nullptr;
1079}
1080
Roland Levillain9240d6a2014-10-20 16:47:04 +01001081HConstant* HUnaryOperation::TryStaticEvaluation() const {
1082 if (GetInput()->IsIntConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001083 return Evaluate(GetInput()->AsIntConstant());
Roland Levillain9240d6a2014-10-20 16:47:04 +01001084 } else if (GetInput()->IsLongConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001085 return Evaluate(GetInput()->AsLongConstant());
Roland Levillain9240d6a2014-10-20 16:47:04 +01001086 }
1087 return nullptr;
1088}
1089
1090HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain9867bc72015-08-05 10:21:34 +01001091 if (GetLeft()->IsIntConstant()) {
1092 if (GetRight()->IsIntConstant()) {
1093 return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsIntConstant());
1094 } else if (GetRight()->IsLongConstant()) {
1095 return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsLongConstant());
1096 }
1097 } else if (GetLeft()->IsLongConstant()) {
1098 if (GetRight()->IsIntConstant()) {
1099 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsIntConstant());
1100 } else if (GetRight()->IsLongConstant()) {
1101 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsLongConstant());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +00001102 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001103 }
1104 return nullptr;
1105}
Dave Allison20dfc792014-06-16 20:44:29 -07001106
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001107HConstant* HBinaryOperation::GetConstantRight() const {
1108 if (GetRight()->IsConstant()) {
1109 return GetRight()->AsConstant();
1110 } else if (IsCommutative() && GetLeft()->IsConstant()) {
1111 return GetLeft()->AsConstant();
1112 } else {
1113 return nullptr;
1114 }
1115}
1116
1117// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001118// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001119HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
1120 HInstruction* most_constant_right = GetConstantRight();
1121 if (most_constant_right == nullptr) {
1122 return nullptr;
1123 } else if (most_constant_right == GetLeft()) {
1124 return GetRight();
1125 } else {
1126 return GetLeft();
1127 }
1128}
1129
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001130bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
1131 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001132}
1133
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001134bool HInstruction::Equals(HInstruction* other) const {
1135 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001136 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001137 if (!InstructionDataEquals(other)) return false;
1138 if (GetType() != other->GetType()) return false;
1139 if (InputCount() != other->InputCount()) return false;
1140
1141 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1142 if (InputAt(i) != other->InputAt(i)) return false;
1143 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001144 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001145 return true;
1146}
1147
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001148std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
1149#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
1150 switch (rhs) {
1151 FOR_EACH_INSTRUCTION(DECLARE_CASE)
1152 default:
1153 os << "Unknown instruction kind " << static_cast<int>(rhs);
1154 break;
1155 }
1156#undef DECLARE_CASE
1157 return os;
1158}
1159
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001160void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001161 next_->previous_ = previous_;
1162 if (previous_ != nullptr) {
1163 previous_->next_ = next_;
1164 }
1165 if (block_->instructions_.first_instruction_ == this) {
1166 block_->instructions_.first_instruction_ = next_;
1167 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001168 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001169
1170 previous_ = cursor->previous_;
1171 if (previous_ != nullptr) {
1172 previous_->next_ = this;
1173 }
1174 next_ = cursor;
1175 cursor->previous_ = this;
1176 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001177
1178 if (block_->instructions_.first_instruction_ == cursor) {
1179 block_->instructions_.first_instruction_ = this;
1180 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001181}
1182
David Brazdilfc6a86a2015-06-26 10:33:45 +00001183HBasicBlock* HBasicBlock::SplitBefore(HInstruction* cursor) {
David Brazdil9bc43612015-11-05 21:25:24 +00001184 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented.";
David Brazdilfc6a86a2015-06-26 10:33:45 +00001185 DCHECK_EQ(cursor->GetBlock(), this);
1186
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001187 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(),
1188 cursor->GetDexPc());
David Brazdilfc6a86a2015-06-26 10:33:45 +00001189 new_block->instructions_.first_instruction_ = cursor;
1190 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1191 instructions_.last_instruction_ = cursor->previous_;
1192 if (cursor->previous_ == nullptr) {
1193 instructions_.first_instruction_ = nullptr;
1194 } else {
1195 cursor->previous_->next_ = nullptr;
1196 cursor->previous_ = nullptr;
1197 }
1198
1199 new_block->instructions_.SetBlockOfInstructions(new_block);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001200 AddInstruction(new (GetGraph()->GetArena()) HGoto(new_block->GetDexPc()));
David Brazdilfc6a86a2015-06-26 10:33:45 +00001201
Vladimir Marko60584552015-09-03 13:35:12 +00001202 for (HBasicBlock* successor : GetSuccessors()) {
1203 new_block->successors_.push_back(successor);
1204 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001205 }
Vladimir Marko60584552015-09-03 13:35:12 +00001206 successors_.clear();
David Brazdilfc6a86a2015-06-26 10:33:45 +00001207 AddSuccessor(new_block);
1208
David Brazdil56e1acc2015-06-30 15:41:36 +01001209 GetGraph()->AddBlock(new_block);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001210 return new_block;
1211}
1212
David Brazdild7558da2015-09-22 13:04:14 +01001213HBasicBlock* HBasicBlock::CreateImmediateDominator() {
David Brazdil9bc43612015-11-05 21:25:24 +00001214 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented.";
David Brazdild7558da2015-09-22 13:04:14 +01001215 DCHECK(!IsCatchBlock()) << "Support for updating try/catch information not implemented.";
1216
1217 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
1218
1219 for (HBasicBlock* predecessor : GetPredecessors()) {
1220 new_block->predecessors_.push_back(predecessor);
1221 predecessor->successors_[predecessor->GetSuccessorIndexOf(this)] = new_block;
1222 }
1223 predecessors_.clear();
1224 AddPredecessor(new_block);
1225
1226 GetGraph()->AddBlock(new_block);
1227 return new_block;
1228}
1229
David Brazdil9bc43612015-11-05 21:25:24 +00001230HBasicBlock* HBasicBlock::SplitCatchBlockAfterMoveException() {
1231 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented.";
1232 DCHECK(IsCatchBlock()) << "This method is intended for catch blocks only.";
1233
1234 HInstruction* first_insn = GetFirstInstruction();
1235 HInstruction* split_before = nullptr;
1236
1237 if (first_insn != nullptr && first_insn->IsLoadException()) {
1238 // Catch block starts with a LoadException. Split the block after
1239 // the StoreLocal and ClearException which must come after the load.
1240 DCHECK(first_insn->GetNext()->IsStoreLocal());
1241 DCHECK(first_insn->GetNext()->GetNext()->IsClearException());
1242 split_before = first_insn->GetNext()->GetNext()->GetNext();
1243 } else {
1244 // Catch block does not load the exception. Split at the beginning
1245 // to create an empty catch block.
1246 split_before = first_insn;
1247 }
1248
1249 if (split_before == nullptr) {
1250 // Catch block has no instructions after the split point (must be dead).
1251 // Do not split it but rather signal error by returning nullptr.
1252 return nullptr;
1253 } else {
1254 return SplitBefore(split_before);
1255 }
1256}
1257
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001258HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
1259 DCHECK(!cursor->IsControlFlow());
1260 DCHECK_NE(instructions_.last_instruction_, cursor);
1261 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001262
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001263 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
1264 new_block->instructions_.first_instruction_ = cursor->GetNext();
1265 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1266 cursor->next_->previous_ = nullptr;
1267 cursor->next_ = nullptr;
1268 instructions_.last_instruction_ = cursor;
1269
1270 new_block->instructions_.SetBlockOfInstructions(new_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001271 for (HBasicBlock* successor : GetSuccessors()) {
1272 new_block->successors_.push_back(successor);
1273 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001274 }
Vladimir Marko60584552015-09-03 13:35:12 +00001275 successors_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001276
Vladimir Marko60584552015-09-03 13:35:12 +00001277 for (HBasicBlock* dominated : GetDominatedBlocks()) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001278 dominated->dominator_ = new_block;
Vladimir Marko60584552015-09-03 13:35:12 +00001279 new_block->dominated_blocks_.push_back(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001280 }
Vladimir Marko60584552015-09-03 13:35:12 +00001281 dominated_blocks_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001282 return new_block;
1283}
1284
David Brazdilec16f792015-08-19 15:04:01 +01001285const HTryBoundary* HBasicBlock::ComputeTryEntryOfSuccessors() const {
David Brazdilffee3d32015-07-06 11:48:53 +01001286 if (EndsWithTryBoundary()) {
1287 HTryBoundary* try_boundary = GetLastInstruction()->AsTryBoundary();
1288 if (try_boundary->IsEntry()) {
David Brazdilec16f792015-08-19 15:04:01 +01001289 DCHECK(!IsTryBlock());
David Brazdilffee3d32015-07-06 11:48:53 +01001290 return try_boundary;
1291 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001292 DCHECK(IsTryBlock());
1293 DCHECK(try_catch_information_->GetTryEntry().HasSameExceptionHandlersAs(*try_boundary));
David Brazdilffee3d32015-07-06 11:48:53 +01001294 return nullptr;
1295 }
David Brazdilec16f792015-08-19 15:04:01 +01001296 } else if (IsTryBlock()) {
1297 return &try_catch_information_->GetTryEntry();
David Brazdilffee3d32015-07-06 11:48:53 +01001298 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001299 return nullptr;
David Brazdilffee3d32015-07-06 11:48:53 +01001300 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00001301}
1302
David Brazdild7558da2015-09-22 13:04:14 +01001303bool HBasicBlock::HasThrowingInstructions() const {
1304 for (HInstructionIterator it(GetInstructions()); !it.Done(); it.Advance()) {
1305 if (it.Current()->CanThrow()) {
1306 return true;
1307 }
1308 }
1309 return false;
1310}
1311
David Brazdilfc6a86a2015-06-26 10:33:45 +00001312static bool HasOnlyOneInstruction(const HBasicBlock& block) {
1313 return block.GetPhis().IsEmpty()
1314 && !block.GetInstructions().IsEmpty()
1315 && block.GetFirstInstruction() == block.GetLastInstruction();
1316}
1317
David Brazdil46e2a392015-03-16 17:31:52 +00001318bool HBasicBlock::IsSingleGoto() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001319 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsGoto();
1320}
1321
1322bool HBasicBlock::IsSingleTryBoundary() const {
1323 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsTryBoundary();
David Brazdil46e2a392015-03-16 17:31:52 +00001324}
1325
David Brazdil8d5b8b22015-03-24 10:51:52 +00001326bool HBasicBlock::EndsWithControlFlowInstruction() const {
1327 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
1328}
1329
David Brazdilb2bd1c52015-03-25 11:17:37 +00001330bool HBasicBlock::EndsWithIf() const {
1331 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
1332}
1333
David Brazdilffee3d32015-07-06 11:48:53 +01001334bool HBasicBlock::EndsWithTryBoundary() const {
1335 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsTryBoundary();
1336}
1337
David Brazdilb2bd1c52015-03-25 11:17:37 +00001338bool HBasicBlock::HasSinglePhi() const {
1339 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
1340}
1341
David Brazdilffee3d32015-07-06 11:48:53 +01001342bool HTryBoundary::HasSameExceptionHandlersAs(const HTryBoundary& other) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001343 if (GetBlock()->GetSuccessors().size() != other.GetBlock()->GetSuccessors().size()) {
David Brazdilffee3d32015-07-06 11:48:53 +01001344 return false;
1345 }
1346
David Brazdilb618ade2015-07-29 10:31:29 +01001347 // Exception handlers need to be stored in the same order.
1348 for (HExceptionHandlerIterator it1(*this), it2(other);
1349 !it1.Done();
1350 it1.Advance(), it2.Advance()) {
1351 DCHECK(!it2.Done());
1352 if (it1.Current() != it2.Current()) {
David Brazdilffee3d32015-07-06 11:48:53 +01001353 return false;
1354 }
1355 }
1356 return true;
1357}
1358
David Brazdil2d7352b2015-04-20 14:52:42 +01001359size_t HInstructionList::CountSize() const {
1360 size_t size = 0;
1361 HInstruction* current = first_instruction_;
1362 for (; current != nullptr; current = current->GetNext()) {
1363 size++;
1364 }
1365 return size;
1366}
1367
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001368void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
1369 for (HInstruction* current = first_instruction_;
1370 current != nullptr;
1371 current = current->GetNext()) {
1372 current->SetBlock(block);
1373 }
1374}
1375
1376void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
1377 DCHECK(Contains(cursor));
1378 if (!instruction_list.IsEmpty()) {
1379 if (cursor == last_instruction_) {
1380 last_instruction_ = instruction_list.last_instruction_;
1381 } else {
1382 cursor->next_->previous_ = instruction_list.last_instruction_;
1383 }
1384 instruction_list.last_instruction_->next_ = cursor->next_;
1385 cursor->next_ = instruction_list.first_instruction_;
1386 instruction_list.first_instruction_->previous_ = cursor;
1387 }
1388}
1389
1390void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +00001391 if (IsEmpty()) {
1392 first_instruction_ = instruction_list.first_instruction_;
1393 last_instruction_ = instruction_list.last_instruction_;
1394 } else {
1395 AddAfter(last_instruction_, instruction_list);
1396 }
1397}
1398
David Brazdil2d7352b2015-04-20 14:52:42 +01001399void HBasicBlock::DisconnectAndDelete() {
1400 // Dominators must be removed after all the blocks they dominate. This way
1401 // a loop header is removed last, a requirement for correct loop information
1402 // iteration.
Vladimir Marko60584552015-09-03 13:35:12 +00001403 DCHECK(dominated_blocks_.empty());
David Brazdil46e2a392015-03-16 17:31:52 +00001404
David Brazdil2d7352b2015-04-20 14:52:42 +01001405 // Remove the block from all loops it is included in.
1406 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
1407 HLoopInformation* loop_info = it.Current();
1408 loop_info->Remove(this);
1409 if (loop_info->IsBackEdge(*this)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001410 // If this was the last back edge of the loop, we deliberately leave the
1411 // loop in an inconsistent state and will fail SSAChecker unless the
1412 // entire loop is removed during the pass.
David Brazdil2d7352b2015-04-20 14:52:42 +01001413 loop_info->RemoveBackEdge(this);
1414 }
1415 }
1416
1417 // Disconnect the block from its predecessors and update their control-flow
1418 // instructions.
Vladimir Marko60584552015-09-03 13:35:12 +00001419 for (HBasicBlock* predecessor : predecessors_) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001420 HInstruction* last_instruction = predecessor->GetLastInstruction();
David Brazdil2d7352b2015-04-20 14:52:42 +01001421 predecessor->RemoveSuccessor(this);
Mark Mendellfe57faa2015-09-18 09:26:15 -04001422 uint32_t num_pred_successors = predecessor->GetSuccessors().size();
1423 if (num_pred_successors == 1u) {
1424 // If we have one successor after removing one, then we must have
David Brazdilfb552d72015-11-02 20:24:24 +00001425 // had an HIf or HPackedSwitch, as they have more than one successor.
1426 // Replace those with a HGoto.
1427 DCHECK(last_instruction->IsIf() || last_instruction->IsPackedSwitch());
Mark Mendellfe57faa2015-09-18 09:26:15 -04001428 predecessor->RemoveInstruction(last_instruction);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001429 predecessor->AddInstruction(new (graph_->GetArena()) HGoto(last_instruction->GetDexPc()));
Mark Mendellfe57faa2015-09-18 09:26:15 -04001430 } else if (num_pred_successors == 0u) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001431 // The predecessor has no remaining successors and therefore must be dead.
1432 // We deliberately leave it without a control-flow instruction so that the
1433 // SSAChecker fails unless it is not removed during the pass too.
Mark Mendellfe57faa2015-09-18 09:26:15 -04001434 predecessor->RemoveInstruction(last_instruction);
1435 } else {
David Brazdilfb552d72015-11-02 20:24:24 +00001436 // There are multiple successors left. This must come from a HPackedSwitch
1437 // and we are in the middle of removing the HPackedSwitch. Like above, leave
1438 // this alone, and the SSAChecker will fail if it is not removed as well.
1439 DCHECK(last_instruction->IsPackedSwitch());
David Brazdil2d7352b2015-04-20 14:52:42 +01001440 }
David Brazdil46e2a392015-03-16 17:31:52 +00001441 }
Vladimir Marko60584552015-09-03 13:35:12 +00001442 predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001443
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001444 // Disconnect the block from its successors and update their phis.
Vladimir Marko60584552015-09-03 13:35:12 +00001445 for (HBasicBlock* successor : successors_) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001446 // Delete this block from the list of predecessors.
1447 size_t this_index = successor->GetPredecessorIndexOf(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001448 successor->predecessors_.erase(successor->predecessors_.begin() + this_index);
David Brazdil2d7352b2015-04-20 14:52:42 +01001449
1450 // Check that `successor` has other predecessors, otherwise `this` is the
1451 // dominator of `successor` which violates the order DCHECKed at the top.
Vladimir Marko60584552015-09-03 13:35:12 +00001452 DCHECK(!successor->predecessors_.empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001453
David Brazdil2d7352b2015-04-20 14:52:42 +01001454 // Remove this block's entries in the successor's phis.
Vladimir Marko60584552015-09-03 13:35:12 +00001455 if (successor->predecessors_.size() == 1u) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001456 // The successor has just one predecessor left. Replace phis with the only
1457 // remaining input.
1458 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1459 HPhi* phi = phi_it.Current()->AsPhi();
1460 phi->ReplaceWith(phi->InputAt(1 - this_index));
1461 successor->RemovePhi(phi);
1462 }
1463 } else {
1464 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1465 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1466 }
1467 }
1468 }
Vladimir Marko60584552015-09-03 13:35:12 +00001469 successors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001470
1471 // Disconnect from the dominator.
1472 dominator_->RemoveDominatedBlock(this);
1473 SetDominator(nullptr);
1474
1475 // Delete from the graph. The function safely deletes remaining instructions
1476 // and updates the reverse post order.
1477 graph_->DeleteDeadBlock(this);
1478 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001479}
1480
1481void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001482 DCHECK_EQ(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00001483 DCHECK(ContainsElement(dominated_blocks_, other));
1484 DCHECK_EQ(GetSingleSuccessor(), other);
1485 DCHECK_EQ(other->GetSinglePredecessor(), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001486 DCHECK(other->GetPhis().IsEmpty());
1487
David Brazdil2d7352b2015-04-20 14:52:42 +01001488 // Move instructions from `other` to `this`.
1489 DCHECK(EndsWithControlFlowInstruction());
1490 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001491 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001492 other->instructions_.SetBlockOfInstructions(this);
1493 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001494
David Brazdil2d7352b2015-04-20 14:52:42 +01001495 // Remove `other` from the loops it is included in.
1496 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1497 HLoopInformation* loop_info = it.Current();
1498 loop_info->Remove(other);
1499 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001500 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01001501 }
1502 }
1503
1504 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00001505 successors_.clear();
1506 while (!other->successors_.empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001507 HBasicBlock* successor = other->GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001508 successor->ReplacePredecessor(other, this);
1509 }
1510
David Brazdil2d7352b2015-04-20 14:52:42 +01001511 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00001512 RemoveDominatedBlock(other);
1513 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
1514 dominated_blocks_.push_back(dominated);
David Brazdil2d7352b2015-04-20 14:52:42 +01001515 dominated->SetDominator(this);
1516 }
Vladimir Marko60584552015-09-03 13:35:12 +00001517 other->dominated_blocks_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001518 other->dominator_ = nullptr;
1519
1520 // Clear the list of predecessors of `other` in preparation of deleting it.
Vladimir Marko60584552015-09-03 13:35:12 +00001521 other->predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001522
1523 // Delete `other` from the graph. The function updates reverse post order.
1524 graph_->DeleteDeadBlock(other);
1525 other->SetGraph(nullptr);
1526}
1527
1528void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1529 DCHECK_NE(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00001530 DCHECK(GetDominatedBlocks().empty());
1531 DCHECK(GetSuccessors().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001532 DCHECK(!EndsWithControlFlowInstruction());
Vladimir Marko60584552015-09-03 13:35:12 +00001533 DCHECK(other->GetSinglePredecessor()->IsEntryBlock());
David Brazdil2d7352b2015-04-20 14:52:42 +01001534 DCHECK(other->GetPhis().IsEmpty());
1535 DCHECK(!other->IsInLoop());
1536
1537 // Move instructions from `other` to `this`.
1538 instructions_.Add(other->GetInstructions());
1539 other->instructions_.SetBlockOfInstructions(this);
1540
1541 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00001542 successors_.clear();
1543 while (!other->successors_.empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001544 HBasicBlock* successor = other->GetSuccessors()[0];
David Brazdil2d7352b2015-04-20 14:52:42 +01001545 successor->ReplacePredecessor(other, this);
1546 }
1547
1548 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00001549 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
1550 dominated_blocks_.push_back(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001551 dominated->SetDominator(this);
1552 }
Vladimir Marko60584552015-09-03 13:35:12 +00001553 other->dominated_blocks_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001554 other->dominator_ = nullptr;
1555 other->graph_ = nullptr;
1556}
1557
1558void HBasicBlock::ReplaceWith(HBasicBlock* other) {
Vladimir Marko60584552015-09-03 13:35:12 +00001559 while (!GetPredecessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001560 HBasicBlock* predecessor = GetPredecessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001561 predecessor->ReplaceSuccessor(this, other);
1562 }
Vladimir Marko60584552015-09-03 13:35:12 +00001563 while (!GetSuccessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001564 HBasicBlock* successor = GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001565 successor->ReplacePredecessor(this, other);
1566 }
Vladimir Marko60584552015-09-03 13:35:12 +00001567 for (HBasicBlock* dominated : GetDominatedBlocks()) {
1568 other->AddDominatedBlock(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001569 }
1570 GetDominator()->ReplaceDominatedBlock(this, other);
1571 other->SetDominator(GetDominator());
1572 dominator_ = nullptr;
1573 graph_ = nullptr;
1574}
1575
1576// Create space in `blocks` for adding `number_of_new_blocks` entries
1577// starting at location `at`. Blocks after `at` are moved accordingly.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001578static void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001579 size_t number_of_new_blocks,
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001580 size_t after) {
1581 DCHECK_LT(after, blocks->size());
1582 size_t old_size = blocks->size();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001583 size_t new_size = old_size + number_of_new_blocks;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001584 blocks->resize(new_size);
1585 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001586}
1587
David Brazdil2d7352b2015-04-20 14:52:42 +01001588void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1589 DCHECK_EQ(block->GetGraph(), this);
Vladimir Marko60584552015-09-03 13:35:12 +00001590 DCHECK(block->GetSuccessors().empty());
1591 DCHECK(block->GetPredecessors().empty());
1592 DCHECK(block->GetDominatedBlocks().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001593 DCHECK(block->GetDominator() == nullptr);
1594
1595 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1596 block->RemoveInstruction(it.Current());
1597 }
1598 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1599 block->RemovePhi(it.Current()->AsPhi());
1600 }
1601
David Brazdilc7af85d2015-05-26 12:05:55 +01001602 if (block->IsExitBlock()) {
1603 exit_block_ = nullptr;
1604 }
1605
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001606 RemoveElement(reverse_post_order_, block);
1607 blocks_[block->GetBlockId()] = nullptr;
David Brazdil2d7352b2015-04-20 14:52:42 +01001608}
1609
Calin Juravle2e768302015-07-28 14:41:11 +00001610HInstruction* HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
David Brazdilc7af85d2015-05-26 12:05:55 +01001611 DCHECK(HasExitBlock()) << "Unimplemented scenario";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001612 // Update the environments in this graph to have the invoke's environment
1613 // as parent.
1614 {
1615 HReversePostOrderIterator it(*this);
1616 it.Advance(); // Skip the entry block, we do not need to update the entry's suspend check.
1617 for (; !it.Done(); it.Advance()) {
1618 HBasicBlock* block = it.Current();
1619 for (HInstructionIterator instr_it(block->GetInstructions());
1620 !instr_it.Done();
1621 instr_it.Advance()) {
1622 HInstruction* current = instr_it.Current();
1623 if (current->NeedsEnvironment()) {
1624 current->GetEnvironment()->SetAndCopyParentChain(
1625 outer_graph->GetArena(), invoke->GetEnvironment());
1626 }
1627 }
1628 }
1629 }
1630 outer_graph->UpdateMaximumNumberOfOutVRegs(GetMaximumNumberOfOutVRegs());
1631 if (HasBoundsChecks()) {
1632 outer_graph->SetHasBoundsChecks(true);
1633 }
1634
Calin Juravle2e768302015-07-28 14:41:11 +00001635 HInstruction* return_value = nullptr;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001636 if (GetBlocks().size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001637 // Simple case of an entry block, a body block, and an exit block.
1638 // Put the body block's instruction into `invoke`'s block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01001639 HBasicBlock* body = GetBlocks()[1];
1640 DCHECK(GetBlocks()[0]->IsEntryBlock());
1641 DCHECK(GetBlocks()[2]->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001642 DCHECK(!body->IsExitBlock());
1643 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001644
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001645 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1646 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001647
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001648 // Replace the invoke with the return value of the inlined graph.
1649 if (last->IsReturn()) {
Calin Juravle2e768302015-07-28 14:41:11 +00001650 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001651 } else {
1652 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001653 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001654
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001655 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001656 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001657 // Need to inline multiple blocks. We split `invoke`'s block
1658 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001659 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001660 // with the second half.
1661 ArenaAllocator* allocator = outer_graph->GetArena();
1662 HBasicBlock* at = invoke->GetBlock();
1663 HBasicBlock* to = at->SplitAfter(invoke);
1664
Vladimir Markoec7802a2015-10-01 20:57:57 +01001665 HBasicBlock* first = entry_block_->GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001666 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001667 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001668 exit_block_->ReplaceWith(to);
1669
1670 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001671 // to not `HReturn` but `HGoto` instead.
Vladimir Markoec7802a2015-10-01 20:57:57 +01001672 bool returns_void = to->GetPredecessors()[0]->GetLastInstruction()->IsReturnVoid();
Vladimir Marko60584552015-09-03 13:35:12 +00001673 if (to->GetPredecessors().size() == 1) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001674 HBasicBlock* predecessor = to->GetPredecessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001675 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001676 if (!returns_void) {
1677 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001678 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001679 predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc()));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001680 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001681 } else {
1682 if (!returns_void) {
1683 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001684 return_value = new (allocator) HPhi(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001685 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()), to->GetDexPc());
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001686 to->AddPhi(return_value->AsPhi());
1687 }
Vladimir Marko60584552015-09-03 13:35:12 +00001688 for (HBasicBlock* predecessor : to->GetPredecessors()) {
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001689 HInstruction* last = predecessor->GetLastInstruction();
1690 if (!returns_void) {
1691 return_value->AsPhi()->AddInput(last->InputAt(0));
1692 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001693 predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001694 predecessor->RemoveInstruction(last);
1695 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001696 }
1697
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001698 // Update the meta information surrounding blocks:
1699 // (1) the graph they are now in,
1700 // (2) the reverse post order of that graph,
David Brazdil95177982015-10-30 12:56:58 -05001701 // (3) the potential loop information they are now in,
1702 // (4) try block membership.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001703
1704 // We don't add the entry block, the exit block, and the first block, which
1705 // has been merged with `at`.
1706 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1707
1708 // We add the `to` block.
1709 static constexpr int kNumberOfNewBlocksInCaller = 1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001710 size_t blocks_added = (reverse_post_order_.size() - kNumberOfSkippedBlocksInCallee)
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001711 + kNumberOfNewBlocksInCaller;
1712
1713 // Find the location of `at` in the outer graph's reverse post order. The new
1714 // blocks will be added after it.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001715 size_t index_of_at = IndexOfElement(outer_graph->reverse_post_order_, at);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001716 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1717
David Brazdil95177982015-10-30 12:56:58 -05001718 HLoopInformation* loop_info = at->GetLoopInformation();
1719 // Copy TryCatchInformation if `at` is a try block, not if it is a catch block.
1720 TryCatchInformation* try_catch_info = at->IsTryBlock() ? at->GetTryCatchInformation() : nullptr;
1721
1722 // Do a reverse post order of the blocks in the callee and do (1), (2), (3)
1723 // and (4) to the blocks that apply.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001724 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1725 HBasicBlock* current = it.Current();
1726 if (current != exit_block_ && current != entry_block_ && current != first) {
1727 DCHECK(!current->IsInLoop());
David Brazdil95177982015-10-30 12:56:58 -05001728 DCHECK(current->GetTryCatchInformation() == nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001729 DCHECK(current->GetGraph() == this);
1730 current->SetGraph(outer_graph);
1731 outer_graph->AddBlock(current);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001732 outer_graph->reverse_post_order_[++index_of_at] = current;
David Brazdil95177982015-10-30 12:56:58 -05001733 if (loop_info != nullptr) {
1734 current->SetLoopInformation(loop_info);
David Brazdil7d275372015-04-21 16:36:35 +01001735 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1736 loop_it.Current()->Add(current);
1737 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001738 }
David Brazdil95177982015-10-30 12:56:58 -05001739 current->SetTryCatchInformation(try_catch_info);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001740 }
1741 }
1742
David Brazdil95177982015-10-30 12:56:58 -05001743 // Do (1), (2), (3) and (4) to `to`.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001744 to->SetGraph(outer_graph);
1745 outer_graph->AddBlock(to);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001746 outer_graph->reverse_post_order_[++index_of_at] = to;
David Brazdil95177982015-10-30 12:56:58 -05001747 if (loop_info != nullptr) {
1748 to->SetLoopInformation(loop_info);
David Brazdil7d275372015-04-21 16:36:35 +01001749 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1750 loop_it.Current()->Add(to);
1751 }
David Brazdil95177982015-10-30 12:56:58 -05001752 if (loop_info->IsBackEdge(*at)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001753 // Only `to` can become a back edge, as the inlined blocks
1754 // are predecessors of `to`.
David Brazdil95177982015-10-30 12:56:58 -05001755 loop_info->ReplaceBackEdge(at, to);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001756 }
1757 }
David Brazdil95177982015-10-30 12:56:58 -05001758 to->SetTryCatchInformation(try_catch_info);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001759 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001760
David Brazdil05144f42015-04-16 15:18:00 +01001761 // Update the next instruction id of the outer graph, so that instructions
1762 // added later get bigger ids than those in the inner graph.
1763 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1764
1765 // Walk over the entry block and:
1766 // - Move constants from the entry block to the outer_graph's entry block,
1767 // - Replace HParameterValue instructions with their real value.
1768 // - Remove suspend checks, that hold an environment.
1769 // We must do this after the other blocks have been inlined, otherwise ids of
1770 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001771 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001772 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1773 HInstruction* current = it.Current();
Calin Juravle214bbcd2015-10-20 14:54:07 +01001774 HInstruction* replacement = nullptr;
David Brazdil05144f42015-04-16 15:18:00 +01001775 if (current->IsNullConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001776 replacement = outer_graph->GetNullConstant(current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001777 } else if (current->IsIntConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001778 replacement = outer_graph->GetIntConstant(
1779 current->AsIntConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001780 } else if (current->IsLongConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001781 replacement = outer_graph->GetLongConstant(
1782 current->AsLongConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001783 } else if (current->IsFloatConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001784 replacement = outer_graph->GetFloatConstant(
1785 current->AsFloatConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001786 } else if (current->IsDoubleConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001787 replacement = outer_graph->GetDoubleConstant(
1788 current->AsDoubleConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001789 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001790 if (kIsDebugBuild
1791 && invoke->IsInvokeStaticOrDirect()
1792 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1793 // Ensure we do not use the last input of `invoke`, as it
1794 // contains a clinit check which is not an actual argument.
1795 size_t last_input_index = invoke->InputCount() - 1;
1796 DCHECK(parameter_index != last_input_index);
1797 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01001798 replacement = invoke->InputAt(parameter_index++);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001799 } else if (current->IsCurrentMethod()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001800 replacement = outer_graph->GetCurrentMethod();
David Brazdil05144f42015-04-16 15:18:00 +01001801 } else {
1802 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1803 entry_block_->RemoveInstruction(current);
1804 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01001805 if (replacement != nullptr) {
1806 current->ReplaceWith(replacement);
1807 // If the current is the return value then we need to update the latter.
1808 if (current == return_value) {
1809 DCHECK_EQ(entry_block_, return_value->GetBlock());
1810 return_value = replacement;
1811 }
1812 }
1813 }
1814
1815 if (return_value != nullptr) {
1816 invoke->ReplaceWith(return_value);
David Brazdil05144f42015-04-16 15:18:00 +01001817 }
1818
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001819 // Finally remove the invoke from the caller.
1820 invoke->GetBlock()->RemoveInstruction(invoke);
Calin Juravle2e768302015-07-28 14:41:11 +00001821
1822 return return_value;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001823}
1824
Mingyao Yang3584bce2015-05-19 16:01:59 -07001825/*
1826 * Loop will be transformed to:
1827 * old_pre_header
1828 * |
1829 * if_block
1830 * / \
1831 * dummy_block deopt_block
1832 * \ /
1833 * new_pre_header
1834 * |
1835 * header
1836 */
1837void HGraph::TransformLoopHeaderForBCE(HBasicBlock* header) {
1838 DCHECK(header->IsLoopHeader());
1839 HBasicBlock* pre_header = header->GetDominator();
1840
1841 // Need this to avoid critical edge.
1842 HBasicBlock* if_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1843 // Need this to avoid critical edge.
1844 HBasicBlock* dummy_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1845 HBasicBlock* deopt_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1846 HBasicBlock* new_pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
1847 AddBlock(if_block);
1848 AddBlock(dummy_block);
1849 AddBlock(deopt_block);
1850 AddBlock(new_pre_header);
1851
1852 header->ReplacePredecessor(pre_header, new_pre_header);
Vladimir Marko60584552015-09-03 13:35:12 +00001853 pre_header->successors_.clear();
1854 pre_header->dominated_blocks_.clear();
Mingyao Yang3584bce2015-05-19 16:01:59 -07001855
1856 pre_header->AddSuccessor(if_block);
1857 if_block->AddSuccessor(dummy_block); // True successor
1858 if_block->AddSuccessor(deopt_block); // False successor
1859 dummy_block->AddSuccessor(new_pre_header);
1860 deopt_block->AddSuccessor(new_pre_header);
1861
Vladimir Marko60584552015-09-03 13:35:12 +00001862 pre_header->dominated_blocks_.push_back(if_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001863 if_block->SetDominator(pre_header);
Vladimir Marko60584552015-09-03 13:35:12 +00001864 if_block->dominated_blocks_.push_back(dummy_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001865 dummy_block->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001866 if_block->dominated_blocks_.push_back(deopt_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001867 deopt_block->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001868 if_block->dominated_blocks_.push_back(new_pre_header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001869 new_pre_header->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001870 new_pre_header->dominated_blocks_.push_back(header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001871 header->SetDominator(new_pre_header);
1872
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001873 size_t index_of_header = IndexOfElement(reverse_post_order_, header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001874 MakeRoomFor(&reverse_post_order_, 4, index_of_header - 1);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001875 reverse_post_order_[index_of_header++] = if_block;
1876 reverse_post_order_[index_of_header++] = dummy_block;
1877 reverse_post_order_[index_of_header++] = deopt_block;
1878 reverse_post_order_[index_of_header++] = new_pre_header;
Mingyao Yang3584bce2015-05-19 16:01:59 -07001879
1880 HLoopInformation* info = pre_header->GetLoopInformation();
1881 if (info != nullptr) {
1882 if_block->SetLoopInformation(info);
1883 dummy_block->SetLoopInformation(info);
1884 deopt_block->SetLoopInformation(info);
1885 new_pre_header->SetLoopInformation(info);
1886 for (HLoopInformationOutwardIterator loop_it(*pre_header);
1887 !loop_it.Done();
1888 loop_it.Advance()) {
1889 loop_it.Current()->Add(if_block);
1890 loop_it.Current()->Add(dummy_block);
1891 loop_it.Current()->Add(deopt_block);
1892 loop_it.Current()->Add(new_pre_header);
1893 }
1894 }
1895}
1896
Calin Juravle2e768302015-07-28 14:41:11 +00001897void HInstruction::SetReferenceTypeInfo(ReferenceTypeInfo rti) {
1898 if (kIsDebugBuild) {
1899 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1900 ScopedObjectAccess soa(Thread::Current());
1901 DCHECK(rti.IsValid()) << "Invalid RTI for " << DebugName();
1902 if (IsBoundType()) {
1903 // Having the test here spares us from making the method virtual just for
1904 // the sake of a DCHECK.
1905 ReferenceTypeInfo upper_bound_rti = AsBoundType()->GetUpperBound();
1906 DCHECK(upper_bound_rti.IsSupertypeOf(rti))
1907 << " upper_bound_rti: " << upper_bound_rti
1908 << " rti: " << rti;
David Brazdilbaf89b82015-09-15 11:36:54 +01001909 DCHECK(!upper_bound_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes() || rti.IsExact());
Calin Juravle2e768302015-07-28 14:41:11 +00001910 }
1911 }
1912 reference_type_info_ = rti;
1913}
1914
1915ReferenceTypeInfo::ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
1916
1917ReferenceTypeInfo::ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
1918 : type_handle_(type_handle), is_exact_(is_exact) {
1919 if (kIsDebugBuild) {
1920 ScopedObjectAccess soa(Thread::Current());
1921 DCHECK(IsValidHandle(type_handle));
1922 }
1923}
1924
Calin Juravleacf735c2015-02-12 15:25:22 +00001925std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1926 ScopedObjectAccess soa(Thread::Current());
1927 os << "["
Calin Juravle2e768302015-07-28 14:41:11 +00001928 << " is_valid=" << rhs.IsValid()
1929 << " type=" << (!rhs.IsValid() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
Calin Juravleacf735c2015-02-12 15:25:22 +00001930 << " is_exact=" << rhs.IsExact()
1931 << " ]";
1932 return os;
1933}
1934
Mark Mendellc4701932015-04-10 13:18:51 -04001935bool HInstruction::HasAnyEnvironmentUseBefore(HInstruction* other) {
1936 // For now, assume that instructions in different blocks may use the
1937 // environment.
1938 // TODO: Use the control flow to decide if this is true.
1939 if (GetBlock() != other->GetBlock()) {
1940 return true;
1941 }
1942
1943 // We know that we are in the same block. Walk from 'this' to 'other',
1944 // checking to see if there is any instruction with an environment.
1945 HInstruction* current = this;
1946 for (; current != other && current != nullptr; current = current->GetNext()) {
1947 // This is a conservative check, as the instruction result may not be in
1948 // the referenced environment.
1949 if (current->HasEnvironment()) {
1950 return true;
1951 }
1952 }
1953
1954 // We should have been called with 'this' before 'other' in the block.
1955 // Just confirm this.
1956 DCHECK(current != nullptr);
1957 return false;
1958}
1959
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001960void HInvoke::SetIntrinsic(Intrinsics intrinsic,
1961 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
1962 intrinsic_ = intrinsic;
1963 IntrinsicOptimizations opt(this);
1964 if (needs_env_or_cache == kNoEnvironmentOrCache) {
1965 opt.SetDoesNotNeedDexCache();
1966 opt.SetDoesNotNeedEnvironment();
1967 }
1968}
1969
1970bool HInvoke::NeedsEnvironment() const {
1971 if (!IsIntrinsic()) {
1972 return true;
1973 }
1974 IntrinsicOptimizations opt(*this);
1975 return !opt.GetDoesNotNeedEnvironment();
1976}
1977
Vladimir Markodc151b22015-10-15 18:02:30 +01001978bool HInvokeStaticOrDirect::NeedsDexCacheOfDeclaringClass() const {
1979 if (GetMethodLoadKind() != MethodLoadKind::kDexCacheViaMethod) {
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001980 return false;
1981 }
1982 if (!IsIntrinsic()) {
1983 return true;
1984 }
1985 IntrinsicOptimizations opt(*this);
1986 return !opt.GetDoesNotNeedDexCache();
1987}
1988
Mark Mendellc4701932015-04-10 13:18:51 -04001989void HInstruction::RemoveEnvironmentUsers() {
1990 for (HUseIterator<HEnvironment*> use_it(GetEnvUses()); !use_it.Done(); use_it.Advance()) {
1991 HUseListNode<HEnvironment*>* user_node = use_it.Current();
1992 HEnvironment* user = user_node->GetUser();
1993 user->SetRawEnvAt(user_node->GetIndex(), nullptr);
1994 }
1995 env_uses_.Clear();
1996}
1997
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001998} // namespace art