blob: 4d79b5577115900e504a21de701bfe7e109989af [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "nodes.h"
Calin Juravle77520bc2015-01-12 18:45:46 +000018
Mark Mendelle82549b2015-05-06 10:55:34 -040019#include "code_generator.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010020#include "ssa_builder.h"
David Brazdila4b8c212015-05-07 09:59:30 +010021#include "base/bit_vector-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010022#include "base/bit_utils.h"
Vladimir Marko1f8695c2015-09-24 13:11:31 +010023#include "base/stl_util.h"
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010024#include "intrinsics.h"
David Brazdilbaf89b82015-09-15 11:36:54 +010025#include "mirror/class-inl.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026#include "scoped_thread_state_change.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000027
28namespace art {
29
30void HGraph::AddBlock(HBasicBlock* block) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010031 block->SetBlockId(blocks_.size());
32 blocks_.push_back(block);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033}
34
Nicolas Geoffray804d0932014-05-02 08:46:00 +010035void HGraph::FindBackEdges(ArenaBitVector* visited) {
Vladimir Marko1f8695c2015-09-24 13:11:31 +010036 // "visited" must be empty on entry, it's an output argument for all visited (i.e. live) blocks.
37 DCHECK_EQ(visited->GetHighestBitSet(), -1);
38
39 // Nodes that we're currently visiting, indexed by block id.
Vladimir Markofa6b93c2015-09-15 10:15:55 +010040 ArenaBitVector visiting(arena_, blocks_.size(), false);
Vladimir Marko1f8695c2015-09-24 13:11:31 +010041 // Number of successors visited from a given node, indexed by block id.
42 ArenaVector<size_t> successors_visited(blocks_.size(), 0u, arena_->Adapter());
43 // Stack of nodes that we're currently visiting (same as marked in "visiting" above).
44 ArenaVector<HBasicBlock*> worklist(arena_->Adapter());
45 constexpr size_t kDefaultWorklistSize = 8;
46 worklist.reserve(kDefaultWorklistSize);
47 visited->SetBit(entry_block_->GetBlockId());
48 visiting.SetBit(entry_block_->GetBlockId());
49 worklist.push_back(entry_block_);
50
51 while (!worklist.empty()) {
52 HBasicBlock* current = worklist.back();
53 uint32_t current_id = current->GetBlockId();
54 if (successors_visited[current_id] == current->GetSuccessors().size()) {
55 visiting.ClearBit(current_id);
56 worklist.pop_back();
57 } else {
Vladimir Marko1f8695c2015-09-24 13:11:31 +010058 HBasicBlock* successor = current->GetSuccessors()[successors_visited[current_id]++];
59 uint32_t successor_id = successor->GetBlockId();
60 if (visiting.IsBitSet(successor_id)) {
61 DCHECK(ContainsElement(worklist, successor));
62 successor->AddBackEdge(current);
63 } else if (!visited->IsBitSet(successor_id)) {
64 visited->SetBit(successor_id);
65 visiting.SetBit(successor_id);
66 worklist.push_back(successor);
67 }
68 }
69 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000070}
71
Roland Levillainfc600dc2014-12-02 17:16:31 +000072static void RemoveAsUser(HInstruction* instruction) {
73 for (size_t i = 0; i < instruction->InputCount(); i++) {
David Brazdil1abb4192015-02-17 18:33:36 +000074 instruction->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000075 }
76
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010077 for (HEnvironment* environment = instruction->GetEnvironment();
78 environment != nullptr;
79 environment = environment->GetParent()) {
Roland Levillainfc600dc2014-12-02 17:16:31 +000080 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
David Brazdil1abb4192015-02-17 18:33:36 +000081 if (environment->GetInstructionAt(i) != nullptr) {
82 environment->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000083 }
84 }
85 }
86}
87
88void HGraph::RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010089 for (size_t i = 0; i < blocks_.size(); ++i) {
Roland Levillainfc600dc2014-12-02 17:16:31 +000090 if (!visited.IsBitSet(i)) {
Vladimir Markoec7802a2015-10-01 20:57:57 +010091 HBasicBlock* block = blocks_[i];
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010092 DCHECK(block->GetPhis().IsEmpty()) << "Phis are not inserted at this stage";
Roland Levillainfc600dc2014-12-02 17:16:31 +000093 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
94 RemoveAsUser(it.Current());
95 }
96 }
97 }
98}
99
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100100void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100101 for (size_t i = 0; i < blocks_.size(); ++i) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000102 if (!visited.IsBitSet(i)) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100103 HBasicBlock* block = blocks_[i];
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100104 // We only need to update the successor, which might be live.
Vladimir Marko60584552015-09-03 13:35:12 +0000105 for (HBasicBlock* successor : block->GetSuccessors()) {
106 successor->RemovePredecessor(block);
David Brazdil1abb4192015-02-17 18:33:36 +0000107 }
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100108 // Remove the block from the list of blocks, so that further analyses
109 // never see it.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100110 blocks_[i] = nullptr;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000111 }
112 }
113}
114
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000115void HGraph::BuildDominatorTree() {
David Brazdilffee3d32015-07-06 11:48:53 +0100116 // (1) Simplify the CFG so that catch blocks have only exceptional incoming
117 // edges. This invariant simplifies building SSA form because Phis cannot
118 // collect both normal- and exceptional-flow values at the same time.
119 SimplifyCatchBlocks();
120
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100121 ArenaBitVector visited(arena_, blocks_.size(), false);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000122
David Brazdilffee3d32015-07-06 11:48:53 +0100123 // (2) Find the back edges in the graph doing a DFS traversal.
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000124 FindBackEdges(&visited);
125
David Brazdilffee3d32015-07-06 11:48:53 +0100126 // (3) Remove instructions and phis from blocks not visited during
Roland Levillainfc600dc2014-12-02 17:16:31 +0000127 // the initial DFS as users from other instructions, so that
128 // users can be safely removed before uses later.
129 RemoveInstructionsAsUsersFromDeadBlocks(visited);
130
David Brazdilffee3d32015-07-06 11:48:53 +0100131 // (4) Remove blocks not visited during the initial DFS.
Roland Levillainfc600dc2014-12-02 17:16:31 +0000132 // Step (4) requires dead blocks to be removed from the
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000133 // predecessors list of live blocks.
134 RemoveDeadBlocks(visited);
135
David Brazdilffee3d32015-07-06 11:48:53 +0100136 // (5) Simplify the CFG now, so that we don't need to recompute
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100137 // dominators and the reverse post order.
138 SimplifyCFG();
139
David Brazdilffee3d32015-07-06 11:48:53 +0100140 // (6) Compute the dominance information and the reverse post order.
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100141 ComputeDominanceInformation();
142}
143
144void HGraph::ClearDominanceInformation() {
145 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
146 it.Current()->ClearDominanceInformation();
147 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100148 reverse_post_order_.clear();
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100149}
150
151void HBasicBlock::ClearDominanceInformation() {
Vladimir Marko60584552015-09-03 13:35:12 +0000152 dominated_blocks_.clear();
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100153 dominator_ = nullptr;
154}
155
156void HGraph::ComputeDominanceInformation() {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100157 DCHECK(reverse_post_order_.empty());
158 reverse_post_order_.reserve(blocks_.size());
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100159 reverse_post_order_.push_back(entry_block_);
Vladimir Markod76d1392015-09-23 16:07:14 +0100160
161 // Number of visits of a given node, indexed by block id.
162 ArenaVector<size_t> visits(blocks_.size(), 0u, arena_->Adapter());
163 // Number of successors visited from a given node, indexed by block id.
164 ArenaVector<size_t> successors_visited(blocks_.size(), 0u, arena_->Adapter());
165 // Nodes for which we need to visit successors.
166 ArenaVector<HBasicBlock*> worklist(arena_->Adapter());
167 constexpr size_t kDefaultWorklistSize = 8;
168 worklist.reserve(kDefaultWorklistSize);
169 worklist.push_back(entry_block_);
170
171 while (!worklist.empty()) {
172 HBasicBlock* current = worklist.back();
173 uint32_t current_id = current->GetBlockId();
174 if (successors_visited[current_id] == current->GetSuccessors().size()) {
175 worklist.pop_back();
176 } else {
Vladimir Markod76d1392015-09-23 16:07:14 +0100177 HBasicBlock* successor = current->GetSuccessors()[successors_visited[current_id]++];
178
179 if (successor->GetDominator() == nullptr) {
180 successor->SetDominator(current);
181 } else {
182 successor->SetDominator(FindCommonDominator(successor->GetDominator(), current));
183 }
184
185 // Once all the forward edges have been visited, we know the immediate
186 // dominator of the block. We can then start visiting its successors.
Vladimir Markod76d1392015-09-23 16:07:14 +0100187 if (++visits[successor->GetBlockId()] ==
188 successor->GetPredecessors().size() - successor->NumberOfBackEdges()) {
189 successor->GetDominator()->AddDominatedBlock(successor);
190 reverse_post_order_.push_back(successor);
191 worklist.push_back(successor);
192 }
193 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000194 }
195}
196
197HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100198 ArenaBitVector visited(arena_, blocks_.size(), false);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000199 // Walk the dominator tree of the first block and mark the visited blocks.
200 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000201 visited.SetBit(first->GetBlockId());
202 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000203 }
204 // Walk the dominator tree of the second block until a marked block is found.
205 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000206 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000207 return second;
208 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000209 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000210 }
211 LOG(ERROR) << "Could not find common dominator";
212 return nullptr;
213}
214
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215void HGraph::TransformToSsa() {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100216 DCHECK(!reverse_post_order_.empty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100217 SsaBuilder ssa_builder(this);
218 ssa_builder.BuildSsa();
219}
220
David Brazdilfc6a86a2015-06-26 10:33:45 +0000221HBasicBlock* HGraph::SplitEdge(HBasicBlock* block, HBasicBlock* successor) {
David Brazdil3e187382015-06-26 09:59:52 +0000222 HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc());
223 AddBlock(new_block);
David Brazdil3e187382015-06-26 09:59:52 +0000224 // Use `InsertBetween` to ensure the predecessor index and successor index of
225 // `block` and `successor` are preserved.
226 new_block->InsertBetween(block, successor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000227 return new_block;
228}
229
230void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) {
231 // Insert a new node between `block` and `successor` to split the
232 // critical edge.
233 HBasicBlock* new_block = SplitEdge(block, successor);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600234 new_block->AddInstruction(new (arena_) HGoto(successor->GetDexPc()));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100235 if (successor->IsLoopHeader()) {
236 // If we split at a back edge boundary, make the new block the back edge.
237 HLoopInformation* info = successor->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000238 if (info->IsBackEdge(*block)) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100239 info->RemoveBackEdge(block);
240 info->AddBackEdge(new_block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100241 }
242 }
243}
244
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100245void HGraph::SimplifyLoop(HBasicBlock* header) {
246 HLoopInformation* info = header->GetLoopInformation();
247
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100248 // Make sure the loop has only one pre header. This simplifies SSA building by having
249 // to just look at the pre header to know which locals are initialized at entry of the
250 // loop.
Vladimir Marko60584552015-09-03 13:35:12 +0000251 size_t number_of_incomings = header->GetPredecessors().size() - info->NumberOfBackEdges();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100252 if (number_of_incomings != 1) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100253 HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100254 AddBlock(pre_header);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600255 pre_header->AddInstruction(new (arena_) HGoto(header->GetDexPc()));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100256
Vladimir Marko60584552015-09-03 13:35:12 +0000257 for (size_t pred = 0; pred < header->GetPredecessors().size(); ++pred) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100258 HBasicBlock* predecessor = header->GetPredecessors()[pred];
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100259 if (!info->IsBackEdge(*predecessor)) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100260 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100261 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100262 }
263 }
264 pre_header->AddSuccessor(header);
265 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100266
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100267 // Make sure the first predecessor of a loop header is the incoming block.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100268 if (info->IsBackEdge(*header->GetPredecessors()[0])) {
269 HBasicBlock* to_swap = header->GetPredecessors()[0];
Vladimir Marko60584552015-09-03 13:35:12 +0000270 for (size_t pred = 1, e = header->GetPredecessors().size(); pred < e; ++pred) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100271 HBasicBlock* predecessor = header->GetPredecessors()[pred];
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100272 if (!info->IsBackEdge(*predecessor)) {
Vladimir Marko60584552015-09-03 13:35:12 +0000273 header->predecessors_[pred] = to_swap;
274 header->predecessors_[0] = predecessor;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100275 break;
276 }
277 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100278 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100279
280 // Place the suspend check at the beginning of the header, so that live registers
281 // will be known when allocating registers. Note that code generation can still
282 // generate the suspend check at the back edge, but needs to be careful with
283 // loop phi spill slots (which are not written to at back edge).
284 HInstruction* first_instruction = header->GetFirstInstruction();
285 if (!first_instruction->IsSuspendCheck()) {
286 HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc());
287 header->InsertInstructionBefore(check, first_instruction);
288 first_instruction = check;
289 }
290 info->SetSuspendCheck(first_instruction->AsSuspendCheck());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100291}
292
David Brazdilffee3d32015-07-06 11:48:53 +0100293static bool CheckIfPredecessorAtIsExceptional(const HBasicBlock& block, size_t pred_idx) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100294 HBasicBlock* predecessor = block.GetPredecessors()[pred_idx];
David Brazdilffee3d32015-07-06 11:48:53 +0100295 if (!predecessor->EndsWithTryBoundary()) {
296 // Only edges from HTryBoundary can be exceptional.
297 return false;
298 }
299 HTryBoundary* try_boundary = predecessor->GetLastInstruction()->AsTryBoundary();
300 if (try_boundary->GetNormalFlowSuccessor() == &block) {
301 // This block is the normal-flow successor of `try_boundary`, but it could
302 // also be one of its exception handlers if catch blocks have not been
303 // simplified yet. Predecessors are unordered, so we will consider the first
304 // occurrence to be the normal edge and a possible second occurrence to be
305 // the exceptional edge.
306 return !block.IsFirstIndexOfPredecessor(predecessor, pred_idx);
307 } else {
308 // This is not the normal-flow successor of `try_boundary`, hence it must be
309 // one of its exception handlers.
310 DCHECK(try_boundary->HasExceptionHandler(block));
311 return true;
312 }
313}
314
315void HGraph::SimplifyCatchBlocks() {
Vladimir Markob7d8e8c2015-09-17 15:47:05 +0100316 // NOTE: We're appending new blocks inside the loop, so we need to use index because iterators
317 // can be invalidated. We remember the initial size to avoid iterating over the new blocks.
318 for (size_t block_id = 0u, end = blocks_.size(); block_id != end; ++block_id) {
319 HBasicBlock* catch_block = blocks_[block_id];
David Brazdilffee3d32015-07-06 11:48:53 +0100320 if (!catch_block->IsCatchBlock()) {
321 continue;
322 }
323
324 bool exceptional_predecessors_only = true;
Vladimir Marko60584552015-09-03 13:35:12 +0000325 for (size_t j = 0; j < catch_block->GetPredecessors().size(); ++j) {
David Brazdilffee3d32015-07-06 11:48:53 +0100326 if (!CheckIfPredecessorAtIsExceptional(*catch_block, j)) {
327 exceptional_predecessors_only = false;
328 break;
329 }
330 }
331
332 if (!exceptional_predecessors_only) {
333 // Catch block has normal-flow predecessors and needs to be simplified.
334 // Splitting the block before its first instruction moves all its
335 // instructions into `normal_block` and links the two blocks with a Goto.
336 // Afterwards, incoming normal-flow edges are re-linked to `normal_block`,
337 // leaving `catch_block` with the exceptional edges only.
338 // Note that catch blocks with normal-flow predecessors cannot begin with
339 // a MOVE_EXCEPTION instruction, as guaranteed by the verifier.
340 DCHECK(!catch_block->GetFirstInstruction()->IsLoadException());
341 HBasicBlock* normal_block = catch_block->SplitBefore(catch_block->GetFirstInstruction());
Vladimir Marko60584552015-09-03 13:35:12 +0000342 for (size_t j = 0; j < catch_block->GetPredecessors().size(); ++j) {
David Brazdilffee3d32015-07-06 11:48:53 +0100343 if (!CheckIfPredecessorAtIsExceptional(*catch_block, j)) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100344 catch_block->GetPredecessors()[j]->ReplaceSuccessor(catch_block, normal_block);
David Brazdilffee3d32015-07-06 11:48:53 +0100345 --j;
346 }
347 }
348 }
349 }
350}
351
352void HGraph::ComputeTryBlockInformation() {
353 // Iterate in reverse post order to propagate try membership information from
354 // predecessors to their successors.
355 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
356 HBasicBlock* block = it.Current();
357 if (block->IsEntryBlock() || block->IsCatchBlock()) {
358 // Catch blocks after simplification have only exceptional predecessors
359 // and hence are never in tries.
360 continue;
361 }
362
363 // Infer try membership from the first predecessor. Having simplified loops,
364 // the first predecessor can never be a back edge and therefore it must have
365 // been visited already and had its try membership set.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100366 HBasicBlock* first_predecessor = block->GetPredecessors()[0];
David Brazdilffee3d32015-07-06 11:48:53 +0100367 DCHECK(!block->IsLoopHeader() || !block->GetLoopInformation()->IsBackEdge(*first_predecessor));
David Brazdilec16f792015-08-19 15:04:01 +0100368 const HTryBoundary* try_entry = first_predecessor->ComputeTryEntryOfSuccessors();
David Brazdilce529012015-10-28 19:25:55 -0500369 if (try_entry != nullptr &&
370 (block->GetTryCatchInformation() == nullptr ||
371 try_entry != &block->GetTryCatchInformation()->GetTryEntry())) {
372 // We are either setting try block membership for the first time or it
373 // has changed.
David Brazdilec16f792015-08-19 15:04:01 +0100374 block->SetTryCatchInformation(new (arena_) TryCatchInformation(*try_entry));
375 }
David Brazdilffee3d32015-07-06 11:48:53 +0100376 }
377}
378
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100379void HGraph::SimplifyCFG() {
380 // Simplify the CFG for future analysis, and code generation:
381 // (1): Split critical edges.
382 // (2): Simplify loops by having only one back edge, and one preheader.
Vladimir Markob7d8e8c2015-09-17 15:47:05 +0100383 // NOTE: We're appending new blocks inside the loop, so we need to use index because iterators
384 // can be invalidated. We remember the initial size to avoid iterating over the new blocks.
385 for (size_t block_id = 0u, end = blocks_.size(); block_id != end; ++block_id) {
386 HBasicBlock* block = blocks_[block_id];
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100387 if (block == nullptr) continue;
David Brazdilffee3d32015-07-06 11:48:53 +0100388 if (block->NumberOfNormalSuccessors() > 1) {
Vladimir Marko60584552015-09-03 13:35:12 +0000389 for (size_t j = 0; j < block->GetSuccessors().size(); ++j) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100390 HBasicBlock* successor = block->GetSuccessors()[j];
David Brazdilffee3d32015-07-06 11:48:53 +0100391 DCHECK(!successor->IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +0000392 if (successor->GetPredecessors().size() > 1) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100393 SplitCriticalEdge(block, successor);
394 --j;
395 }
396 }
397 }
398 if (block->IsLoopHeader()) {
399 SimplifyLoop(block);
400 }
401 }
402}
403
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000404bool HGraph::AnalyzeNaturalLoops() const {
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100405 // Order does not matter.
406 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
407 HBasicBlock* block = it.Current();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100408 if (block->IsLoopHeader()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100409 if (block->IsCatchBlock()) {
410 // TODO: Dealing with exceptional back edges could be tricky because
411 // they only approximate the real control flow. Bail out for now.
412 return false;
413 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100414 HLoopInformation* info = block->GetLoopInformation();
415 if (!info->Populate()) {
416 // Abort if the loop is non natural. We currently bailout in such cases.
417 return false;
418 }
419 }
420 }
421 return true;
422}
423
David Brazdil8d5b8b22015-03-24 10:51:52 +0000424void HGraph::InsertConstant(HConstant* constant) {
425 // New constants are inserted before the final control-flow instruction
426 // of the graph, or at its end if called from the graph builder.
427 if (entry_block_->EndsWithControlFlowInstruction()) {
428 entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction());
David Brazdil46e2a392015-03-16 17:31:52 +0000429 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000430 entry_block_->AddInstruction(constant);
David Brazdil46e2a392015-03-16 17:31:52 +0000431 }
432}
433
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600434HNullConstant* HGraph::GetNullConstant(uint32_t dex_pc) {
Nicolas Geoffray18e68732015-06-17 23:09:05 +0100435 // For simplicity, don't bother reviving the cached null constant if it is
436 // not null and not in a block. Otherwise, we need to clear the instruction
437 // id and/or any invariants the graph is assuming when adding new instructions.
438 if ((cached_null_constant_ == nullptr) || (cached_null_constant_->GetBlock() == nullptr)) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600439 cached_null_constant_ = new (arena_) HNullConstant(dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000440 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000441 }
442 return cached_null_constant_;
443}
444
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100445HCurrentMethod* HGraph::GetCurrentMethod() {
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100446 // For simplicity, don't bother reviving the cached current method if it is
447 // not null and not in a block. Otherwise, we need to clear the instruction
448 // id and/or any invariants the graph is assuming when adding new instructions.
449 if ((cached_current_method_ == nullptr) || (cached_current_method_->GetBlock() == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700450 cached_current_method_ = new (arena_) HCurrentMethod(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600451 Is64BitInstructionSet(instruction_set_) ? Primitive::kPrimLong : Primitive::kPrimInt,
452 entry_block_->GetDexPc());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100453 if (entry_block_->GetFirstInstruction() == nullptr) {
454 entry_block_->AddInstruction(cached_current_method_);
455 } else {
456 entry_block_->InsertInstructionBefore(
457 cached_current_method_, entry_block_->GetFirstInstruction());
458 }
459 }
460 return cached_current_method_;
461}
462
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600463HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000464 switch (type) {
465 case Primitive::Type::kPrimBoolean:
466 DCHECK(IsUint<1>(value));
467 FALLTHROUGH_INTENDED;
468 case Primitive::Type::kPrimByte:
469 case Primitive::Type::kPrimChar:
470 case Primitive::Type::kPrimShort:
471 case Primitive::Type::kPrimInt:
472 DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600473 return GetIntConstant(static_cast<int32_t>(value), dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000474
475 case Primitive::Type::kPrimLong:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600476 return GetLongConstant(value, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000477
478 default:
479 LOG(FATAL) << "Unsupported constant type";
480 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000481 }
David Brazdil46e2a392015-03-16 17:31:52 +0000482}
483
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000484void HGraph::CacheFloatConstant(HFloatConstant* constant) {
485 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
486 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
487 cached_float_constants_.Overwrite(value, constant);
488}
489
490void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
491 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
492 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
493 cached_double_constants_.Overwrite(value, constant);
494}
495
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000496void HLoopInformation::Add(HBasicBlock* block) {
497 blocks_.SetBit(block->GetBlockId());
498}
499
David Brazdil46e2a392015-03-16 17:31:52 +0000500void HLoopInformation::Remove(HBasicBlock* block) {
501 blocks_.ClearBit(block->GetBlockId());
502}
503
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100504void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
505 if (blocks_.IsBitSet(block->GetBlockId())) {
506 return;
507 }
508
509 blocks_.SetBit(block->GetBlockId());
510 block->SetInLoop(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000511 for (HBasicBlock* predecessor : block->GetPredecessors()) {
512 PopulateRecursive(predecessor);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100513 }
514}
515
516bool HLoopInformation::Populate() {
David Brazdila4b8c212015-05-07 09:59:30 +0100517 DCHECK_EQ(blocks_.NumSetBits(), 0u) << "Loop information has already been populated";
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100518 for (HBasicBlock* back_edge : GetBackEdges()) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100519 DCHECK(back_edge->GetDominator() != nullptr);
520 if (!header_->Dominates(back_edge)) {
521 // This loop is not natural. Do not bother going further.
522 return false;
523 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100524
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100525 // Populate this loop: starting with the back edge, recursively add predecessors
526 // that are not already part of that loop. Set the header as part of the loop
527 // to end the recursion.
528 // This is a recursive implementation of the algorithm described in
529 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
530 blocks_.SetBit(header_->GetBlockId());
531 PopulateRecursive(back_edge);
532 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100533 return true;
534}
535
David Brazdila4b8c212015-05-07 09:59:30 +0100536void HLoopInformation::Update() {
537 HGraph* graph = header_->GetGraph();
538 for (uint32_t id : blocks_.Indexes()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100539 HBasicBlock* block = graph->GetBlocks()[id];
David Brazdila4b8c212015-05-07 09:59:30 +0100540 // Reset loop information of non-header blocks inside the loop, except
541 // members of inner nested loops because those should already have been
542 // updated by their own LoopInformation.
543 if (block->GetLoopInformation() == this && block != header_) {
544 block->SetLoopInformation(nullptr);
545 }
546 }
547 blocks_.ClearAllBits();
548
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100549 if (back_edges_.empty()) {
David Brazdila4b8c212015-05-07 09:59:30 +0100550 // The loop has been dismantled, delete its suspend check and remove info
551 // from the header.
552 DCHECK(HasSuspendCheck());
553 header_->RemoveInstruction(suspend_check_);
554 header_->SetLoopInformation(nullptr);
555 header_ = nullptr;
556 suspend_check_ = nullptr;
557 } else {
558 if (kIsDebugBuild) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100559 for (HBasicBlock* back_edge : back_edges_) {
560 DCHECK(header_->Dominates(back_edge));
David Brazdila4b8c212015-05-07 09:59:30 +0100561 }
562 }
563 // This loop still has reachable back edges. Repopulate the list of blocks.
564 bool populate_successful = Populate();
565 DCHECK(populate_successful);
566 }
567}
568
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100569HBasicBlock* HLoopInformation::GetPreHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100570 return header_->GetDominator();
571}
572
573bool HLoopInformation::Contains(const HBasicBlock& block) const {
574 return blocks_.IsBitSet(block.GetBlockId());
575}
576
577bool HLoopInformation::IsIn(const HLoopInformation& other) const {
578 return other.blocks_.IsBitSet(header_->GetBlockId());
579}
580
Aart Bik73f1f3b2015-10-28 15:28:08 -0700581bool HLoopInformation::IsLoopInvariant(HInstruction* instruction, bool must_dominate) const {
582 HLoopInformation* other_loop = instruction->GetBlock()->GetLoopInformation();
583 if (other_loop != this && (other_loop == nullptr || !other_loop->IsIn(*this))) {
584 if (must_dominate) {
585 return instruction->GetBlock()->Dominates(GetHeader());
586 }
587 return true;
588 }
589 return false;
590}
591
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100592size_t HLoopInformation::GetLifetimeEnd() const {
593 size_t last_position = 0;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100594 for (HBasicBlock* back_edge : GetBackEdges()) {
595 last_position = std::max(back_edge->GetLifetimeEnd(), last_position);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100596 }
597 return last_position;
598}
599
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100600bool HBasicBlock::Dominates(HBasicBlock* other) const {
601 // Walk up the dominator tree from `other`, to find out if `this`
602 // is an ancestor.
603 HBasicBlock* current = other;
604 while (current != nullptr) {
605 if (current == this) {
606 return true;
607 }
608 current = current->GetDominator();
609 }
610 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100611}
612
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100613static void UpdateInputsUsers(HInstruction* instruction) {
614 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
615 instruction->InputAt(i)->AddUseAt(instruction, i);
616 }
617 // Environment should be created later.
618 DCHECK(!instruction->HasEnvironment());
619}
620
Roland Levillainccc07a92014-09-16 14:48:16 +0100621void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
622 HInstruction* replacement) {
623 DCHECK(initial->GetBlock() == this);
Mark Mendell805b3b52015-09-18 14:10:29 -0400624 if (initial->IsControlFlow()) {
625 // We can only replace a control flow instruction with another control flow instruction.
626 DCHECK(replacement->IsControlFlow());
627 DCHECK_EQ(replacement->GetId(), -1);
628 DCHECK_EQ(replacement->GetType(), Primitive::kPrimVoid);
629 DCHECK_EQ(initial->GetBlock(), this);
630 DCHECK_EQ(initial->GetType(), Primitive::kPrimVoid);
631 DCHECK(initial->GetUses().IsEmpty());
632 DCHECK(initial->GetEnvUses().IsEmpty());
633 replacement->SetBlock(this);
634 replacement->SetId(GetGraph()->GetNextInstructionId());
635 instructions_.InsertInstructionBefore(replacement, initial);
636 UpdateInputsUsers(replacement);
637 } else {
638 InsertInstructionBefore(replacement, initial);
639 initial->ReplaceWith(replacement);
640 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100641 RemoveInstruction(initial);
642}
643
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100644static void Add(HInstructionList* instruction_list,
645 HBasicBlock* block,
646 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000647 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000648 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100649 instruction->SetBlock(block);
650 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100651 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100652 instruction_list->AddInstruction(instruction);
653}
654
655void HBasicBlock::AddInstruction(HInstruction* instruction) {
656 Add(&instructions_, this, instruction);
657}
658
659void HBasicBlock::AddPhi(HPhi* phi) {
660 Add(&phis_, this, phi);
661}
662
David Brazdilc3d743f2015-04-22 13:40:50 +0100663void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
664 DCHECK(!cursor->IsPhi());
665 DCHECK(!instruction->IsPhi());
666 DCHECK_EQ(instruction->GetId(), -1);
667 DCHECK_NE(cursor->GetId(), -1);
668 DCHECK_EQ(cursor->GetBlock(), this);
669 DCHECK(!instruction->IsControlFlow());
670 instruction->SetBlock(this);
671 instruction->SetId(GetGraph()->GetNextInstructionId());
672 UpdateInputsUsers(instruction);
673 instructions_.InsertInstructionBefore(instruction, cursor);
674}
675
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100676void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
677 DCHECK(!cursor->IsPhi());
678 DCHECK(!instruction->IsPhi());
679 DCHECK_EQ(instruction->GetId(), -1);
680 DCHECK_NE(cursor->GetId(), -1);
681 DCHECK_EQ(cursor->GetBlock(), this);
682 DCHECK(!instruction->IsControlFlow());
683 DCHECK(!cursor->IsControlFlow());
684 instruction->SetBlock(this);
685 instruction->SetId(GetGraph()->GetNextInstructionId());
686 UpdateInputsUsers(instruction);
687 instructions_.InsertInstructionAfter(instruction, cursor);
688}
689
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100690void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
691 DCHECK_EQ(phi->GetId(), -1);
692 DCHECK_NE(cursor->GetId(), -1);
693 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100694 phi->SetBlock(this);
695 phi->SetId(GetGraph()->GetNextInstructionId());
696 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100697 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100698}
699
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100700static void Remove(HInstructionList* instruction_list,
701 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000702 HInstruction* instruction,
703 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100704 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100705 instruction->SetBlock(nullptr);
706 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000707 if (ensure_safety) {
708 DCHECK(instruction->GetUses().IsEmpty());
709 DCHECK(instruction->GetEnvUses().IsEmpty());
710 RemoveAsUser(instruction);
711 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100712}
713
David Brazdil1abb4192015-02-17 18:33:36 +0000714void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100715 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000716 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100717}
718
David Brazdil1abb4192015-02-17 18:33:36 +0000719void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
720 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100721}
722
David Brazdilc7508e92015-04-27 13:28:57 +0100723void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
724 if (instruction->IsPhi()) {
725 RemovePhi(instruction->AsPhi(), ensure_safety);
726 } else {
727 RemoveInstruction(instruction, ensure_safety);
728 }
729}
730
Vladimir Marko71bf8092015-09-15 15:33:14 +0100731void HEnvironment::CopyFrom(const ArenaVector<HInstruction*>& locals) {
732 for (size_t i = 0; i < locals.size(); i++) {
733 HInstruction* instruction = locals[i];
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +0100734 SetRawEnvAt(i, instruction);
735 if (instruction != nullptr) {
736 instruction->AddEnvUseAt(this, i);
737 }
738 }
739}
740
David Brazdiled596192015-01-23 10:39:45 +0000741void HEnvironment::CopyFrom(HEnvironment* env) {
742 for (size_t i = 0; i < env->Size(); i++) {
743 HInstruction* instruction = env->GetInstructionAt(i);
744 SetRawEnvAt(i, instruction);
745 if (instruction != nullptr) {
746 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100747 }
David Brazdiled596192015-01-23 10:39:45 +0000748 }
749}
750
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700751void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
752 HBasicBlock* loop_header) {
753 DCHECK(loop_header->IsLoopHeader());
754 for (size_t i = 0; i < env->Size(); i++) {
755 HInstruction* instruction = env->GetInstructionAt(i);
756 SetRawEnvAt(i, instruction);
757 if (instruction == nullptr) {
758 continue;
759 }
760 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
761 // At the end of the loop pre-header, the corresponding value for instruction
762 // is the first input of the phi.
763 HInstruction* initial = instruction->AsPhi()->InputAt(0);
764 DCHECK(initial->GetBlock()->Dominates(loop_header));
765 SetRawEnvAt(i, initial);
766 initial->AddEnvUseAt(this, i);
767 } else {
768 instruction->AddEnvUseAt(this, i);
769 }
770 }
771}
772
David Brazdil1abb4192015-02-17 18:33:36 +0000773void HEnvironment::RemoveAsUserOfInput(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100774 const HUserRecord<HEnvironment*>& user_record = vregs_[index];
David Brazdil1abb4192015-02-17 18:33:36 +0000775 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100776}
777
Calin Juravle77520bc2015-01-12 18:45:46 +0000778HInstruction* HInstruction::GetNextDisregardingMoves() const {
779 HInstruction* next = GetNext();
780 while (next != nullptr && next->IsParallelMove()) {
781 next = next->GetNext();
782 }
783 return next;
784}
785
786HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
787 HInstruction* previous = GetPrevious();
788 while (previous != nullptr && previous->IsParallelMove()) {
789 previous = previous->GetPrevious();
790 }
791 return previous;
792}
793
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100794void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000795 if (first_instruction_ == nullptr) {
796 DCHECK(last_instruction_ == nullptr);
797 first_instruction_ = last_instruction_ = instruction;
798 } else {
799 last_instruction_->next_ = instruction;
800 instruction->previous_ = last_instruction_;
801 last_instruction_ = instruction;
802 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803}
804
David Brazdilc3d743f2015-04-22 13:40:50 +0100805void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
806 DCHECK(Contains(cursor));
807 if (cursor == first_instruction_) {
808 cursor->previous_ = instruction;
809 instruction->next_ = cursor;
810 first_instruction_ = instruction;
811 } else {
812 instruction->previous_ = cursor->previous_;
813 instruction->next_ = cursor;
814 cursor->previous_ = instruction;
815 instruction->previous_->next_ = instruction;
816 }
817}
818
819void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
820 DCHECK(Contains(cursor));
821 if (cursor == last_instruction_) {
822 cursor->next_ = instruction;
823 instruction->previous_ = cursor;
824 last_instruction_ = instruction;
825 } else {
826 instruction->next_ = cursor->next_;
827 instruction->previous_ = cursor;
828 cursor->next_ = instruction;
829 instruction->next_->previous_ = instruction;
830 }
831}
832
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100833void HInstructionList::RemoveInstruction(HInstruction* instruction) {
834 if (instruction->previous_ != nullptr) {
835 instruction->previous_->next_ = instruction->next_;
836 }
837 if (instruction->next_ != nullptr) {
838 instruction->next_->previous_ = instruction->previous_;
839 }
840 if (instruction == first_instruction_) {
841 first_instruction_ = instruction->next_;
842 }
843 if (instruction == last_instruction_) {
844 last_instruction_ = instruction->previous_;
845 }
846}
847
Roland Levillain6b469232014-09-25 10:10:38 +0100848bool HInstructionList::Contains(HInstruction* instruction) const {
849 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
850 if (it.Current() == instruction) {
851 return true;
852 }
853 }
854 return false;
855}
856
Roland Levillainccc07a92014-09-16 14:48:16 +0100857bool HInstructionList::FoundBefore(const HInstruction* instruction1,
858 const HInstruction* instruction2) const {
859 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
860 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
861 if (it.Current() == instruction1) {
862 return true;
863 }
864 if (it.Current() == instruction2) {
865 return false;
866 }
867 }
868 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
869 return true;
870}
871
Roland Levillain6c82d402014-10-13 16:10:27 +0100872bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
873 if (other_instruction == this) {
874 // An instruction does not strictly dominate itself.
875 return false;
876 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100877 HBasicBlock* block = GetBlock();
878 HBasicBlock* other_block = other_instruction->GetBlock();
879 if (block != other_block) {
880 return GetBlock()->Dominates(other_instruction->GetBlock());
881 } else {
882 // If both instructions are in the same block, ensure this
883 // instruction comes before `other_instruction`.
884 if (IsPhi()) {
885 if (!other_instruction->IsPhi()) {
886 // Phis appear before non phi-instructions so this instruction
887 // dominates `other_instruction`.
888 return true;
889 } else {
890 // There is no order among phis.
891 LOG(FATAL) << "There is no dominance between phis of a same block.";
892 return false;
893 }
894 } else {
895 // `this` is not a phi.
896 if (other_instruction->IsPhi()) {
897 // Phis appear before non phi-instructions so this instruction
898 // does not dominate `other_instruction`.
899 return false;
900 } else {
901 // Check whether this instruction comes before
902 // `other_instruction` in the instruction list.
903 return block->GetInstructions().FoundBefore(this, other_instruction);
904 }
905 }
906 }
907}
908
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100909void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100910 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000911 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
912 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100913 HInstruction* user = current->GetUser();
914 size_t input_index = current->GetIndex();
915 user->SetRawInputAt(input_index, other);
916 other->AddUseAt(user, input_index);
917 }
918
David Brazdiled596192015-01-23 10:39:45 +0000919 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
920 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100921 HEnvironment* user = current->GetUser();
922 size_t input_index = current->GetIndex();
923 user->SetRawEnvAt(input_index, other);
924 other->AddEnvUseAt(user, input_index);
925 }
926
David Brazdiled596192015-01-23 10:39:45 +0000927 uses_.Clear();
928 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100929}
930
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100931void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000932 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100933 SetRawInputAt(index, replacement);
934 replacement->AddUseAt(this, index);
935}
936
Nicolas Geoffray39468442014-09-02 15:17:15 +0100937size_t HInstruction::EnvironmentSize() const {
938 return HasEnvironment() ? environment_->Size() : 0;
939}
940
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100941void HPhi::AddInput(HInstruction* input) {
942 DCHECK(input->GetBlock() != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100943 inputs_.push_back(HUserRecord<HInstruction*>(input));
944 input->AddUseAt(this, inputs_.size() - 1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100945}
946
David Brazdil2d7352b2015-04-20 14:52:42 +0100947void HPhi::RemoveInputAt(size_t index) {
948 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100949 inputs_.erase(inputs_.begin() + index);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100950 for (size_t i = index, e = InputCount(); i < e; ++i) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100951 DCHECK_EQ(InputRecordAt(i).GetUseNode()->GetIndex(), i + 1u);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100952 InputRecordAt(i).GetUseNode()->SetIndex(i);
953 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100954}
955
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100956#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000957void H##name::Accept(HGraphVisitor* visitor) { \
958 visitor->Visit##name(this); \
959}
960
961FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
962
963#undef DEFINE_ACCEPT
964
965void HGraphVisitor::VisitInsertionOrder() {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100966 const ArenaVector<HBasicBlock*>& blocks = graph_->GetBlocks();
967 for (HBasicBlock* block : blocks) {
David Brazdil46e2a392015-03-16 17:31:52 +0000968 if (block != nullptr) {
969 VisitBasicBlock(block);
970 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000971 }
972}
973
Roland Levillain633021e2014-10-01 14:12:25 +0100974void HGraphVisitor::VisitReversePostOrder() {
975 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
976 VisitBasicBlock(it.Current());
977 }
978}
979
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100981 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100982 it.Current()->Accept(this);
983 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100984 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000985 it.Current()->Accept(this);
986 }
987}
988
Mark Mendelle82549b2015-05-06 10:55:34 -0400989HConstant* HTypeConversion::TryStaticEvaluation() const {
990 HGraph* graph = GetBlock()->GetGraph();
991 if (GetInput()->IsIntConstant()) {
992 int32_t value = GetInput()->AsIntConstant()->GetValue();
993 switch (GetResultType()) {
994 case Primitive::kPrimLong:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600995 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -0400996 case Primitive::kPrimFloat:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600997 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -0400998 case Primitive::kPrimDouble:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600999 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001000 default:
1001 return nullptr;
1002 }
1003 } else if (GetInput()->IsLongConstant()) {
1004 int64_t value = GetInput()->AsLongConstant()->GetValue();
1005 switch (GetResultType()) {
1006 case Primitive::kPrimInt:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001007 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001008 case Primitive::kPrimFloat:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001009 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001010 case Primitive::kPrimDouble:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001011 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001012 default:
1013 return nullptr;
1014 }
1015 } else if (GetInput()->IsFloatConstant()) {
1016 float value = GetInput()->AsFloatConstant()->GetValue();
1017 switch (GetResultType()) {
1018 case Primitive::kPrimInt:
1019 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001020 return graph->GetIntConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001021 if (value >= kPrimIntMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001022 return graph->GetIntConstant(kPrimIntMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001023 if (value <= kPrimIntMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001024 return graph->GetIntConstant(kPrimIntMin, GetDexPc());
1025 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001026 case Primitive::kPrimLong:
1027 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001028 return graph->GetLongConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001029 if (value >= kPrimLongMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001030 return graph->GetLongConstant(kPrimLongMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001031 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001032 return graph->GetLongConstant(kPrimLongMin, GetDexPc());
1033 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001034 case Primitive::kPrimDouble:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001035 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001036 default:
1037 return nullptr;
1038 }
1039 } else if (GetInput()->IsDoubleConstant()) {
1040 double value = GetInput()->AsDoubleConstant()->GetValue();
1041 switch (GetResultType()) {
1042 case Primitive::kPrimInt:
1043 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001044 return graph->GetIntConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001045 if (value >= kPrimIntMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001046 return graph->GetIntConstant(kPrimIntMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001047 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001048 return graph->GetIntConstant(kPrimIntMin, GetDexPc());
1049 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001050 case Primitive::kPrimLong:
1051 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001052 return graph->GetLongConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001053 if (value >= kPrimLongMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001054 return graph->GetLongConstant(kPrimLongMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001055 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001056 return graph->GetLongConstant(kPrimLongMin, GetDexPc());
1057 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001058 case Primitive::kPrimFloat:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001059 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001060 default:
1061 return nullptr;
1062 }
1063 }
1064 return nullptr;
1065}
1066
Roland Levillain9240d6a2014-10-20 16:47:04 +01001067HConstant* HUnaryOperation::TryStaticEvaluation() const {
1068 if (GetInput()->IsIntConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001069 return Evaluate(GetInput()->AsIntConstant());
Roland Levillain9240d6a2014-10-20 16:47:04 +01001070 } else if (GetInput()->IsLongConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001071 return Evaluate(GetInput()->AsLongConstant());
Roland Levillain9240d6a2014-10-20 16:47:04 +01001072 }
1073 return nullptr;
1074}
1075
1076HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain9867bc72015-08-05 10:21:34 +01001077 if (GetLeft()->IsIntConstant()) {
1078 if (GetRight()->IsIntConstant()) {
1079 return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsIntConstant());
1080 } else if (GetRight()->IsLongConstant()) {
1081 return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsLongConstant());
1082 }
1083 } else if (GetLeft()->IsLongConstant()) {
1084 if (GetRight()->IsIntConstant()) {
1085 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsIntConstant());
1086 } else if (GetRight()->IsLongConstant()) {
1087 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsLongConstant());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +00001088 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001089 }
1090 return nullptr;
1091}
Dave Allison20dfc792014-06-16 20:44:29 -07001092
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001093HConstant* HBinaryOperation::GetConstantRight() const {
1094 if (GetRight()->IsConstant()) {
1095 return GetRight()->AsConstant();
1096 } else if (IsCommutative() && GetLeft()->IsConstant()) {
1097 return GetLeft()->AsConstant();
1098 } else {
1099 return nullptr;
1100 }
1101}
1102
1103// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001104// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001105HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
1106 HInstruction* most_constant_right = GetConstantRight();
1107 if (most_constant_right == nullptr) {
1108 return nullptr;
1109 } else if (most_constant_right == GetLeft()) {
1110 return GetRight();
1111 } else {
1112 return GetLeft();
1113 }
1114}
1115
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001116bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
1117 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001118}
1119
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001120bool HInstruction::Equals(HInstruction* other) const {
1121 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001122 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001123 if (!InstructionDataEquals(other)) return false;
1124 if (GetType() != other->GetType()) return false;
1125 if (InputCount() != other->InputCount()) return false;
1126
1127 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1128 if (InputAt(i) != other->InputAt(i)) return false;
1129 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001130 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001131 return true;
1132}
1133
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001134std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
1135#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
1136 switch (rhs) {
1137 FOR_EACH_INSTRUCTION(DECLARE_CASE)
1138 default:
1139 os << "Unknown instruction kind " << static_cast<int>(rhs);
1140 break;
1141 }
1142#undef DECLARE_CASE
1143 return os;
1144}
1145
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001146void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001147 next_->previous_ = previous_;
1148 if (previous_ != nullptr) {
1149 previous_->next_ = next_;
1150 }
1151 if (block_->instructions_.first_instruction_ == this) {
1152 block_->instructions_.first_instruction_ = next_;
1153 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001154 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001155
1156 previous_ = cursor->previous_;
1157 if (previous_ != nullptr) {
1158 previous_->next_ = this;
1159 }
1160 next_ = cursor;
1161 cursor->previous_ = this;
1162 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001163
1164 if (block_->instructions_.first_instruction_ == cursor) {
1165 block_->instructions_.first_instruction_ = this;
1166 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001167}
1168
David Brazdilfc6a86a2015-06-26 10:33:45 +00001169HBasicBlock* HBasicBlock::SplitBefore(HInstruction* cursor) {
1170 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented";
1171 DCHECK_EQ(cursor->GetBlock(), this);
1172
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001173 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(),
1174 cursor->GetDexPc());
David Brazdilfc6a86a2015-06-26 10:33:45 +00001175 new_block->instructions_.first_instruction_ = cursor;
1176 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1177 instructions_.last_instruction_ = cursor->previous_;
1178 if (cursor->previous_ == nullptr) {
1179 instructions_.first_instruction_ = nullptr;
1180 } else {
1181 cursor->previous_->next_ = nullptr;
1182 cursor->previous_ = nullptr;
1183 }
1184
1185 new_block->instructions_.SetBlockOfInstructions(new_block);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001186 AddInstruction(new (GetGraph()->GetArena()) HGoto(new_block->GetDexPc()));
David Brazdilfc6a86a2015-06-26 10:33:45 +00001187
Vladimir Marko60584552015-09-03 13:35:12 +00001188 for (HBasicBlock* successor : GetSuccessors()) {
1189 new_block->successors_.push_back(successor);
1190 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001191 }
Vladimir Marko60584552015-09-03 13:35:12 +00001192 successors_.clear();
David Brazdilfc6a86a2015-06-26 10:33:45 +00001193 AddSuccessor(new_block);
1194
David Brazdil56e1acc2015-06-30 15:41:36 +01001195 GetGraph()->AddBlock(new_block);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001196 return new_block;
1197}
1198
David Brazdild7558da2015-09-22 13:04:14 +01001199HBasicBlock* HBasicBlock::CreateImmediateDominator() {
1200 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented";
1201 DCHECK(!IsCatchBlock()) << "Support for updating try/catch information not implemented.";
1202
1203 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
1204
1205 for (HBasicBlock* predecessor : GetPredecessors()) {
1206 new_block->predecessors_.push_back(predecessor);
1207 predecessor->successors_[predecessor->GetSuccessorIndexOf(this)] = new_block;
1208 }
1209 predecessors_.clear();
1210 AddPredecessor(new_block);
1211
1212 GetGraph()->AddBlock(new_block);
1213 return new_block;
1214}
1215
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001216HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
1217 DCHECK(!cursor->IsControlFlow());
1218 DCHECK_NE(instructions_.last_instruction_, cursor);
1219 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001220
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001221 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
1222 new_block->instructions_.first_instruction_ = cursor->GetNext();
1223 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1224 cursor->next_->previous_ = nullptr;
1225 cursor->next_ = nullptr;
1226 instructions_.last_instruction_ = cursor;
1227
1228 new_block->instructions_.SetBlockOfInstructions(new_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001229 for (HBasicBlock* successor : GetSuccessors()) {
1230 new_block->successors_.push_back(successor);
1231 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001232 }
Vladimir Marko60584552015-09-03 13:35:12 +00001233 successors_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001234
Vladimir Marko60584552015-09-03 13:35:12 +00001235 for (HBasicBlock* dominated : GetDominatedBlocks()) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001236 dominated->dominator_ = new_block;
Vladimir Marko60584552015-09-03 13:35:12 +00001237 new_block->dominated_blocks_.push_back(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001238 }
Vladimir Marko60584552015-09-03 13:35:12 +00001239 dominated_blocks_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001240 return new_block;
1241}
1242
David Brazdilec16f792015-08-19 15:04:01 +01001243const HTryBoundary* HBasicBlock::ComputeTryEntryOfSuccessors() const {
David Brazdilffee3d32015-07-06 11:48:53 +01001244 if (EndsWithTryBoundary()) {
1245 HTryBoundary* try_boundary = GetLastInstruction()->AsTryBoundary();
1246 if (try_boundary->IsEntry()) {
David Brazdilec16f792015-08-19 15:04:01 +01001247 DCHECK(!IsTryBlock());
David Brazdilffee3d32015-07-06 11:48:53 +01001248 return try_boundary;
1249 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001250 DCHECK(IsTryBlock());
1251 DCHECK(try_catch_information_->GetTryEntry().HasSameExceptionHandlersAs(*try_boundary));
David Brazdilffee3d32015-07-06 11:48:53 +01001252 return nullptr;
1253 }
David Brazdilec16f792015-08-19 15:04:01 +01001254 } else if (IsTryBlock()) {
1255 return &try_catch_information_->GetTryEntry();
David Brazdilffee3d32015-07-06 11:48:53 +01001256 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001257 return nullptr;
David Brazdilffee3d32015-07-06 11:48:53 +01001258 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00001259}
1260
David Brazdild7558da2015-09-22 13:04:14 +01001261bool HBasicBlock::HasThrowingInstructions() const {
1262 for (HInstructionIterator it(GetInstructions()); !it.Done(); it.Advance()) {
1263 if (it.Current()->CanThrow()) {
1264 return true;
1265 }
1266 }
1267 return false;
1268}
1269
David Brazdilfc6a86a2015-06-26 10:33:45 +00001270static bool HasOnlyOneInstruction(const HBasicBlock& block) {
1271 return block.GetPhis().IsEmpty()
1272 && !block.GetInstructions().IsEmpty()
1273 && block.GetFirstInstruction() == block.GetLastInstruction();
1274}
1275
David Brazdil46e2a392015-03-16 17:31:52 +00001276bool HBasicBlock::IsSingleGoto() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001277 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsGoto();
1278}
1279
1280bool HBasicBlock::IsSingleTryBoundary() const {
1281 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsTryBoundary();
David Brazdil46e2a392015-03-16 17:31:52 +00001282}
1283
David Brazdil8d5b8b22015-03-24 10:51:52 +00001284bool HBasicBlock::EndsWithControlFlowInstruction() const {
1285 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
1286}
1287
David Brazdilb2bd1c52015-03-25 11:17:37 +00001288bool HBasicBlock::EndsWithIf() const {
1289 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
1290}
1291
David Brazdilffee3d32015-07-06 11:48:53 +01001292bool HBasicBlock::EndsWithTryBoundary() const {
1293 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsTryBoundary();
1294}
1295
David Brazdilb2bd1c52015-03-25 11:17:37 +00001296bool HBasicBlock::HasSinglePhi() const {
1297 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
1298}
1299
David Brazdilffee3d32015-07-06 11:48:53 +01001300bool HTryBoundary::HasSameExceptionHandlersAs(const HTryBoundary& other) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001301 if (GetBlock()->GetSuccessors().size() != other.GetBlock()->GetSuccessors().size()) {
David Brazdilffee3d32015-07-06 11:48:53 +01001302 return false;
1303 }
1304
David Brazdilb618ade2015-07-29 10:31:29 +01001305 // Exception handlers need to be stored in the same order.
1306 for (HExceptionHandlerIterator it1(*this), it2(other);
1307 !it1.Done();
1308 it1.Advance(), it2.Advance()) {
1309 DCHECK(!it2.Done());
1310 if (it1.Current() != it2.Current()) {
David Brazdilffee3d32015-07-06 11:48:53 +01001311 return false;
1312 }
1313 }
1314 return true;
1315}
1316
David Brazdil2d7352b2015-04-20 14:52:42 +01001317size_t HInstructionList::CountSize() const {
1318 size_t size = 0;
1319 HInstruction* current = first_instruction_;
1320 for (; current != nullptr; current = current->GetNext()) {
1321 size++;
1322 }
1323 return size;
1324}
1325
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001326void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
1327 for (HInstruction* current = first_instruction_;
1328 current != nullptr;
1329 current = current->GetNext()) {
1330 current->SetBlock(block);
1331 }
1332}
1333
1334void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
1335 DCHECK(Contains(cursor));
1336 if (!instruction_list.IsEmpty()) {
1337 if (cursor == last_instruction_) {
1338 last_instruction_ = instruction_list.last_instruction_;
1339 } else {
1340 cursor->next_->previous_ = instruction_list.last_instruction_;
1341 }
1342 instruction_list.last_instruction_->next_ = cursor->next_;
1343 cursor->next_ = instruction_list.first_instruction_;
1344 instruction_list.first_instruction_->previous_ = cursor;
1345 }
1346}
1347
1348void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +00001349 if (IsEmpty()) {
1350 first_instruction_ = instruction_list.first_instruction_;
1351 last_instruction_ = instruction_list.last_instruction_;
1352 } else {
1353 AddAfter(last_instruction_, instruction_list);
1354 }
1355}
1356
David Brazdil2d7352b2015-04-20 14:52:42 +01001357void HBasicBlock::DisconnectAndDelete() {
1358 // Dominators must be removed after all the blocks they dominate. This way
1359 // a loop header is removed last, a requirement for correct loop information
1360 // iteration.
Vladimir Marko60584552015-09-03 13:35:12 +00001361 DCHECK(dominated_blocks_.empty());
David Brazdil46e2a392015-03-16 17:31:52 +00001362
David Brazdil2d7352b2015-04-20 14:52:42 +01001363 // Remove the block from all loops it is included in.
1364 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
1365 HLoopInformation* loop_info = it.Current();
1366 loop_info->Remove(this);
1367 if (loop_info->IsBackEdge(*this)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001368 // If this was the last back edge of the loop, we deliberately leave the
1369 // loop in an inconsistent state and will fail SSAChecker unless the
1370 // entire loop is removed during the pass.
David Brazdil2d7352b2015-04-20 14:52:42 +01001371 loop_info->RemoveBackEdge(this);
1372 }
1373 }
1374
1375 // Disconnect the block from its predecessors and update their control-flow
1376 // instructions.
Vladimir Marko60584552015-09-03 13:35:12 +00001377 for (HBasicBlock* predecessor : predecessors_) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001378 HInstruction* last_instruction = predecessor->GetLastInstruction();
David Brazdilce529012015-10-28 19:25:55 -05001379 if (last_instruction->IsTryBoundary() && !IsCatchBlock()) {
1380 // This block is the only normal-flow successor of the TryBoundary which
1381 // makes `predecessor` dead. Since DCE removes blocks in post order,
1382 // exception handlers of this TryBoundary were already visited and any
1383 // remaining handlers therefore must be live. We remove `predecessor` from
1384 // their list of predecessors.
1385 DCHECK_EQ(last_instruction->AsTryBoundary()->GetNormalFlowSuccessor(), this);
1386 while (predecessor->GetSuccessors().size() > 1) {
1387 HBasicBlock* handler = predecessor->GetSuccessors()[1];
1388 DCHECK(handler->IsCatchBlock());
1389 predecessor->RemoveSuccessor(handler);
1390 handler->RemovePredecessor(predecessor);
1391 }
1392 }
1393
David Brazdil2d7352b2015-04-20 14:52:42 +01001394 predecessor->RemoveSuccessor(this);
Mark Mendellfe57faa2015-09-18 09:26:15 -04001395 uint32_t num_pred_successors = predecessor->GetSuccessors().size();
1396 if (num_pred_successors == 1u) {
1397 // If we have one successor after removing one, then we must have
David Brazdilce529012015-10-28 19:25:55 -05001398 // had an HIf, HPackedSwitch or HTryBoundary, as they have more than one
1399 // successor. Replace those with a HGoto.
1400 DCHECK(last_instruction->IsIf() ||
1401 last_instruction->IsPackedSwitch() ||
1402 (last_instruction->IsTryBoundary() && IsCatchBlock()));
Mark Mendellfe57faa2015-09-18 09:26:15 -04001403 predecessor->RemoveInstruction(last_instruction);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001404 predecessor->AddInstruction(new (graph_->GetArena()) HGoto(last_instruction->GetDexPc()));
Mark Mendellfe57faa2015-09-18 09:26:15 -04001405 } else if (num_pred_successors == 0u) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001406 // The predecessor has no remaining successors and therefore must be dead.
1407 // We deliberately leave it without a control-flow instruction so that the
1408 // SSAChecker fails unless it is not removed during the pass too.
Mark Mendellfe57faa2015-09-18 09:26:15 -04001409 predecessor->RemoveInstruction(last_instruction);
1410 } else {
David Brazdilce529012015-10-28 19:25:55 -05001411 // There are multiple successors left. The removed block might be a successor
1412 // of a PackedSwitch which will be completely removed (perhaps replaced with
1413 // a Goto), or we are deleting a catch block from a TryBoundary. In either
1414 // case, leave `last_instruction` as is for now.
1415 DCHECK(last_instruction->IsPackedSwitch() ||
1416 (last_instruction->IsTryBoundary() && IsCatchBlock()));
David Brazdil2d7352b2015-04-20 14:52:42 +01001417 }
David Brazdil46e2a392015-03-16 17:31:52 +00001418 }
Vladimir Marko60584552015-09-03 13:35:12 +00001419 predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001420
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001421 // Disconnect the block from its successors and update their phis.
Vladimir Marko60584552015-09-03 13:35:12 +00001422 for (HBasicBlock* successor : successors_) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001423 // Delete this block from the list of predecessors.
1424 size_t this_index = successor->GetPredecessorIndexOf(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001425 successor->predecessors_.erase(successor->predecessors_.begin() + this_index);
David Brazdil2d7352b2015-04-20 14:52:42 +01001426
1427 // Check that `successor` has other predecessors, otherwise `this` is the
1428 // dominator of `successor` which violates the order DCHECKed at the top.
Vladimir Marko60584552015-09-03 13:35:12 +00001429 DCHECK(!successor->predecessors_.empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001430
David Brazdil2d7352b2015-04-20 14:52:42 +01001431 // Remove this block's entries in the successor's phis.
Vladimir Marko60584552015-09-03 13:35:12 +00001432 if (successor->predecessors_.size() == 1u) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001433 // The successor has just one predecessor left. Replace phis with the only
1434 // remaining input.
1435 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1436 HPhi* phi = phi_it.Current()->AsPhi();
1437 phi->ReplaceWith(phi->InputAt(1 - this_index));
1438 successor->RemovePhi(phi);
1439 }
1440 } else {
1441 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1442 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1443 }
1444 }
1445 }
Vladimir Marko60584552015-09-03 13:35:12 +00001446 successors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001447
1448 // Disconnect from the dominator.
1449 dominator_->RemoveDominatedBlock(this);
1450 SetDominator(nullptr);
1451
1452 // Delete from the graph. The function safely deletes remaining instructions
1453 // and updates the reverse post order.
1454 graph_->DeleteDeadBlock(this);
1455 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001456}
1457
1458void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001459 DCHECK_EQ(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00001460 DCHECK(ContainsElement(dominated_blocks_, other));
1461 DCHECK_EQ(GetSingleSuccessor(), other);
1462 DCHECK_EQ(other->GetSinglePredecessor(), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001463 DCHECK(other->GetPhis().IsEmpty());
1464
David Brazdil2d7352b2015-04-20 14:52:42 +01001465 // Move instructions from `other` to `this`.
1466 DCHECK(EndsWithControlFlowInstruction());
1467 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001468 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001469 other->instructions_.SetBlockOfInstructions(this);
1470 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001471
David Brazdil2d7352b2015-04-20 14:52:42 +01001472 // Remove `other` from the loops it is included in.
1473 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1474 HLoopInformation* loop_info = it.Current();
1475 loop_info->Remove(other);
1476 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001477 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01001478 }
1479 }
1480
1481 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00001482 successors_.clear();
1483 while (!other->successors_.empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001484 HBasicBlock* successor = other->GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001485 successor->ReplacePredecessor(other, this);
1486 }
1487
David Brazdil2d7352b2015-04-20 14:52:42 +01001488 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00001489 RemoveDominatedBlock(other);
1490 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
1491 dominated_blocks_.push_back(dominated);
David Brazdil2d7352b2015-04-20 14:52:42 +01001492 dominated->SetDominator(this);
1493 }
Vladimir Marko60584552015-09-03 13:35:12 +00001494 other->dominated_blocks_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001495 other->dominator_ = nullptr;
1496
1497 // Clear the list of predecessors of `other` in preparation of deleting it.
Vladimir Marko60584552015-09-03 13:35:12 +00001498 other->predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01001499
1500 // Delete `other` from the graph. The function updates reverse post order.
1501 graph_->DeleteDeadBlock(other);
1502 other->SetGraph(nullptr);
1503}
1504
1505void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1506 DCHECK_NE(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00001507 DCHECK(GetDominatedBlocks().empty());
1508 DCHECK(GetSuccessors().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001509 DCHECK(!EndsWithControlFlowInstruction());
Vladimir Marko60584552015-09-03 13:35:12 +00001510 DCHECK(other->GetSinglePredecessor()->IsEntryBlock());
David Brazdil2d7352b2015-04-20 14:52:42 +01001511 DCHECK(other->GetPhis().IsEmpty());
1512 DCHECK(!other->IsInLoop());
1513
1514 // Move instructions from `other` to `this`.
1515 instructions_.Add(other->GetInstructions());
1516 other->instructions_.SetBlockOfInstructions(this);
1517
1518 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00001519 successors_.clear();
1520 while (!other->successors_.empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001521 HBasicBlock* successor = other->GetSuccessors()[0];
David Brazdil2d7352b2015-04-20 14:52:42 +01001522 successor->ReplacePredecessor(other, this);
1523 }
1524
1525 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00001526 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
1527 dominated_blocks_.push_back(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001528 dominated->SetDominator(this);
1529 }
Vladimir Marko60584552015-09-03 13:35:12 +00001530 other->dominated_blocks_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001531 other->dominator_ = nullptr;
1532 other->graph_ = nullptr;
1533}
1534
1535void HBasicBlock::ReplaceWith(HBasicBlock* other) {
Vladimir Marko60584552015-09-03 13:35:12 +00001536 while (!GetPredecessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001537 HBasicBlock* predecessor = GetPredecessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001538 predecessor->ReplaceSuccessor(this, other);
1539 }
Vladimir Marko60584552015-09-03 13:35:12 +00001540 while (!GetSuccessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001541 HBasicBlock* successor = GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001542 successor->ReplacePredecessor(this, other);
1543 }
Vladimir Marko60584552015-09-03 13:35:12 +00001544 for (HBasicBlock* dominated : GetDominatedBlocks()) {
1545 other->AddDominatedBlock(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001546 }
1547 GetDominator()->ReplaceDominatedBlock(this, other);
1548 other->SetDominator(GetDominator());
1549 dominator_ = nullptr;
1550 graph_ = nullptr;
1551}
1552
1553// Create space in `blocks` for adding `number_of_new_blocks` entries
1554// starting at location `at`. Blocks after `at` are moved accordingly.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001555static void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001556 size_t number_of_new_blocks,
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001557 size_t after) {
1558 DCHECK_LT(after, blocks->size());
1559 size_t old_size = blocks->size();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001560 size_t new_size = old_size + number_of_new_blocks;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001561 blocks->resize(new_size);
1562 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001563}
1564
David Brazdil2d7352b2015-04-20 14:52:42 +01001565void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1566 DCHECK_EQ(block->GetGraph(), this);
Vladimir Marko60584552015-09-03 13:35:12 +00001567 DCHECK(block->GetSuccessors().empty());
1568 DCHECK(block->GetPredecessors().empty());
1569 DCHECK(block->GetDominatedBlocks().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01001570 DCHECK(block->GetDominator() == nullptr);
1571
1572 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1573 block->RemoveInstruction(it.Current());
1574 }
1575 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1576 block->RemovePhi(it.Current()->AsPhi());
1577 }
1578
David Brazdilc7af85d2015-05-26 12:05:55 +01001579 if (block->IsExitBlock()) {
1580 exit_block_ = nullptr;
1581 }
1582
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001583 RemoveElement(reverse_post_order_, block);
1584 blocks_[block->GetBlockId()] = nullptr;
David Brazdil2d7352b2015-04-20 14:52:42 +01001585}
1586
Calin Juravle2e768302015-07-28 14:41:11 +00001587HInstruction* HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
David Brazdilc7af85d2015-05-26 12:05:55 +01001588 DCHECK(HasExitBlock()) << "Unimplemented scenario";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001589 // Update the environments in this graph to have the invoke's environment
1590 // as parent.
1591 {
1592 HReversePostOrderIterator it(*this);
1593 it.Advance(); // Skip the entry block, we do not need to update the entry's suspend check.
1594 for (; !it.Done(); it.Advance()) {
1595 HBasicBlock* block = it.Current();
1596 for (HInstructionIterator instr_it(block->GetInstructions());
1597 !instr_it.Done();
1598 instr_it.Advance()) {
1599 HInstruction* current = instr_it.Current();
1600 if (current->NeedsEnvironment()) {
1601 current->GetEnvironment()->SetAndCopyParentChain(
1602 outer_graph->GetArena(), invoke->GetEnvironment());
1603 }
1604 }
1605 }
1606 }
1607 outer_graph->UpdateMaximumNumberOfOutVRegs(GetMaximumNumberOfOutVRegs());
1608 if (HasBoundsChecks()) {
1609 outer_graph->SetHasBoundsChecks(true);
1610 }
1611
Calin Juravle2e768302015-07-28 14:41:11 +00001612 HInstruction* return_value = nullptr;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001613 if (GetBlocks().size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001614 // Simple case of an entry block, a body block, and an exit block.
1615 // Put the body block's instruction into `invoke`'s block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01001616 HBasicBlock* body = GetBlocks()[1];
1617 DCHECK(GetBlocks()[0]->IsEntryBlock());
1618 DCHECK(GetBlocks()[2]->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001619 DCHECK(!body->IsExitBlock());
1620 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001621
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001622 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1623 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001624
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001625 // Replace the invoke with the return value of the inlined graph.
1626 if (last->IsReturn()) {
Calin Juravle2e768302015-07-28 14:41:11 +00001627 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001628 } else {
1629 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001630 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001631
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001632 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001633 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001634 // Need to inline multiple blocks. We split `invoke`'s block
1635 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001636 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001637 // with the second half.
1638 ArenaAllocator* allocator = outer_graph->GetArena();
1639 HBasicBlock* at = invoke->GetBlock();
1640 HBasicBlock* to = at->SplitAfter(invoke);
1641
Vladimir Markoec7802a2015-10-01 20:57:57 +01001642 HBasicBlock* first = entry_block_->GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001643 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001644 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001645 exit_block_->ReplaceWith(to);
1646
1647 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001648 // to not `HReturn` but `HGoto` instead.
Vladimir Markoec7802a2015-10-01 20:57:57 +01001649 bool returns_void = to->GetPredecessors()[0]->GetLastInstruction()->IsReturnVoid();
Vladimir Marko60584552015-09-03 13:35:12 +00001650 if (to->GetPredecessors().size() == 1) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001651 HBasicBlock* predecessor = to->GetPredecessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001652 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001653 if (!returns_void) {
1654 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001655 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001656 predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc()));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001657 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001658 } else {
1659 if (!returns_void) {
1660 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001661 return_value = new (allocator) HPhi(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001662 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()), to->GetDexPc());
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001663 to->AddPhi(return_value->AsPhi());
1664 }
Vladimir Marko60584552015-09-03 13:35:12 +00001665 for (HBasicBlock* predecessor : to->GetPredecessors()) {
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001666 HInstruction* last = predecessor->GetLastInstruction();
1667 if (!returns_void) {
1668 return_value->AsPhi()->AddInput(last->InputAt(0));
1669 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001670 predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001671 predecessor->RemoveInstruction(last);
1672 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001673 }
1674
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001675 // Update the meta information surrounding blocks:
1676 // (1) the graph they are now in,
1677 // (2) the reverse post order of that graph,
David Brazdil95177982015-10-30 12:56:58 -05001678 // (3) the potential loop information they are now in,
1679 // (4) try block membership.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001680
1681 // We don't add the entry block, the exit block, and the first block, which
1682 // has been merged with `at`.
1683 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1684
1685 // We add the `to` block.
1686 static constexpr int kNumberOfNewBlocksInCaller = 1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001687 size_t blocks_added = (reverse_post_order_.size() - kNumberOfSkippedBlocksInCallee)
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001688 + kNumberOfNewBlocksInCaller;
1689
1690 // Find the location of `at` in the outer graph's reverse post order. The new
1691 // blocks will be added after it.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001692 size_t index_of_at = IndexOfElement(outer_graph->reverse_post_order_, at);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001693 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1694
David Brazdil95177982015-10-30 12:56:58 -05001695 HLoopInformation* loop_info = at->GetLoopInformation();
1696 // Copy TryCatchInformation if `at` is a try block, not if it is a catch block.
1697 TryCatchInformation* try_catch_info = at->IsTryBlock() ? at->GetTryCatchInformation() : nullptr;
1698
1699 // Do a reverse post order of the blocks in the callee and do (1), (2), (3)
1700 // and (4) to the blocks that apply.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001701 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1702 HBasicBlock* current = it.Current();
1703 if (current != exit_block_ && current != entry_block_ && current != first) {
1704 DCHECK(!current->IsInLoop());
David Brazdil95177982015-10-30 12:56:58 -05001705 DCHECK(current->GetTryCatchInformation() == nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001706 DCHECK(current->GetGraph() == this);
1707 current->SetGraph(outer_graph);
1708 outer_graph->AddBlock(current);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001709 outer_graph->reverse_post_order_[++index_of_at] = current;
David Brazdil95177982015-10-30 12:56:58 -05001710 if (loop_info != nullptr) {
1711 current->SetLoopInformation(loop_info);
David Brazdil7d275372015-04-21 16:36:35 +01001712 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1713 loop_it.Current()->Add(current);
1714 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001715 }
David Brazdil95177982015-10-30 12:56:58 -05001716 current->SetTryCatchInformation(try_catch_info);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001717 }
1718 }
1719
David Brazdil95177982015-10-30 12:56:58 -05001720 // Do (1), (2), (3) and (4) to `to`.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001721 to->SetGraph(outer_graph);
1722 outer_graph->AddBlock(to);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001723 outer_graph->reverse_post_order_[++index_of_at] = to;
David Brazdil95177982015-10-30 12:56:58 -05001724 if (loop_info != nullptr) {
1725 to->SetLoopInformation(loop_info);
David Brazdil7d275372015-04-21 16:36:35 +01001726 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1727 loop_it.Current()->Add(to);
1728 }
David Brazdil95177982015-10-30 12:56:58 -05001729 if (loop_info->IsBackEdge(*at)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001730 // Only `to` can become a back edge, as the inlined blocks
1731 // are predecessors of `to`.
David Brazdil95177982015-10-30 12:56:58 -05001732 loop_info->ReplaceBackEdge(at, to);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001733 }
1734 }
David Brazdil95177982015-10-30 12:56:58 -05001735 to->SetTryCatchInformation(try_catch_info);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001736 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001737
David Brazdil05144f42015-04-16 15:18:00 +01001738 // Update the next instruction id of the outer graph, so that instructions
1739 // added later get bigger ids than those in the inner graph.
1740 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1741
1742 // Walk over the entry block and:
1743 // - Move constants from the entry block to the outer_graph's entry block,
1744 // - Replace HParameterValue instructions with their real value.
1745 // - Remove suspend checks, that hold an environment.
1746 // We must do this after the other blocks have been inlined, otherwise ids of
1747 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001748 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001749 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1750 HInstruction* current = it.Current();
Calin Juravle214bbcd2015-10-20 14:54:07 +01001751 HInstruction* replacement = nullptr;
David Brazdil05144f42015-04-16 15:18:00 +01001752 if (current->IsNullConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001753 replacement = outer_graph->GetNullConstant(current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001754 } else if (current->IsIntConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001755 replacement = outer_graph->GetIntConstant(
1756 current->AsIntConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001757 } else if (current->IsLongConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001758 replacement = outer_graph->GetLongConstant(
1759 current->AsLongConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001760 } else if (current->IsFloatConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001761 replacement = outer_graph->GetFloatConstant(
1762 current->AsFloatConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001763 } else if (current->IsDoubleConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001764 replacement = outer_graph->GetDoubleConstant(
1765 current->AsDoubleConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01001766 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001767 if (kIsDebugBuild
1768 && invoke->IsInvokeStaticOrDirect()
1769 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1770 // Ensure we do not use the last input of `invoke`, as it
1771 // contains a clinit check which is not an actual argument.
1772 size_t last_input_index = invoke->InputCount() - 1;
1773 DCHECK(parameter_index != last_input_index);
1774 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01001775 replacement = invoke->InputAt(parameter_index++);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001776 } else if (current->IsCurrentMethod()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01001777 replacement = outer_graph->GetCurrentMethod();
David Brazdil05144f42015-04-16 15:18:00 +01001778 } else {
1779 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1780 entry_block_->RemoveInstruction(current);
1781 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01001782 if (replacement != nullptr) {
1783 current->ReplaceWith(replacement);
1784 // If the current is the return value then we need to update the latter.
1785 if (current == return_value) {
1786 DCHECK_EQ(entry_block_, return_value->GetBlock());
1787 return_value = replacement;
1788 }
1789 }
1790 }
1791
1792 if (return_value != nullptr) {
1793 invoke->ReplaceWith(return_value);
David Brazdil05144f42015-04-16 15:18:00 +01001794 }
1795
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001796 // Finally remove the invoke from the caller.
1797 invoke->GetBlock()->RemoveInstruction(invoke);
Calin Juravle2e768302015-07-28 14:41:11 +00001798
1799 return return_value;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001800}
1801
Mingyao Yang3584bce2015-05-19 16:01:59 -07001802/*
1803 * Loop will be transformed to:
1804 * old_pre_header
1805 * |
1806 * if_block
1807 * / \
1808 * dummy_block deopt_block
1809 * \ /
1810 * new_pre_header
1811 * |
1812 * header
1813 */
1814void HGraph::TransformLoopHeaderForBCE(HBasicBlock* header) {
1815 DCHECK(header->IsLoopHeader());
1816 HBasicBlock* pre_header = header->GetDominator();
1817
1818 // Need this to avoid critical edge.
1819 HBasicBlock* if_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1820 // Need this to avoid critical edge.
1821 HBasicBlock* dummy_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1822 HBasicBlock* deopt_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1823 HBasicBlock* new_pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
1824 AddBlock(if_block);
1825 AddBlock(dummy_block);
1826 AddBlock(deopt_block);
1827 AddBlock(new_pre_header);
1828
1829 header->ReplacePredecessor(pre_header, new_pre_header);
Vladimir Marko60584552015-09-03 13:35:12 +00001830 pre_header->successors_.clear();
1831 pre_header->dominated_blocks_.clear();
Mingyao Yang3584bce2015-05-19 16:01:59 -07001832
1833 pre_header->AddSuccessor(if_block);
1834 if_block->AddSuccessor(dummy_block); // True successor
1835 if_block->AddSuccessor(deopt_block); // False successor
1836 dummy_block->AddSuccessor(new_pre_header);
1837 deopt_block->AddSuccessor(new_pre_header);
1838
Vladimir Marko60584552015-09-03 13:35:12 +00001839 pre_header->dominated_blocks_.push_back(if_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001840 if_block->SetDominator(pre_header);
Vladimir Marko60584552015-09-03 13:35:12 +00001841 if_block->dominated_blocks_.push_back(dummy_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001842 dummy_block->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001843 if_block->dominated_blocks_.push_back(deopt_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001844 deopt_block->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001845 if_block->dominated_blocks_.push_back(new_pre_header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001846 new_pre_header->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001847 new_pre_header->dominated_blocks_.push_back(header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001848 header->SetDominator(new_pre_header);
1849
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001850 size_t index_of_header = IndexOfElement(reverse_post_order_, header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07001851 MakeRoomFor(&reverse_post_order_, 4, index_of_header - 1);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001852 reverse_post_order_[index_of_header++] = if_block;
1853 reverse_post_order_[index_of_header++] = dummy_block;
1854 reverse_post_order_[index_of_header++] = deopt_block;
1855 reverse_post_order_[index_of_header++] = new_pre_header;
Mingyao Yang3584bce2015-05-19 16:01:59 -07001856
1857 HLoopInformation* info = pre_header->GetLoopInformation();
1858 if (info != nullptr) {
1859 if_block->SetLoopInformation(info);
1860 dummy_block->SetLoopInformation(info);
1861 deopt_block->SetLoopInformation(info);
1862 new_pre_header->SetLoopInformation(info);
1863 for (HLoopInformationOutwardIterator loop_it(*pre_header);
1864 !loop_it.Done();
1865 loop_it.Advance()) {
1866 loop_it.Current()->Add(if_block);
1867 loop_it.Current()->Add(dummy_block);
1868 loop_it.Current()->Add(deopt_block);
1869 loop_it.Current()->Add(new_pre_header);
1870 }
1871 }
1872}
1873
Calin Juravle2e768302015-07-28 14:41:11 +00001874void HInstruction::SetReferenceTypeInfo(ReferenceTypeInfo rti) {
1875 if (kIsDebugBuild) {
1876 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1877 ScopedObjectAccess soa(Thread::Current());
1878 DCHECK(rti.IsValid()) << "Invalid RTI for " << DebugName();
1879 if (IsBoundType()) {
1880 // Having the test here spares us from making the method virtual just for
1881 // the sake of a DCHECK.
1882 ReferenceTypeInfo upper_bound_rti = AsBoundType()->GetUpperBound();
1883 DCHECK(upper_bound_rti.IsSupertypeOf(rti))
1884 << " upper_bound_rti: " << upper_bound_rti
1885 << " rti: " << rti;
David Brazdilbaf89b82015-09-15 11:36:54 +01001886 DCHECK(!upper_bound_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes() || rti.IsExact());
Calin Juravle2e768302015-07-28 14:41:11 +00001887 }
1888 }
1889 reference_type_info_ = rti;
1890}
1891
1892ReferenceTypeInfo::ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
1893
1894ReferenceTypeInfo::ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
1895 : type_handle_(type_handle), is_exact_(is_exact) {
1896 if (kIsDebugBuild) {
1897 ScopedObjectAccess soa(Thread::Current());
1898 DCHECK(IsValidHandle(type_handle));
1899 }
1900}
1901
Calin Juravleacf735c2015-02-12 15:25:22 +00001902std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1903 ScopedObjectAccess soa(Thread::Current());
1904 os << "["
Calin Juravle2e768302015-07-28 14:41:11 +00001905 << " is_valid=" << rhs.IsValid()
1906 << " type=" << (!rhs.IsValid() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
Calin Juravleacf735c2015-02-12 15:25:22 +00001907 << " is_exact=" << rhs.IsExact()
1908 << " ]";
1909 return os;
1910}
1911
Mark Mendellc4701932015-04-10 13:18:51 -04001912bool HInstruction::HasAnyEnvironmentUseBefore(HInstruction* other) {
1913 // For now, assume that instructions in different blocks may use the
1914 // environment.
1915 // TODO: Use the control flow to decide if this is true.
1916 if (GetBlock() != other->GetBlock()) {
1917 return true;
1918 }
1919
1920 // We know that we are in the same block. Walk from 'this' to 'other',
1921 // checking to see if there is any instruction with an environment.
1922 HInstruction* current = this;
1923 for (; current != other && current != nullptr; current = current->GetNext()) {
1924 // This is a conservative check, as the instruction result may not be in
1925 // the referenced environment.
1926 if (current->HasEnvironment()) {
1927 return true;
1928 }
1929 }
1930
1931 // We should have been called with 'this' before 'other' in the block.
1932 // Just confirm this.
1933 DCHECK(current != nullptr);
1934 return false;
1935}
1936
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001937void HInvoke::SetIntrinsic(Intrinsics intrinsic,
1938 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
1939 intrinsic_ = intrinsic;
1940 IntrinsicOptimizations opt(this);
1941 if (needs_env_or_cache == kNoEnvironmentOrCache) {
1942 opt.SetDoesNotNeedDexCache();
1943 opt.SetDoesNotNeedEnvironment();
1944 }
1945}
1946
1947bool HInvoke::NeedsEnvironment() const {
1948 if (!IsIntrinsic()) {
1949 return true;
1950 }
1951 IntrinsicOptimizations opt(*this);
1952 return !opt.GetDoesNotNeedEnvironment();
1953}
1954
Vladimir Markodc151b22015-10-15 18:02:30 +01001955bool HInvokeStaticOrDirect::NeedsDexCacheOfDeclaringClass() const {
1956 if (GetMethodLoadKind() != MethodLoadKind::kDexCacheViaMethod) {
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001957 return false;
1958 }
1959 if (!IsIntrinsic()) {
1960 return true;
1961 }
1962 IntrinsicOptimizations opt(*this);
1963 return !opt.GetDoesNotNeedDexCache();
1964}
1965
Mark Mendellc4701932015-04-10 13:18:51 -04001966void HInstruction::RemoveEnvironmentUsers() {
1967 for (HUseIterator<HEnvironment*> use_it(GetEnvUses()); !use_it.Done(); use_it.Advance()) {
1968 HUseListNode<HEnvironment*>* user_node = use_it.Current();
1969 HEnvironment* user = user_node->GetUser();
1970 user->SetRawEnvAt(user_node->GetIndex(), nullptr);
1971 }
1972 env_uses_.Clear();
1973}
1974
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001975} // namespace art