blob: af3d8f4a60cbe1136858d07099e9393e2f20bb27 [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() {
386 // Simplify the CFG for future analysis, and code generation:
387 // (1): Split critical edges.
388 // (2): Simplify loops by having only one back edge, and 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 Brazdilffee3d32015-07-06 11:48:53 +0100394 if (block->NumberOfNormalSuccessors() > 1) {
Vladimir Marko60584552015-09-03 13:35:12 +0000395 for (size_t j = 0; j < block->GetSuccessors().size(); ++j) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100396 HBasicBlock* successor = block->GetSuccessors()[j];
David Brazdilffee3d32015-07-06 11:48:53 +0100397 DCHECK(!successor->IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +0000398 if (successor->GetPredecessors().size() > 1) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100399 SplitCriticalEdge(block, successor);
400 --j;
401 }
402 }
403 }
404 if (block->IsLoopHeader()) {
405 SimplifyLoop(block);
406 }
407 }
408}
409
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000410bool HGraph::AnalyzeNaturalLoops() const {
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100411 // Order does not matter.
412 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
413 HBasicBlock* block = it.Current();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100414 if (block->IsLoopHeader()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100415 if (block->IsCatchBlock()) {
416 // TODO: Dealing with exceptional back edges could be tricky because
417 // they only approximate the real control flow. Bail out for now.
418 return false;
419 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100420 HLoopInformation* info = block->GetLoopInformation();
421 if (!info->Populate()) {
422 // Abort if the loop is non natural. We currently bailout in such cases.
423 return false;
424 }
425 }
426 }
427 return true;
428}
429
David Brazdil8d5b8b22015-03-24 10:51:52 +0000430void HGraph::InsertConstant(HConstant* constant) {
431 // New constants are inserted before the final control-flow instruction
432 // of the graph, or at its end if called from the graph builder.
433 if (entry_block_->EndsWithControlFlowInstruction()) {
434 entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction());
David Brazdil46e2a392015-03-16 17:31:52 +0000435 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000436 entry_block_->AddInstruction(constant);
David Brazdil46e2a392015-03-16 17:31:52 +0000437 }
438}
439
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600440HNullConstant* HGraph::GetNullConstant(uint32_t dex_pc) {
Nicolas Geoffray18e68732015-06-17 23:09:05 +0100441 // For simplicity, don't bother reviving the cached null constant if it is
442 // not null and not in a block. Otherwise, we need to clear the instruction
443 // id and/or any invariants the graph is assuming when adding new instructions.
444 if ((cached_null_constant_ == nullptr) || (cached_null_constant_->GetBlock() == nullptr)) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600445 cached_null_constant_ = new (arena_) HNullConstant(dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000446 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000447 }
448 return cached_null_constant_;
449}
450
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100451HCurrentMethod* HGraph::GetCurrentMethod() {
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100452 // For simplicity, don't bother reviving the cached current method if it is
453 // not null and not in a block. Otherwise, we need to clear the instruction
454 // id and/or any invariants the graph is assuming when adding new instructions.
455 if ((cached_current_method_ == nullptr) || (cached_current_method_->GetBlock() == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700456 cached_current_method_ = new (arena_) HCurrentMethod(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600457 Is64BitInstructionSet(instruction_set_) ? Primitive::kPrimLong : Primitive::kPrimInt,
458 entry_block_->GetDexPc());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100459 if (entry_block_->GetFirstInstruction() == nullptr) {
460 entry_block_->AddInstruction(cached_current_method_);
461 } else {
462 entry_block_->InsertInstructionBefore(
463 cached_current_method_, entry_block_->GetFirstInstruction());
464 }
465 }
466 return cached_current_method_;
467}
468
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600469HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000470 switch (type) {
471 case Primitive::Type::kPrimBoolean:
472 DCHECK(IsUint<1>(value));
473 FALLTHROUGH_INTENDED;
474 case Primitive::Type::kPrimByte:
475 case Primitive::Type::kPrimChar:
476 case Primitive::Type::kPrimShort:
477 case Primitive::Type::kPrimInt:
478 DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600479 return GetIntConstant(static_cast<int32_t>(value), dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000480
481 case Primitive::Type::kPrimLong:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600482 return GetLongConstant(value, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000483
484 default:
485 LOG(FATAL) << "Unsupported constant type";
486 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000487 }
David Brazdil46e2a392015-03-16 17:31:52 +0000488}
489
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000490void HGraph::CacheFloatConstant(HFloatConstant* constant) {
491 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
492 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
493 cached_float_constants_.Overwrite(value, constant);
494}
495
496void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
497 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
498 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
499 cached_double_constants_.Overwrite(value, constant);
500}
501
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000502void HLoopInformation::Add(HBasicBlock* block) {
503 blocks_.SetBit(block->GetBlockId());
504}
505
David Brazdil46e2a392015-03-16 17:31:52 +0000506void HLoopInformation::Remove(HBasicBlock* block) {
507 blocks_.ClearBit(block->GetBlockId());
508}
509
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100510void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
511 if (blocks_.IsBitSet(block->GetBlockId())) {
512 return;
513 }
514
515 blocks_.SetBit(block->GetBlockId());
516 block->SetInLoop(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000517 for (HBasicBlock* predecessor : block->GetPredecessors()) {
518 PopulateRecursive(predecessor);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100519 }
520}
521
522bool HLoopInformation::Populate() {
David Brazdila4b8c212015-05-07 09:59:30 +0100523 DCHECK_EQ(blocks_.NumSetBits(), 0u) << "Loop information has already been populated";
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100524 for (HBasicBlock* back_edge : GetBackEdges()) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100525 DCHECK(back_edge->GetDominator() != nullptr);
526 if (!header_->Dominates(back_edge)) {
527 // This loop is not natural. Do not bother going further.
528 return false;
529 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100530
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100531 // Populate this loop: starting with the back edge, recursively add predecessors
532 // that are not already part of that loop. Set the header as part of the loop
533 // to end the recursion.
534 // This is a recursive implementation of the algorithm described in
535 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
536 blocks_.SetBit(header_->GetBlockId());
537 PopulateRecursive(back_edge);
538 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100539 return true;
540}
541
David Brazdila4b8c212015-05-07 09:59:30 +0100542void HLoopInformation::Update() {
543 HGraph* graph = header_->GetGraph();
544 for (uint32_t id : blocks_.Indexes()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100545 HBasicBlock* block = graph->GetBlocks()[id];
David Brazdila4b8c212015-05-07 09:59:30 +0100546 // Reset loop information of non-header blocks inside the loop, except
547 // members of inner nested loops because those should already have been
548 // updated by their own LoopInformation.
549 if (block->GetLoopInformation() == this && block != header_) {
550 block->SetLoopInformation(nullptr);
551 }
552 }
553 blocks_.ClearAllBits();
554
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100555 if (back_edges_.empty()) {
David Brazdila4b8c212015-05-07 09:59:30 +0100556 // The loop has been dismantled, delete its suspend check and remove info
557 // from the header.
558 DCHECK(HasSuspendCheck());
559 header_->RemoveInstruction(suspend_check_);
560 header_->SetLoopInformation(nullptr);
561 header_ = nullptr;
562 suspend_check_ = nullptr;
563 } else {
564 if (kIsDebugBuild) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100565 for (HBasicBlock* back_edge : back_edges_) {
566 DCHECK(header_->Dominates(back_edge));
David Brazdila4b8c212015-05-07 09:59:30 +0100567 }
568 }
569 // This loop still has reachable back edges. Repopulate the list of blocks.
570 bool populate_successful = Populate();
571 DCHECK(populate_successful);
572 }
573}
574
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100575HBasicBlock* HLoopInformation::GetPreHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100576 return header_->GetDominator();
577}
578
579bool HLoopInformation::Contains(const HBasicBlock& block) const {
580 return blocks_.IsBitSet(block.GetBlockId());
581}
582
583bool HLoopInformation::IsIn(const HLoopInformation& other) const {
584 return other.blocks_.IsBitSet(header_->GetBlockId());
585}
586
Aart Bik73f1f3b2015-10-28 15:28:08 -0700587bool HLoopInformation::IsLoopInvariant(HInstruction* instruction, bool must_dominate) const {
588 HLoopInformation* other_loop = instruction->GetBlock()->GetLoopInformation();
589 if (other_loop != this && (other_loop == nullptr || !other_loop->IsIn(*this))) {
590 if (must_dominate) {
591 return instruction->GetBlock()->Dominates(GetHeader());
592 }
593 return true;
594 }
595 return false;
596}
597
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100598size_t HLoopInformation::GetLifetimeEnd() const {
599 size_t last_position = 0;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100600 for (HBasicBlock* back_edge : GetBackEdges()) {
601 last_position = std::max(back_edge->GetLifetimeEnd(), last_position);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100602 }
603 return last_position;
604}
605
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100606bool HBasicBlock::Dominates(HBasicBlock* other) const {
607 // Walk up the dominator tree from `other`, to find out if `this`
608 // is an ancestor.
609 HBasicBlock* current = other;
610 while (current != nullptr) {
611 if (current == this) {
612 return true;
613 }
614 current = current->GetDominator();
615 }
616 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100617}
618
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100619static void UpdateInputsUsers(HInstruction* instruction) {
620 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
621 instruction->InputAt(i)->AddUseAt(instruction, i);
622 }
623 // Environment should be created later.
624 DCHECK(!instruction->HasEnvironment());
625}
626
Roland Levillainccc07a92014-09-16 14:48:16 +0100627void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
628 HInstruction* replacement) {
629 DCHECK(initial->GetBlock() == this);
Mark Mendell805b3b52015-09-18 14:10:29 -0400630 if (initial->IsControlFlow()) {
631 // We can only replace a control flow instruction with another control flow instruction.
632 DCHECK(replacement->IsControlFlow());
633 DCHECK_EQ(replacement->GetId(), -1);
634 DCHECK_EQ(replacement->GetType(), Primitive::kPrimVoid);
635 DCHECK_EQ(initial->GetBlock(), this);
636 DCHECK_EQ(initial->GetType(), Primitive::kPrimVoid);
637 DCHECK(initial->GetUses().IsEmpty());
638 DCHECK(initial->GetEnvUses().IsEmpty());
639 replacement->SetBlock(this);
640 replacement->SetId(GetGraph()->GetNextInstructionId());
641 instructions_.InsertInstructionBefore(replacement, initial);
642 UpdateInputsUsers(replacement);
643 } else {
644 InsertInstructionBefore(replacement, initial);
645 initial->ReplaceWith(replacement);
646 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100647 RemoveInstruction(initial);
648}
649
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100650static void Add(HInstructionList* instruction_list,
651 HBasicBlock* block,
652 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000653 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000654 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100655 instruction->SetBlock(block);
656 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100657 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100658 instruction_list->AddInstruction(instruction);
659}
660
661void HBasicBlock::AddInstruction(HInstruction* instruction) {
662 Add(&instructions_, this, instruction);
663}
664
665void HBasicBlock::AddPhi(HPhi* phi) {
666 Add(&phis_, this, phi);
667}
668
David Brazdilc3d743f2015-04-22 13:40:50 +0100669void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
670 DCHECK(!cursor->IsPhi());
671 DCHECK(!instruction->IsPhi());
672 DCHECK_EQ(instruction->GetId(), -1);
673 DCHECK_NE(cursor->GetId(), -1);
674 DCHECK_EQ(cursor->GetBlock(), this);
675 DCHECK(!instruction->IsControlFlow());
676 instruction->SetBlock(this);
677 instruction->SetId(GetGraph()->GetNextInstructionId());
678 UpdateInputsUsers(instruction);
679 instructions_.InsertInstructionBefore(instruction, cursor);
680}
681
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100682void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
683 DCHECK(!cursor->IsPhi());
684 DCHECK(!instruction->IsPhi());
685 DCHECK_EQ(instruction->GetId(), -1);
686 DCHECK_NE(cursor->GetId(), -1);
687 DCHECK_EQ(cursor->GetBlock(), this);
688 DCHECK(!instruction->IsControlFlow());
689 DCHECK(!cursor->IsControlFlow());
690 instruction->SetBlock(this);
691 instruction->SetId(GetGraph()->GetNextInstructionId());
692 UpdateInputsUsers(instruction);
693 instructions_.InsertInstructionAfter(instruction, cursor);
694}
695
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100696void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
697 DCHECK_EQ(phi->GetId(), -1);
698 DCHECK_NE(cursor->GetId(), -1);
699 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100700 phi->SetBlock(this);
701 phi->SetId(GetGraph()->GetNextInstructionId());
702 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100703 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100704}
705
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100706static void Remove(HInstructionList* instruction_list,
707 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000708 HInstruction* instruction,
709 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100710 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100711 instruction->SetBlock(nullptr);
712 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000713 if (ensure_safety) {
714 DCHECK(instruction->GetUses().IsEmpty());
715 DCHECK(instruction->GetEnvUses().IsEmpty());
716 RemoveAsUser(instruction);
717 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100718}
719
David Brazdil1abb4192015-02-17 18:33:36 +0000720void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100721 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000722 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100723}
724
David Brazdil1abb4192015-02-17 18:33:36 +0000725void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
726 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100727}
728
David Brazdilc7508e92015-04-27 13:28:57 +0100729void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
730 if (instruction->IsPhi()) {
731 RemovePhi(instruction->AsPhi(), ensure_safety);
732 } else {
733 RemoveInstruction(instruction, ensure_safety);
734 }
735}
736
Vladimir Marko71bf8092015-09-15 15:33:14 +0100737void HEnvironment::CopyFrom(const ArenaVector<HInstruction*>& locals) {
738 for (size_t i = 0; i < locals.size(); i++) {
739 HInstruction* instruction = locals[i];
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +0100740 SetRawEnvAt(i, instruction);
741 if (instruction != nullptr) {
742 instruction->AddEnvUseAt(this, i);
743 }
744 }
745}
746
David Brazdiled596192015-01-23 10:39:45 +0000747void HEnvironment::CopyFrom(HEnvironment* env) {
748 for (size_t i = 0; i < env->Size(); i++) {
749 HInstruction* instruction = env->GetInstructionAt(i);
750 SetRawEnvAt(i, instruction);
751 if (instruction != nullptr) {
752 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100753 }
David Brazdiled596192015-01-23 10:39:45 +0000754 }
755}
756
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700757void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
758 HBasicBlock* loop_header) {
759 DCHECK(loop_header->IsLoopHeader());
760 for (size_t i = 0; i < env->Size(); i++) {
761 HInstruction* instruction = env->GetInstructionAt(i);
762 SetRawEnvAt(i, instruction);
763 if (instruction == nullptr) {
764 continue;
765 }
766 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
767 // At the end of the loop pre-header, the corresponding value for instruction
768 // is the first input of the phi.
769 HInstruction* initial = instruction->AsPhi()->InputAt(0);
770 DCHECK(initial->GetBlock()->Dominates(loop_header));
771 SetRawEnvAt(i, initial);
772 initial->AddEnvUseAt(this, i);
773 } else {
774 instruction->AddEnvUseAt(this, i);
775 }
776 }
777}
778
David Brazdil1abb4192015-02-17 18:33:36 +0000779void HEnvironment::RemoveAsUserOfInput(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100780 const HUserRecord<HEnvironment*>& user_record = vregs_[index];
David Brazdil1abb4192015-02-17 18:33:36 +0000781 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100782}
783
Calin Juravle77520bc2015-01-12 18:45:46 +0000784HInstruction* HInstruction::GetNextDisregardingMoves() const {
785 HInstruction* next = GetNext();
786 while (next != nullptr && next->IsParallelMove()) {
787 next = next->GetNext();
788 }
789 return next;
790}
791
792HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
793 HInstruction* previous = GetPrevious();
794 while (previous != nullptr && previous->IsParallelMove()) {
795 previous = previous->GetPrevious();
796 }
797 return previous;
798}
799
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100800void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000801 if (first_instruction_ == nullptr) {
802 DCHECK(last_instruction_ == nullptr);
803 first_instruction_ = last_instruction_ = instruction;
804 } else {
805 last_instruction_->next_ = instruction;
806 instruction->previous_ = last_instruction_;
807 last_instruction_ = instruction;
808 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000809}
810
David Brazdilc3d743f2015-04-22 13:40:50 +0100811void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
812 DCHECK(Contains(cursor));
813 if (cursor == first_instruction_) {
814 cursor->previous_ = instruction;
815 instruction->next_ = cursor;
816 first_instruction_ = instruction;
817 } else {
818 instruction->previous_ = cursor->previous_;
819 instruction->next_ = cursor;
820 cursor->previous_ = instruction;
821 instruction->previous_->next_ = instruction;
822 }
823}
824
825void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
826 DCHECK(Contains(cursor));
827 if (cursor == last_instruction_) {
828 cursor->next_ = instruction;
829 instruction->previous_ = cursor;
830 last_instruction_ = instruction;
831 } else {
832 instruction->next_ = cursor->next_;
833 instruction->previous_ = cursor;
834 cursor->next_ = instruction;
835 instruction->next_->previous_ = instruction;
836 }
837}
838
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100839void HInstructionList::RemoveInstruction(HInstruction* instruction) {
840 if (instruction->previous_ != nullptr) {
841 instruction->previous_->next_ = instruction->next_;
842 }
843 if (instruction->next_ != nullptr) {
844 instruction->next_->previous_ = instruction->previous_;
845 }
846 if (instruction == first_instruction_) {
847 first_instruction_ = instruction->next_;
848 }
849 if (instruction == last_instruction_) {
850 last_instruction_ = instruction->previous_;
851 }
852}
853
Roland Levillain6b469232014-09-25 10:10:38 +0100854bool HInstructionList::Contains(HInstruction* instruction) const {
855 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
856 if (it.Current() == instruction) {
857 return true;
858 }
859 }
860 return false;
861}
862
Roland Levillainccc07a92014-09-16 14:48:16 +0100863bool HInstructionList::FoundBefore(const HInstruction* instruction1,
864 const HInstruction* instruction2) const {
865 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
866 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
867 if (it.Current() == instruction1) {
868 return true;
869 }
870 if (it.Current() == instruction2) {
871 return false;
872 }
873 }
874 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
875 return true;
876}
877
Roland Levillain6c82d402014-10-13 16:10:27 +0100878bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
879 if (other_instruction == this) {
880 // An instruction does not strictly dominate itself.
881 return false;
882 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100883 HBasicBlock* block = GetBlock();
884 HBasicBlock* other_block = other_instruction->GetBlock();
885 if (block != other_block) {
886 return GetBlock()->Dominates(other_instruction->GetBlock());
887 } else {
888 // If both instructions are in the same block, ensure this
889 // instruction comes before `other_instruction`.
890 if (IsPhi()) {
891 if (!other_instruction->IsPhi()) {
892 // Phis appear before non phi-instructions so this instruction
893 // dominates `other_instruction`.
894 return true;
895 } else {
896 // There is no order among phis.
897 LOG(FATAL) << "There is no dominance between phis of a same block.";
898 return false;
899 }
900 } else {
901 // `this` is not a phi.
902 if (other_instruction->IsPhi()) {
903 // Phis appear before non phi-instructions so this instruction
904 // does not dominate `other_instruction`.
905 return false;
906 } else {
907 // Check whether this instruction comes before
908 // `other_instruction` in the instruction list.
909 return block->GetInstructions().FoundBefore(this, other_instruction);
910 }
911 }
912 }
913}
914
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100915void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100916 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000917 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
918 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100919 HInstruction* user = current->GetUser();
920 size_t input_index = current->GetIndex();
921 user->SetRawInputAt(input_index, other);
922 other->AddUseAt(user, input_index);
923 }
924
David Brazdiled596192015-01-23 10:39:45 +0000925 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
926 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100927 HEnvironment* user = current->GetUser();
928 size_t input_index = current->GetIndex();
929 user->SetRawEnvAt(input_index, other);
930 other->AddEnvUseAt(user, input_index);
931 }
932
David Brazdiled596192015-01-23 10:39:45 +0000933 uses_.Clear();
934 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100935}
936
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100937void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000938 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100939 SetRawInputAt(index, replacement);
940 replacement->AddUseAt(this, index);
941}
942
Nicolas Geoffray39468442014-09-02 15:17:15 +0100943size_t HInstruction::EnvironmentSize() const {
944 return HasEnvironment() ? environment_->Size() : 0;
945}
946
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100947void HPhi::AddInput(HInstruction* input) {
948 DCHECK(input->GetBlock() != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100949 inputs_.push_back(HUserRecord<HInstruction*>(input));
950 input->AddUseAt(this, inputs_.size() - 1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100951}
952
David Brazdil2d7352b2015-04-20 14:52:42 +0100953void HPhi::RemoveInputAt(size_t index) {
954 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100955 inputs_.erase(inputs_.begin() + index);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100956 for (size_t i = index, e = InputCount(); i < e; ++i) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100957 DCHECK_EQ(InputRecordAt(i).GetUseNode()->GetIndex(), i + 1u);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100958 InputRecordAt(i).GetUseNode()->SetIndex(i);
959 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100960}
961
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100962#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000963void H##name::Accept(HGraphVisitor* visitor) { \
964 visitor->Visit##name(this); \
965}
966
967FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
968
969#undef DEFINE_ACCEPT
970
971void HGraphVisitor::VisitInsertionOrder() {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100972 const ArenaVector<HBasicBlock*>& blocks = graph_->GetBlocks();
973 for (HBasicBlock* block : blocks) {
David Brazdil46e2a392015-03-16 17:31:52 +0000974 if (block != nullptr) {
975 VisitBasicBlock(block);
976 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000977 }
978}
979
Roland Levillain633021e2014-10-01 14:12:25 +0100980void HGraphVisitor::VisitReversePostOrder() {
981 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
982 VisitBasicBlock(it.Current());
983 }
984}
985
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000986void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100987 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100988 it.Current()->Accept(this);
989 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100990 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000991 it.Current()->Accept(this);
992 }
993}
994
Mark Mendelle82549b2015-05-06 10:55:34 -0400995HConstant* HTypeConversion::TryStaticEvaluation() const {
996 HGraph* graph = GetBlock()->GetGraph();
997 if (GetInput()->IsIntConstant()) {
998 int32_t value = GetInput()->AsIntConstant()->GetValue();
999 switch (GetResultType()) {
1000 case Primitive::kPrimLong:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001001 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001002 case Primitive::kPrimFloat:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001003 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001004 case Primitive::kPrimDouble:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001005 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001006 default:
1007 return nullptr;
1008 }
1009 } else if (GetInput()->IsLongConstant()) {
1010 int64_t value = GetInput()->AsLongConstant()->GetValue();
1011 switch (GetResultType()) {
1012 case Primitive::kPrimInt:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001013 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001014 case Primitive::kPrimFloat:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001015 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001016 case Primitive::kPrimDouble:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001017 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001018 default:
1019 return nullptr;
1020 }
1021 } else if (GetInput()->IsFloatConstant()) {
1022 float value = GetInput()->AsFloatConstant()->GetValue();
1023 switch (GetResultType()) {
1024 case Primitive::kPrimInt:
1025 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001026 return graph->GetIntConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001027 if (value >= kPrimIntMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001028 return graph->GetIntConstant(kPrimIntMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001029 if (value <= kPrimIntMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001030 return graph->GetIntConstant(kPrimIntMin, GetDexPc());
1031 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001032 case Primitive::kPrimLong:
1033 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001034 return graph->GetLongConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001035 if (value >= kPrimLongMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001036 return graph->GetLongConstant(kPrimLongMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001037 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001038 return graph->GetLongConstant(kPrimLongMin, GetDexPc());
1039 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001040 case Primitive::kPrimDouble:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001041 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001042 default:
1043 return nullptr;
1044 }
1045 } else if (GetInput()->IsDoubleConstant()) {
1046 double value = GetInput()->AsDoubleConstant()->GetValue();
1047 switch (GetResultType()) {
1048 case Primitive::kPrimInt:
1049 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001050 return graph->GetIntConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001051 if (value >= kPrimIntMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001052 return graph->GetIntConstant(kPrimIntMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001053 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001054 return graph->GetIntConstant(kPrimIntMin, GetDexPc());
1055 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001056 case Primitive::kPrimLong:
1057 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001058 return graph->GetLongConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001059 if (value >= kPrimLongMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001060 return graph->GetLongConstant(kPrimLongMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001061 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001062 return graph->GetLongConstant(kPrimLongMin, GetDexPc());
1063 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001064 case Primitive::kPrimFloat:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001065 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001066 default:
1067 return nullptr;
1068 }
1069 }
1070 return nullptr;
1071}
1072
Roland Levillain9240d6a2014-10-20 16:47:04 +01001073HConstant* HUnaryOperation::TryStaticEvaluation() const {
1074 if (GetInput()->IsIntConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001075 return Evaluate(GetInput()->AsIntConstant());
Roland Levillain9240d6a2014-10-20 16:47:04 +01001076 } else if (GetInput()->IsLongConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001077 return Evaluate(GetInput()->AsLongConstant());
Roland Levillain9240d6a2014-10-20 16:47:04 +01001078 }
1079 return nullptr;
1080}
1081
1082HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain9867bc72015-08-05 10:21:34 +01001083 if (GetLeft()->IsIntConstant()) {
1084 if (GetRight()->IsIntConstant()) {
1085 return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsIntConstant());
1086 } else if (GetRight()->IsLongConstant()) {
1087 return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsLongConstant());
1088 }
1089 } else if (GetLeft()->IsLongConstant()) {
1090 if (GetRight()->IsIntConstant()) {
1091 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsIntConstant());
1092 } else if (GetRight()->IsLongConstant()) {
1093 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsLongConstant());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +00001094 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001095 }
1096 return nullptr;
1097}
Dave Allison20dfc792014-06-16 20:44:29 -07001098
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001099HConstant* HBinaryOperation::GetConstantRight() const {
1100 if (GetRight()->IsConstant()) {
1101 return GetRight()->AsConstant();
1102 } else if (IsCommutative() && GetLeft()->IsConstant()) {
1103 return GetLeft()->AsConstant();
1104 } else {
1105 return nullptr;
1106 }
1107}
1108
1109// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001110// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001111HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
1112 HInstruction* most_constant_right = GetConstantRight();
1113 if (most_constant_right == nullptr) {
1114 return nullptr;
1115 } else if (most_constant_right == GetLeft()) {
1116 return GetRight();
1117 } else {
1118 return GetLeft();
1119 }
1120}
1121
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001122bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
1123 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001124}
1125
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001126bool HInstruction::Equals(HInstruction* other) const {
1127 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001128 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001129 if (!InstructionDataEquals(other)) return false;
1130 if (GetType() != other->GetType()) return false;
1131 if (InputCount() != other->InputCount()) return false;
1132
1133 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1134 if (InputAt(i) != other->InputAt(i)) return false;
1135 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001136 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001137 return true;
1138}
1139
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001140std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
1141#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
1142 switch (rhs) {
1143 FOR_EACH_INSTRUCTION(DECLARE_CASE)
1144 default:
1145 os << "Unknown instruction kind " << static_cast<int>(rhs);
1146 break;
1147 }
1148#undef DECLARE_CASE
1149 return os;
1150}
1151
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001152void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001153 next_->previous_ = previous_;
1154 if (previous_ != nullptr) {
1155 previous_->next_ = next_;
1156 }
1157 if (block_->instructions_.first_instruction_ == this) {
1158 block_->instructions_.first_instruction_ = next_;
1159 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001160 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001161
1162 previous_ = cursor->previous_;
1163 if (previous_ != nullptr) {
1164 previous_->next_ = this;
1165 }
1166 next_ = cursor;
1167 cursor->previous_ = this;
1168 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001169
1170 if (block_->instructions_.first_instruction_ == cursor) {
1171 block_->instructions_.first_instruction_ = this;
1172 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001173}
1174
David Brazdilfc6a86a2015-06-26 10:33:45 +00001175HBasicBlock* HBasicBlock::SplitBefore(HInstruction* cursor) {
David Brazdil9bc43612015-11-05 21:25:24 +00001176 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented.";
David Brazdilfc6a86a2015-06-26 10:33:45 +00001177 DCHECK_EQ(cursor->GetBlock(), this);
1178
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001179 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(),
1180 cursor->GetDexPc());
David Brazdilfc6a86a2015-06-26 10:33:45 +00001181 new_block->instructions_.first_instruction_ = cursor;
1182 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1183 instructions_.last_instruction_ = cursor->previous_;
1184 if (cursor->previous_ == nullptr) {
1185 instructions_.first_instruction_ = nullptr;
1186 } else {
1187 cursor->previous_->next_ = nullptr;
1188 cursor->previous_ = nullptr;
1189 }
1190
1191 new_block->instructions_.SetBlockOfInstructions(new_block);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001192 AddInstruction(new (GetGraph()->GetArena()) HGoto(new_block->GetDexPc()));
David Brazdilfc6a86a2015-06-26 10:33:45 +00001193
Vladimir Marko60584552015-09-03 13:35:12 +00001194 for (HBasicBlock* successor : GetSuccessors()) {
1195 new_block->successors_.push_back(successor);
1196 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001197 }
Vladimir Marko60584552015-09-03 13:35:12 +00001198 successors_.clear();
David Brazdilfc6a86a2015-06-26 10:33:45 +00001199 AddSuccessor(new_block);
1200
David Brazdil56e1acc2015-06-30 15:41:36 +01001201 GetGraph()->AddBlock(new_block);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001202 return new_block;
1203}
1204
David Brazdild7558da2015-09-22 13:04:14 +01001205HBasicBlock* HBasicBlock::CreateImmediateDominator() {
David Brazdil9bc43612015-11-05 21:25:24 +00001206 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented.";
David Brazdild7558da2015-09-22 13:04:14 +01001207 DCHECK(!IsCatchBlock()) << "Support for updating try/catch information not implemented.";
1208
1209 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
1210
1211 for (HBasicBlock* predecessor : GetPredecessors()) {
1212 new_block->predecessors_.push_back(predecessor);
1213 predecessor->successors_[predecessor->GetSuccessorIndexOf(this)] = new_block;
1214 }
1215 predecessors_.clear();
1216 AddPredecessor(new_block);
1217
1218 GetGraph()->AddBlock(new_block);
1219 return new_block;
1220}
1221
David Brazdil9bc43612015-11-05 21:25:24 +00001222HBasicBlock* HBasicBlock::SplitCatchBlockAfterMoveException() {
1223 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented.";
1224 DCHECK(IsCatchBlock()) << "This method is intended for catch blocks only.";
1225
1226 HInstruction* first_insn = GetFirstInstruction();
1227 HInstruction* split_before = nullptr;
1228
1229 if (first_insn != nullptr && first_insn->IsLoadException()) {
1230 // Catch block starts with a LoadException. Split the block after
1231 // the StoreLocal and ClearException which must come after the load.
1232 DCHECK(first_insn->GetNext()->IsStoreLocal());
1233 DCHECK(first_insn->GetNext()->GetNext()->IsClearException());
1234 split_before = first_insn->GetNext()->GetNext()->GetNext();
1235 } else {
1236 // Catch block does not load the exception. Split at the beginning
1237 // to create an empty catch block.
1238 split_before = first_insn;
1239 }
1240
1241 if (split_before == nullptr) {
1242 // Catch block has no instructions after the split point (must be dead).
1243 // Do not split it but rather signal error by returning nullptr.
1244 return nullptr;
1245 } else {
1246 return SplitBefore(split_before);
1247 }
1248}
1249
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001250HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
1251 DCHECK(!cursor->IsControlFlow());
1252 DCHECK_NE(instructions_.last_instruction_, cursor);
1253 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001254
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001255 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
1256 new_block->instructions_.first_instruction_ = cursor->GetNext();
1257 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1258 cursor->next_->previous_ = nullptr;
1259 cursor->next_ = nullptr;
1260 instructions_.last_instruction_ = cursor;
1261
1262 new_block->instructions_.SetBlockOfInstructions(new_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001263 for (HBasicBlock* successor : GetSuccessors()) {
1264 new_block->successors_.push_back(successor);
1265 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001266 }
Vladimir Marko60584552015-09-03 13:35:12 +00001267 successors_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001268
Vladimir Marko60584552015-09-03 13:35:12 +00001269 for (HBasicBlock* dominated : GetDominatedBlocks()) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001270 dominated->dominator_ = new_block;
Vladimir Marko60584552015-09-03 13:35:12 +00001271 new_block->dominated_blocks_.push_back(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001272 }
Vladimir Marko60584552015-09-03 13:35:12 +00001273 dominated_blocks_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001274 return new_block;
1275}
1276
David Brazdilec16f792015-08-19 15:04:01 +01001277const HTryBoundary* HBasicBlock::ComputeTryEntryOfSuccessors() const {
David Brazdilffee3d32015-07-06 11:48:53 +01001278 if (EndsWithTryBoundary()) {
1279 HTryBoundary* try_boundary = GetLastInstruction()->AsTryBoundary();
1280 if (try_boundary->IsEntry()) {
David Brazdilec16f792015-08-19 15:04:01 +01001281 DCHECK(!IsTryBlock());
David Brazdilffee3d32015-07-06 11:48:53 +01001282 return try_boundary;
1283 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001284 DCHECK(IsTryBlock());
1285 DCHECK(try_catch_information_->GetTryEntry().HasSameExceptionHandlersAs(*try_boundary));
David Brazdilffee3d32015-07-06 11:48:53 +01001286 return nullptr;
1287 }
David Brazdilec16f792015-08-19 15:04:01 +01001288 } else if (IsTryBlock()) {
1289 return &try_catch_information_->GetTryEntry();
David Brazdilffee3d32015-07-06 11:48:53 +01001290 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001291 return nullptr;
David Brazdilffee3d32015-07-06 11:48:53 +01001292 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00001293}
1294
David Brazdild7558da2015-09-22 13:04:14 +01001295bool HBasicBlock::HasThrowingInstructions() const {
1296 for (HInstructionIterator it(GetInstructions()); !it.Done(); it.Advance()) {
1297 if (it.Current()->CanThrow()) {
1298 return true;
1299 }
1300 }
1301 return false;
1302}
1303
David Brazdilfc6a86a2015-06-26 10:33:45 +00001304static bool HasOnlyOneInstruction(const HBasicBlock& block) {
1305 return block.GetPhis().IsEmpty()
1306 && !block.GetInstructions().IsEmpty()
1307 && block.GetFirstInstruction() == block.GetLastInstruction();
1308}
1309
David Brazdil46e2a392015-03-16 17:31:52 +00001310bool HBasicBlock::IsSingleGoto() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001311 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsGoto();
1312}
1313
1314bool HBasicBlock::IsSingleTryBoundary() const {
1315 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsTryBoundary();
David Brazdil46e2a392015-03-16 17:31:52 +00001316}
1317
David Brazdil8d5b8b22015-03-24 10:51:52 +00001318bool HBasicBlock::EndsWithControlFlowInstruction() const {
1319 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
1320}
1321
David Brazdilb2bd1c52015-03-25 11:17:37 +00001322bool HBasicBlock::EndsWithIf() const {
1323 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
1324}
1325
David Brazdilffee3d32015-07-06 11:48:53 +01001326bool HBasicBlock::EndsWithTryBoundary() const {
1327 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsTryBoundary();
1328}
1329
David Brazdilb2bd1c52015-03-25 11:17:37 +00001330bool HBasicBlock::HasSinglePhi() const {
1331 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
1332}
1333
David Brazdilffee3d32015-07-06 11:48:53 +01001334bool HTryBoundary::HasSameExceptionHandlersAs(const HTryBoundary& other) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001335 if (GetBlock()->GetSuccessors().size() != other.GetBlock()->GetSuccessors().size()) {
David Brazdilffee3d32015-07-06 11:48:53 +01001336 return false;
1337 }
1338
David Brazdilb618ade2015-07-29 10:31:29 +01001339 // Exception handlers need to be stored in the same order.
1340 for (HExceptionHandlerIterator it1(*this), it2(other);
1341 !it1.Done();
1342 it1.Advance(), it2.Advance()) {
1343 DCHECK(!it2.Done());
1344 if (it1.Current() != it2.Current()) {
David Brazdilffee3d32015-07-06 11:48:53 +01001345 return false;
1346 }
1347 }
1348 return true;
1349}
1350
David Brazdil2d7352b2015-04-20 14:52:42 +01001351size_t HInstructionList::CountSize() const {
1352 size_t size = 0;
1353 HInstruction* current = first_instruction_;
1354 for (; current != nullptr; current = current->GetNext()) {
1355 size++;
1356 }
1357 return size;
1358}
1359
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001360void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
1361 for (HInstruction* current = first_instruction_;
1362 current != nullptr;
1363 current = current->GetNext()) {
1364 current->SetBlock(block);
1365 }
1366}
1367
1368void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
1369 DCHECK(Contains(cursor));
1370 if (!instruction_list.IsEmpty()) {
1371 if (cursor == last_instruction_) {
1372 last_instruction_ = instruction_list.last_instruction_;
1373 } else {
1374 cursor->next_->previous_ = instruction_list.last_instruction_;
1375 }
1376 instruction_list.last_instruction_->next_ = cursor->next_;
1377 cursor->next_ = instruction_list.first_instruction_;
1378 instruction_list.first_instruction_->previous_ = cursor;
1379 }
1380}
1381
1382void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +00001383 if (IsEmpty()) {
1384 first_instruction_ = instruction_list.first_instruction_;
1385 last_instruction_ = instruction_list.last_instruction_;
1386 } else {
1387 AddAfter(last_instruction_, instruction_list);
1388 }
1389}
1390
David Brazdil2d7352b2015-04-20 14:52:42 +01001391void HBasicBlock::DisconnectAndDelete() {
1392 // Dominators must be removed after all the blocks they dominate. This way
1393 // a loop header is removed last, a requirement for correct loop information
1394 // iteration.
Vladimir Marko60584552015-09-03 13:35:12 +00001395 DCHECK(dominated_blocks_.empty());
David Brazdil46e2a392015-03-16 17:31:52 +00001396
David Brazdil2d7352b2015-04-20 14:52:42 +01001397 // Remove the block from all loops it is included in.
1398 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
1399 HLoopInformation* loop_info = it.Current();
1400 loop_info->Remove(this);
1401 if (loop_info->IsBackEdge(*this)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001402 // If this was the last back edge of the loop, we deliberately leave the
1403 // loop in an inconsistent state and will fail SSAChecker unless the
1404 // entire loop is removed during the pass.
David Brazdil2d7352b2015-04-20 14:52:42 +01001405 loop_info->RemoveBackEdge(this);
1406 }
1407 }
1408
1409 // Disconnect the block from its predecessors and update their control-flow
1410 // instructions.
Vladimir Marko60584552015-09-03 13:35:12 +00001411 for (HBasicBlock* predecessor : predecessors_) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001412 HInstruction* last_instruction = predecessor->GetLastInstruction();
David Brazdil2d7352b2015-04-20 14:52:42 +01001413 predecessor->RemoveSuccessor(this);
Mark Mendellfe57faa2015-09-18 09:26:15 -04001414 uint32_t num_pred_successors = predecessor->GetSuccessors().size();
1415 if (num_pred_successors == 1u) {
1416 // If we have one successor after removing one, then we must have
David Brazdilfb552d72015-11-02 20:24:24 +00001417 // had an HIf or HPackedSwitch, as they have more than one successor.
1418 // Replace those with a HGoto.
1419 DCHECK(last_instruction->IsIf() || last_instruction->IsPackedSwitch());
Mark Mendellfe57faa2015-09-18 09:26:15 -04001420 predecessor->RemoveInstruction(last_instruction);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001421 predecessor->AddInstruction(new (graph_->GetArena()) HGoto(last_instruction->GetDexPc()));
Mark Mendellfe57faa2015-09-18 09:26:15 -04001422 } else if (num_pred_successors == 0u) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001423 // The predecessor has no remaining successors and therefore must be dead.
1424 // We deliberately leave it without a control-flow instruction so that the
1425 // SSAChecker fails unless it is not removed during the pass too.
Mark Mendellfe57faa2015-09-18 09:26:15 -04001426 predecessor->RemoveInstruction(last_instruction);
1427 } else {
David Brazdilfb552d72015-11-02 20:24:24 +00001428 // There are multiple successors left. This must come from a HPackedSwitch
1429 // and we are in the middle of removing the HPackedSwitch. Like above, leave
1430 // this alone, and the SSAChecker will fail if it is not removed as well.
1431 DCHECK(last_instruction->IsPackedSwitch());
David Brazdil2d7352b2015-04-20 14:52:42 +01001432 }
David Brazdil46e2a392015-03-16 17:31:52 +00001433 }
Vladimir Marko60584552015-09-03 13:35:12 +00001434 predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001435
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001436 // Disconnect the block from its successors and update their phis.
Vladimir Marko60584552015-09-03 13:35:12 +00001437 for (HBasicBlock* successor : successors_) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001438 // Delete this block from the list of predecessors.
1439 size_t this_index = successor->GetPredecessorIndexOf(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001440 successor->predecessors_.erase(successor->predecessors_.begin() + this_index);
David Brazdil2d7352b2015-04-20 14:52:42 +01001441
1442 // Check that `successor` has other predecessors, otherwise `this` is the
1443 // dominator of `successor` which violates the order DCHECKed at the top.
Vladimir Marko60584552015-09-03 13:35:12 +00001444 DCHECK(!successor->predecessors_.empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001445
David Brazdil2d7352b2015-04-20 14:52:42 +01001446 // Remove this block's entries in the successor's phis.
Vladimir Marko60584552015-09-03 13:35:12 +00001447 if (successor->predecessors_.size() == 1u) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001448 // The successor has just one predecessor left. Replace phis with the only
1449 // remaining input.
1450 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1451 HPhi* phi = phi_it.Current()->AsPhi();
1452 phi->ReplaceWith(phi->InputAt(1 - this_index));
1453 successor->RemovePhi(phi);
1454 }
1455 } else {
1456 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1457 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1458 }
1459 }
1460 }
Vladimir Marko60584552015-09-03 13:35:12 +00001461 successors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001462
1463 // Disconnect from the dominator.
1464 dominator_->RemoveDominatedBlock(this);
1465 SetDominator(nullptr);
1466
1467 // Delete from the graph. The function safely deletes remaining instructions
1468 // and updates the reverse post order.
1469 graph_->DeleteDeadBlock(this);
1470 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001471}
1472
1473void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001474 DCHECK_EQ(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00001475 DCHECK(ContainsElement(dominated_blocks_, other));
1476 DCHECK_EQ(GetSingleSuccessor(), other);
1477 DCHECK_EQ(other->GetSinglePredecessor(), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001478 DCHECK(other->GetPhis().IsEmpty());
1479
David Brazdil2d7352b2015-04-20 14:52:42 +01001480 // Move instructions from `other` to `this`.
1481 DCHECK(EndsWithControlFlowInstruction());
1482 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001483 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001484 other->instructions_.SetBlockOfInstructions(this);
1485 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001486
David Brazdil2d7352b2015-04-20 14:52:42 +01001487 // Remove `other` from the loops it is included in.
1488 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1489 HLoopInformation* loop_info = it.Current();
1490 loop_info->Remove(other);
1491 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001492 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01001493 }
1494 }
1495
1496 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00001497 successors_.clear();
1498 while (!other->successors_.empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001499 HBasicBlock* successor = other->GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001500 successor->ReplacePredecessor(other, this);
1501 }
1502
David Brazdil2d7352b2015-04-20 14:52:42 +01001503 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00001504 RemoveDominatedBlock(other);
1505 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
1506 dominated_blocks_.push_back(dominated);
David Brazdil2d7352b2015-04-20 14:52:42 +01001507 dominated->SetDominator(this);
1508 }
Vladimir Marko60584552015-09-03 13:35:12 +00001509 other->dominated_blocks_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001510 other->dominator_ = nullptr;
1511
1512 // Clear the list of predecessors of `other` in preparation of deleting it.
Vladimir Marko60584552015-09-03 13:35:12 +00001513 other->predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001514
1515 // Delete `other` from the graph. The function updates reverse post order.
1516 graph_->DeleteDeadBlock(other);
1517 other->SetGraph(nullptr);
1518}
1519
1520void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1521 DCHECK_NE(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00001522 DCHECK(GetDominatedBlocks().empty());
1523 DCHECK(GetSuccessors().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001524 DCHECK(!EndsWithControlFlowInstruction());
Vladimir Marko60584552015-09-03 13:35:12 +00001525 DCHECK(other->GetSinglePredecessor()->IsEntryBlock());
David Brazdil2d7352b2015-04-20 14:52:42 +01001526 DCHECK(other->GetPhis().IsEmpty());
1527 DCHECK(!other->IsInLoop());
1528
1529 // Move instructions from `other` to `this`.
1530 instructions_.Add(other->GetInstructions());
1531 other->instructions_.SetBlockOfInstructions(this);
1532
1533 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00001534 successors_.clear();
1535 while (!other->successors_.empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001536 HBasicBlock* successor = other->GetSuccessors()[0];
David Brazdil2d7352b2015-04-20 14:52:42 +01001537 successor->ReplacePredecessor(other, this);
1538 }
1539
1540 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00001541 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
1542 dominated_blocks_.push_back(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001543 dominated->SetDominator(this);
1544 }
Vladimir Marko60584552015-09-03 13:35:12 +00001545 other->dominated_blocks_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001546 other->dominator_ = nullptr;
1547 other->graph_ = nullptr;
1548}
1549
1550void HBasicBlock::ReplaceWith(HBasicBlock* other) {
Vladimir Marko60584552015-09-03 13:35:12 +00001551 while (!GetPredecessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001552 HBasicBlock* predecessor = GetPredecessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001553 predecessor->ReplaceSuccessor(this, other);
1554 }
Vladimir Marko60584552015-09-03 13:35:12 +00001555 while (!GetSuccessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001556 HBasicBlock* successor = GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001557 successor->ReplacePredecessor(this, other);
1558 }
Vladimir Marko60584552015-09-03 13:35:12 +00001559 for (HBasicBlock* dominated : GetDominatedBlocks()) {
1560 other->AddDominatedBlock(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001561 }
1562 GetDominator()->ReplaceDominatedBlock(this, other);
1563 other->SetDominator(GetDominator());
1564 dominator_ = nullptr;
1565 graph_ = nullptr;
1566}
1567
1568// Create space in `blocks` for adding `number_of_new_blocks` entries
1569// starting at location `at`. Blocks after `at` are moved accordingly.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001570static void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001571 size_t number_of_new_blocks,
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001572 size_t after) {
1573 DCHECK_LT(after, blocks->size());
1574 size_t old_size = blocks->size();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001575 size_t new_size = old_size + number_of_new_blocks;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001576 blocks->resize(new_size);
1577 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001578}
1579
David Brazdil2d7352b2015-04-20 14:52:42 +01001580void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1581 DCHECK_EQ(block->GetGraph(), this);
Vladimir Marko60584552015-09-03 13:35:12 +00001582 DCHECK(block->GetSuccessors().empty());
1583 DCHECK(block->GetPredecessors().empty());
1584 DCHECK(block->GetDominatedBlocks().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001585 DCHECK(block->GetDominator() == nullptr);
1586
1587 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1588 block->RemoveInstruction(it.Current());
1589 }
1590 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1591 block->RemovePhi(it.Current()->AsPhi());
1592 }
1593
David Brazdilc7af85d2015-05-26 12:05:55 +01001594 if (block->IsExitBlock()) {
1595 exit_block_ = nullptr;
1596 }
1597
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001598 RemoveElement(reverse_post_order_, block);
1599 blocks_[block->GetBlockId()] = nullptr;
David Brazdil2d7352b2015-04-20 14:52:42 +01001600}
1601
Calin Juravle2e768302015-07-28 14:41:11 +00001602HInstruction* HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
David Brazdilc7af85d2015-05-26 12:05:55 +01001603 DCHECK(HasExitBlock()) << "Unimplemented scenario";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001604 // Update the environments in this graph to have the invoke's environment
1605 // as parent.
1606 {
1607 HReversePostOrderIterator it(*this);
1608 it.Advance(); // Skip the entry block, we do not need to update the entry's suspend check.
1609 for (; !it.Done(); it.Advance()) {
1610 HBasicBlock* block = it.Current();
1611 for (HInstructionIterator instr_it(block->GetInstructions());
1612 !instr_it.Done();
1613 instr_it.Advance()) {
1614 HInstruction* current = instr_it.Current();
1615 if (current->NeedsEnvironment()) {
1616 current->GetEnvironment()->SetAndCopyParentChain(
1617 outer_graph->GetArena(), invoke->GetEnvironment());
1618 }
1619 }
1620 }
1621 }
1622 outer_graph->UpdateMaximumNumberOfOutVRegs(GetMaximumNumberOfOutVRegs());
1623 if (HasBoundsChecks()) {
1624 outer_graph->SetHasBoundsChecks(true);
1625 }
1626
Calin Juravle2e768302015-07-28 14:41:11 +00001627 HInstruction* return_value = nullptr;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001628 if (GetBlocks().size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001629 // Simple case of an entry block, a body block, and an exit block.
1630 // Put the body block's instruction into `invoke`'s block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01001631 HBasicBlock* body = GetBlocks()[1];
1632 DCHECK(GetBlocks()[0]->IsEntryBlock());
1633 DCHECK(GetBlocks()[2]->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001634 DCHECK(!body->IsExitBlock());
1635 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001636
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001637 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1638 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001639
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001640 // Replace the invoke with the return value of the inlined graph.
1641 if (last->IsReturn()) {
Calin Juravle2e768302015-07-28 14:41:11 +00001642 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001643 } else {
1644 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001645 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001646
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001647 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001648 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001649 // Need to inline multiple blocks. We split `invoke`'s block
1650 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001651 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001652 // with the second half.
1653 ArenaAllocator* allocator = outer_graph->GetArena();
1654 HBasicBlock* at = invoke->GetBlock();
1655 HBasicBlock* to = at->SplitAfter(invoke);
1656
Vladimir Markoec7802a2015-10-01 20:57:57 +01001657 HBasicBlock* first = entry_block_->GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001658 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001659 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001660 exit_block_->ReplaceWith(to);
1661
1662 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001663 // to not `HReturn` but `HGoto` instead.
Vladimir Markoec7802a2015-10-01 20:57:57 +01001664 bool returns_void = to->GetPredecessors()[0]->GetLastInstruction()->IsReturnVoid();
Vladimir Marko60584552015-09-03 13:35:12 +00001665 if (to->GetPredecessors().size() == 1) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001666 HBasicBlock* predecessor = to->GetPredecessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001667 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001668 if (!returns_void) {
1669 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001670 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001671 predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc()));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001672 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001673 } else {
1674 if (!returns_void) {
1675 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001676 return_value = new (allocator) HPhi(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001677 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()), to->GetDexPc());
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001678 to->AddPhi(return_value->AsPhi());
1679 }
Vladimir Marko60584552015-09-03 13:35:12 +00001680 for (HBasicBlock* predecessor : to->GetPredecessors()) {
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001681 HInstruction* last = predecessor->GetLastInstruction();
1682 if (!returns_void) {
1683 return_value->AsPhi()->AddInput(last->InputAt(0));
1684 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001685 predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001686 predecessor->RemoveInstruction(last);
1687 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001688 }
1689
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001690 // Update the meta information surrounding blocks:
1691 // (1) the graph they are now in,
1692 // (2) the reverse post order of that graph,
David Brazdil95177982015-10-30 12:56:58 -05001693 // (3) the potential loop information they are now in,
1694 // (4) try block membership.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001695
1696 // We don't add the entry block, the exit block, and the first block, which
1697 // has been merged with `at`.
1698 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1699
1700 // We add the `to` block.
1701 static constexpr int kNumberOfNewBlocksInCaller = 1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001702 size_t blocks_added = (reverse_post_order_.size() - kNumberOfSkippedBlocksInCallee)
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001703 + kNumberOfNewBlocksInCaller;
1704
1705 // Find the location of `at` in the outer graph's reverse post order. The new
1706 // blocks will be added after it.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001707 size_t index_of_at = IndexOfElement(outer_graph->reverse_post_order_, at);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001708 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1709
David Brazdil95177982015-10-30 12:56:58 -05001710 HLoopInformation* loop_info = at->GetLoopInformation();
1711 // Copy TryCatchInformation if `at` is a try block, not if it is a catch block.
1712 TryCatchInformation* try_catch_info = at->IsTryBlock() ? at->GetTryCatchInformation() : nullptr;
1713
1714 // Do a reverse post order of the blocks in the callee and do (1), (2), (3)
1715 // and (4) to the blocks that apply.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001716 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1717 HBasicBlock* current = it.Current();
1718 if (current != exit_block_ && current != entry_block_ && current != first) {
1719 DCHECK(!current->IsInLoop());
David Brazdil95177982015-10-30 12:56:58 -05001720 DCHECK(current->GetTryCatchInformation() == nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001721 DCHECK(current->GetGraph() == this);
1722 current->SetGraph(outer_graph);
1723 outer_graph->AddBlock(current);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001724 outer_graph->reverse_post_order_[++index_of_at] = current;
David Brazdil95177982015-10-30 12:56:58 -05001725 if (loop_info != nullptr) {
1726 current->SetLoopInformation(loop_info);
David Brazdil7d275372015-04-21 16:36:35 +01001727 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1728 loop_it.Current()->Add(current);
1729 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001730 }
David Brazdil95177982015-10-30 12:56:58 -05001731 current->SetTryCatchInformation(try_catch_info);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001732 }
1733 }
1734
David Brazdil95177982015-10-30 12:56:58 -05001735 // Do (1), (2), (3) and (4) to `to`.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001736 to->SetGraph(outer_graph);
1737 outer_graph->AddBlock(to);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001738 outer_graph->reverse_post_order_[++index_of_at] = to;
David Brazdil95177982015-10-30 12:56:58 -05001739 if (loop_info != nullptr) {
1740 to->SetLoopInformation(loop_info);
David Brazdil7d275372015-04-21 16:36:35 +01001741 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1742 loop_it.Current()->Add(to);
1743 }
David Brazdil95177982015-10-30 12:56:58 -05001744 if (loop_info->IsBackEdge(*at)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001745 // Only `to` can become a back edge, as the inlined blocks
1746 // are predecessors of `to`.
David Brazdil95177982015-10-30 12:56:58 -05001747 loop_info->ReplaceBackEdge(at, to);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001748 }
1749 }
David Brazdil95177982015-10-30 12:56:58 -05001750 to->SetTryCatchInformation(try_catch_info);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001751 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001752
David Brazdil05144f42015-04-16 15:18:00 +01001753 // Update the next instruction id of the outer graph, so that instructions
1754 // added later get bigger ids than those in the inner graph.
1755 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1756
1757 // Walk over the entry block and:
1758 // - Move constants from the entry block to the outer_graph's entry block,
1759 // - Replace HParameterValue instructions with their real value.
1760 // - Remove suspend checks, that hold an environment.
1761 // We must do this after the other blocks have been inlined, otherwise ids of
1762 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001763 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001764 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1765 HInstruction* current = it.Current();
Calin Juravle214bbcd2015-10-20 14:54:07 +01001766 HInstruction* replacement = nullptr;
David Brazdil05144f42015-04-16 15:18:00 +01001767 if (current->IsNullConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001768 replacement = outer_graph->GetNullConstant(current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001769 } else if (current->IsIntConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001770 replacement = outer_graph->GetIntConstant(
1771 current->AsIntConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001772 } else if (current->IsLongConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001773 replacement = outer_graph->GetLongConstant(
1774 current->AsLongConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001775 } else if (current->IsFloatConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001776 replacement = outer_graph->GetFloatConstant(
1777 current->AsFloatConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001778 } else if (current->IsDoubleConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001779 replacement = outer_graph->GetDoubleConstant(
1780 current->AsDoubleConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001781 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001782 if (kIsDebugBuild
1783 && invoke->IsInvokeStaticOrDirect()
1784 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1785 // Ensure we do not use the last input of `invoke`, as it
1786 // contains a clinit check which is not an actual argument.
1787 size_t last_input_index = invoke->InputCount() - 1;
1788 DCHECK(parameter_index != last_input_index);
1789 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01001790 replacement = invoke->InputAt(parameter_index++);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001791 } else if (current->IsCurrentMethod()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001792 replacement = outer_graph->GetCurrentMethod();
David Brazdil05144f42015-04-16 15:18:00 +01001793 } else {
1794 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1795 entry_block_->RemoveInstruction(current);
1796 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01001797 if (replacement != nullptr) {
1798 current->ReplaceWith(replacement);
1799 // If the current is the return value then we need to update the latter.
1800 if (current == return_value) {
1801 DCHECK_EQ(entry_block_, return_value->GetBlock());
1802 return_value = replacement;
1803 }
1804 }
1805 }
1806
1807 if (return_value != nullptr) {
1808 invoke->ReplaceWith(return_value);
David Brazdil05144f42015-04-16 15:18:00 +01001809 }
1810
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001811 // Finally remove the invoke from the caller.
1812 invoke->GetBlock()->RemoveInstruction(invoke);
Calin Juravle2e768302015-07-28 14:41:11 +00001813
1814 return return_value;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001815}
1816
Mingyao Yang3584bce2015-05-19 16:01:59 -07001817/*
1818 * Loop will be transformed to:
1819 * old_pre_header
1820 * |
1821 * if_block
1822 * / \
1823 * dummy_block deopt_block
1824 * \ /
1825 * new_pre_header
1826 * |
1827 * header
1828 */
1829void HGraph::TransformLoopHeaderForBCE(HBasicBlock* header) {
1830 DCHECK(header->IsLoopHeader());
1831 HBasicBlock* pre_header = header->GetDominator();
1832
1833 // Need this to avoid critical edge.
1834 HBasicBlock* if_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1835 // Need this to avoid critical edge.
1836 HBasicBlock* dummy_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1837 HBasicBlock* deopt_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1838 HBasicBlock* new_pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
1839 AddBlock(if_block);
1840 AddBlock(dummy_block);
1841 AddBlock(deopt_block);
1842 AddBlock(new_pre_header);
1843
1844 header->ReplacePredecessor(pre_header, new_pre_header);
Vladimir Marko60584552015-09-03 13:35:12 +00001845 pre_header->successors_.clear();
1846 pre_header->dominated_blocks_.clear();
Mingyao Yang3584bce2015-05-19 16:01:59 -07001847
1848 pre_header->AddSuccessor(if_block);
1849 if_block->AddSuccessor(dummy_block); // True successor
1850 if_block->AddSuccessor(deopt_block); // False successor
1851 dummy_block->AddSuccessor(new_pre_header);
1852 deopt_block->AddSuccessor(new_pre_header);
1853
Vladimir Marko60584552015-09-03 13:35:12 +00001854 pre_header->dominated_blocks_.push_back(if_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001855 if_block->SetDominator(pre_header);
Vladimir Marko60584552015-09-03 13:35:12 +00001856 if_block->dominated_blocks_.push_back(dummy_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001857 dummy_block->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001858 if_block->dominated_blocks_.push_back(deopt_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001859 deopt_block->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001860 if_block->dominated_blocks_.push_back(new_pre_header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001861 new_pre_header->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001862 new_pre_header->dominated_blocks_.push_back(header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001863 header->SetDominator(new_pre_header);
1864
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001865 size_t index_of_header = IndexOfElement(reverse_post_order_, header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001866 MakeRoomFor(&reverse_post_order_, 4, index_of_header - 1);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001867 reverse_post_order_[index_of_header++] = if_block;
1868 reverse_post_order_[index_of_header++] = dummy_block;
1869 reverse_post_order_[index_of_header++] = deopt_block;
1870 reverse_post_order_[index_of_header++] = new_pre_header;
Mingyao Yang3584bce2015-05-19 16:01:59 -07001871
1872 HLoopInformation* info = pre_header->GetLoopInformation();
1873 if (info != nullptr) {
1874 if_block->SetLoopInformation(info);
1875 dummy_block->SetLoopInformation(info);
1876 deopt_block->SetLoopInformation(info);
1877 new_pre_header->SetLoopInformation(info);
1878 for (HLoopInformationOutwardIterator loop_it(*pre_header);
1879 !loop_it.Done();
1880 loop_it.Advance()) {
1881 loop_it.Current()->Add(if_block);
1882 loop_it.Current()->Add(dummy_block);
1883 loop_it.Current()->Add(deopt_block);
1884 loop_it.Current()->Add(new_pre_header);
1885 }
1886 }
1887}
1888
Calin Juravle2e768302015-07-28 14:41:11 +00001889void HInstruction::SetReferenceTypeInfo(ReferenceTypeInfo rti) {
1890 if (kIsDebugBuild) {
1891 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1892 ScopedObjectAccess soa(Thread::Current());
1893 DCHECK(rti.IsValid()) << "Invalid RTI for " << DebugName();
1894 if (IsBoundType()) {
1895 // Having the test here spares us from making the method virtual just for
1896 // the sake of a DCHECK.
1897 ReferenceTypeInfo upper_bound_rti = AsBoundType()->GetUpperBound();
1898 DCHECK(upper_bound_rti.IsSupertypeOf(rti))
1899 << " upper_bound_rti: " << upper_bound_rti
1900 << " rti: " << rti;
David Brazdilbaf89b82015-09-15 11:36:54 +01001901 DCHECK(!upper_bound_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes() || rti.IsExact());
Calin Juravle2e768302015-07-28 14:41:11 +00001902 }
1903 }
1904 reference_type_info_ = rti;
1905}
1906
1907ReferenceTypeInfo::ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
1908
1909ReferenceTypeInfo::ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
1910 : type_handle_(type_handle), is_exact_(is_exact) {
1911 if (kIsDebugBuild) {
1912 ScopedObjectAccess soa(Thread::Current());
1913 DCHECK(IsValidHandle(type_handle));
1914 }
1915}
1916
Calin Juravleacf735c2015-02-12 15:25:22 +00001917std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1918 ScopedObjectAccess soa(Thread::Current());
1919 os << "["
Calin Juravle2e768302015-07-28 14:41:11 +00001920 << " is_valid=" << rhs.IsValid()
1921 << " type=" << (!rhs.IsValid() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
Calin Juravleacf735c2015-02-12 15:25:22 +00001922 << " is_exact=" << rhs.IsExact()
1923 << " ]";
1924 return os;
1925}
1926
Mark Mendellc4701932015-04-10 13:18:51 -04001927bool HInstruction::HasAnyEnvironmentUseBefore(HInstruction* other) {
1928 // For now, assume that instructions in different blocks may use the
1929 // environment.
1930 // TODO: Use the control flow to decide if this is true.
1931 if (GetBlock() != other->GetBlock()) {
1932 return true;
1933 }
1934
1935 // We know that we are in the same block. Walk from 'this' to 'other',
1936 // checking to see if there is any instruction with an environment.
1937 HInstruction* current = this;
1938 for (; current != other && current != nullptr; current = current->GetNext()) {
1939 // This is a conservative check, as the instruction result may not be in
1940 // the referenced environment.
1941 if (current->HasEnvironment()) {
1942 return true;
1943 }
1944 }
1945
1946 // We should have been called with 'this' before 'other' in the block.
1947 // Just confirm this.
1948 DCHECK(current != nullptr);
1949 return false;
1950}
1951
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001952void HInvoke::SetIntrinsic(Intrinsics intrinsic,
1953 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
1954 intrinsic_ = intrinsic;
1955 IntrinsicOptimizations opt(this);
1956 if (needs_env_or_cache == kNoEnvironmentOrCache) {
1957 opt.SetDoesNotNeedDexCache();
1958 opt.SetDoesNotNeedEnvironment();
1959 }
1960}
1961
1962bool HInvoke::NeedsEnvironment() const {
1963 if (!IsIntrinsic()) {
1964 return true;
1965 }
1966 IntrinsicOptimizations opt(*this);
1967 return !opt.GetDoesNotNeedEnvironment();
1968}
1969
Vladimir Markodc151b22015-10-15 18:02:30 +01001970bool HInvokeStaticOrDirect::NeedsDexCacheOfDeclaringClass() const {
1971 if (GetMethodLoadKind() != MethodLoadKind::kDexCacheViaMethod) {
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001972 return false;
1973 }
1974 if (!IsIntrinsic()) {
1975 return true;
1976 }
1977 IntrinsicOptimizations opt(*this);
1978 return !opt.GetDoesNotNeedDexCache();
1979}
1980
Mark Mendellc4701932015-04-10 13:18:51 -04001981void HInstruction::RemoveEnvironmentUsers() {
1982 for (HUseIterator<HEnvironment*> use_it(GetEnvUses()); !use_it.Done(); use_it.Advance()) {
1983 HUseListNode<HEnvironment*>* user_node = use_it.Current();
1984 HEnvironment* user = user_node->GetUser();
1985 user->SetRawEnvAt(user_node->GetIndex(), nullptr);
1986 }
1987 env_uses_.Clear();
1988}
1989
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001990} // namespace art