blob: fa580d9bed65509b18a792ee7b33f9373dd88485 [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 */
Nicolas Geoffray818f2102014-02-18 16:43:35 +000016#include "nodes.h"
Calin Juravle77520bc2015-01-12 18:45:46 +000017
Roland Levillain31dd3d62016-02-16 12:21:02 +000018#include <cfloat>
19
Andreas Gampec6ea7d02017-02-01 16:46:28 -080020#include "art_method-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070021#include "base/bit_utils.h"
22#include "base/bit_vector-inl.h"
23#include "base/stl_util.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080024#include "class_linker-inl.h"
Mark Mendelle82549b2015-05-06 10:55:34 -040025#include "code_generator.h"
Vladimir Marko391d01f2015-11-06 11:02:08 +000026#include "common_dominator.h"
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010027#include "intrinsics.h"
David Brazdilbaf89b82015-09-15 11:36:54 +010028#include "mirror/class-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070029#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070030#include "ssa_builder.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031
32namespace art {
33
Roland Levillain31dd3d62016-02-16 12:21:02 +000034// Enable floating-point static evaluation during constant folding
35// only if all floating-point operations and constants evaluate in the
36// range and precision of the type used (i.e., 32-bit float, 64-bit
37// double).
38static constexpr bool kEnableFloatingPointStaticEvaluation = (FLT_EVAL_METHOD == 0);
39
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070040void HGraph::InitializeInexactObjectRTI(VariableSizedHandleScope* handles) {
David Brazdilbadd8262016-02-02 16:28:56 +000041 ScopedObjectAccess soa(Thread::Current());
42 // Create the inexact Object reference type and store it in the HGraph.
43 ClassLinker* linker = Runtime::Current()->GetClassLinker();
44 inexact_object_rti_ = ReferenceTypeInfo::Create(
45 handles->NewHandle(linker->GetClassRoot(ClassLinker::kJavaLangObject)),
46 /* is_exact */ false);
47}
48
Nicolas Geoffray818f2102014-02-18 16:43:35 +000049void HGraph::AddBlock(HBasicBlock* block) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010050 block->SetBlockId(blocks_.size());
51 blocks_.push_back(block);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052}
53
Nicolas Geoffray804d0932014-05-02 08:46:00 +010054void HGraph::FindBackEdges(ArenaBitVector* visited) {
Vladimir Marko1f8695c2015-09-24 13:11:31 +010055 // "visited" must be empty on entry, it's an output argument for all visited (i.e. live) blocks.
56 DCHECK_EQ(visited->GetHighestBitSet(), -1);
57
Vladimir Marko69d310e2017-10-09 14:12:23 +010058 // Allocate memory from local ScopedArenaAllocator.
59 ScopedArenaAllocator allocator(GetArenaStack());
Vladimir Marko1f8695c2015-09-24 13:11:31 +010060 // Nodes that we're currently visiting, indexed by block id.
Vladimir Marko69d310e2017-10-09 14:12:23 +010061 ArenaBitVector visiting(
62 &allocator, blocks_.size(), /* expandable */ false, kArenaAllocGraphBuilder);
63 visiting.ClearAllBits();
Vladimir Marko1f8695c2015-09-24 13:11:31 +010064 // Number of successors visited from a given node, indexed by block id.
Vladimir Marko69d310e2017-10-09 14:12:23 +010065 ScopedArenaVector<size_t> successors_visited(blocks_.size(),
66 0u,
67 allocator.Adapter(kArenaAllocGraphBuilder));
Vladimir Marko1f8695c2015-09-24 13:11:31 +010068 // Stack of nodes that we're currently visiting (same as marked in "visiting" above).
Vladimir Marko69d310e2017-10-09 14:12:23 +010069 ScopedArenaVector<HBasicBlock*> worklist(allocator.Adapter(kArenaAllocGraphBuilder));
Vladimir Marko1f8695c2015-09-24 13:11:31 +010070 constexpr size_t kDefaultWorklistSize = 8;
71 worklist.reserve(kDefaultWorklistSize);
72 visited->SetBit(entry_block_->GetBlockId());
73 visiting.SetBit(entry_block_->GetBlockId());
74 worklist.push_back(entry_block_);
75
76 while (!worklist.empty()) {
77 HBasicBlock* current = worklist.back();
78 uint32_t current_id = current->GetBlockId();
79 if (successors_visited[current_id] == current->GetSuccessors().size()) {
80 visiting.ClearBit(current_id);
81 worklist.pop_back();
82 } else {
Vladimir Marko1f8695c2015-09-24 13:11:31 +010083 HBasicBlock* successor = current->GetSuccessors()[successors_visited[current_id]++];
84 uint32_t successor_id = successor->GetBlockId();
85 if (visiting.IsBitSet(successor_id)) {
86 DCHECK(ContainsElement(worklist, successor));
87 successor->AddBackEdge(current);
88 } else if (!visited->IsBitSet(successor_id)) {
89 visited->SetBit(successor_id);
90 visiting.SetBit(successor_id);
91 worklist.push_back(successor);
92 }
93 }
94 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000095}
96
Artem Serov21c7e6f2017-07-27 16:04:42 +010097// Remove the environment use records of the instruction for users.
98void RemoveEnvironmentUses(HInstruction* instruction) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010099 for (HEnvironment* environment = instruction->GetEnvironment();
100 environment != nullptr;
101 environment = environment->GetParent()) {
Roland Levillainfc600dc2014-12-02 17:16:31 +0000102 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
David Brazdil1abb4192015-02-17 18:33:36 +0000103 if (environment->GetInstructionAt(i) != nullptr) {
104 environment->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000105 }
106 }
107 }
108}
109
Artem Serov21c7e6f2017-07-27 16:04:42 +0100110// Return whether the instruction has an environment and it's used by others.
111bool HasEnvironmentUsedByOthers(HInstruction* instruction) {
112 for (HEnvironment* environment = instruction->GetEnvironment();
113 environment != nullptr;
114 environment = environment->GetParent()) {
115 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
116 HInstruction* user = environment->GetInstructionAt(i);
117 if (user != nullptr) {
118 return true;
119 }
120 }
121 }
122 return false;
123}
124
125// Reset environment records of the instruction itself.
126void ResetEnvironmentInputRecords(HInstruction* instruction) {
127 for (HEnvironment* environment = instruction->GetEnvironment();
128 environment != nullptr;
129 environment = environment->GetParent()) {
130 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
131 DCHECK(environment->GetHolder() == instruction);
132 if (environment->GetInstructionAt(i) != nullptr) {
133 environment->SetRawEnvAt(i, nullptr);
134 }
135 }
136 }
137}
138
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000139static void RemoveAsUser(HInstruction* instruction) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100140 instruction->RemoveAsUserOfAllInputs();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000141 RemoveEnvironmentUses(instruction);
142}
143
Roland Levillainfc600dc2014-12-02 17:16:31 +0000144void HGraph::RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100145 for (size_t i = 0; i < blocks_.size(); ++i) {
Roland Levillainfc600dc2014-12-02 17:16:31 +0000146 if (!visited.IsBitSet(i)) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100147 HBasicBlock* block = blocks_[i];
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000148 if (block == nullptr) continue;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100149 DCHECK(block->GetPhis().IsEmpty()) << "Phis are not inserted at this stage";
Roland Levillainfc600dc2014-12-02 17:16:31 +0000150 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
151 RemoveAsUser(it.Current());
152 }
153 }
154 }
155}
156
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100157void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100158 for (size_t i = 0; i < blocks_.size(); ++i) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000159 if (!visited.IsBitSet(i)) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100160 HBasicBlock* block = blocks_[i];
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000161 if (block == nullptr) continue;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100162 // We only need to update the successor, which might be live.
Vladimir Marko60584552015-09-03 13:35:12 +0000163 for (HBasicBlock* successor : block->GetSuccessors()) {
164 successor->RemovePredecessor(block);
David Brazdil1abb4192015-02-17 18:33:36 +0000165 }
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100166 // Remove the block from the list of blocks, so that further analyses
167 // never see it.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100168 blocks_[i] = nullptr;
Serguei Katkov7ba99662016-03-02 16:25:36 +0600169 if (block->IsExitBlock()) {
170 SetExitBlock(nullptr);
171 }
David Brazdil86ea7ee2016-02-16 09:26:07 +0000172 // Mark the block as removed. This is used by the HGraphBuilder to discard
173 // the block as a branch target.
174 block->SetGraph(nullptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000175 }
176 }
177}
178
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000179GraphAnalysisResult HGraph::BuildDominatorTree() {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100180 // Allocate memory from local ScopedArenaAllocator.
181 ScopedArenaAllocator allocator(GetArenaStack());
182
183 ArenaBitVector visited(&allocator, blocks_.size(), false, kArenaAllocGraphBuilder);
184 visited.ClearAllBits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000185
David Brazdil86ea7ee2016-02-16 09:26:07 +0000186 // (1) Find the back edges in the graph doing a DFS traversal.
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000187 FindBackEdges(&visited);
188
David Brazdil86ea7ee2016-02-16 09:26:07 +0000189 // (2) Remove instructions and phis from blocks not visited during
Roland Levillainfc600dc2014-12-02 17:16:31 +0000190 // the initial DFS as users from other instructions, so that
191 // users can be safely removed before uses later.
192 RemoveInstructionsAsUsersFromDeadBlocks(visited);
193
David Brazdil86ea7ee2016-02-16 09:26:07 +0000194 // (3) Remove blocks not visited during the initial DFS.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000195 // Step (5) requires dead blocks to be removed from the
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000196 // predecessors list of live blocks.
197 RemoveDeadBlocks(visited);
198
David Brazdil86ea7ee2016-02-16 09:26:07 +0000199 // (4) Simplify the CFG now, so that we don't need to recompute
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100200 // dominators and the reverse post order.
201 SimplifyCFG();
202
David Brazdil86ea7ee2016-02-16 09:26:07 +0000203 // (5) Compute the dominance information and the reverse post order.
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100204 ComputeDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000205
David Brazdil86ea7ee2016-02-16 09:26:07 +0000206 // (6) Analyze loops discovered through back edge analysis, and
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000207 // set the loop information on each block.
208 GraphAnalysisResult result = AnalyzeLoops();
209 if (result != kAnalysisSuccess) {
210 return result;
211 }
212
David Brazdil86ea7ee2016-02-16 09:26:07 +0000213 // (7) Precompute per-block try membership before entering the SSA builder,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000214 // which needs the information to build catch block phis from values of
215 // locals at throwing instructions inside try blocks.
216 ComputeTryBlockInformation();
217
218 return kAnalysisSuccess;
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100219}
220
221void HGraph::ClearDominanceInformation() {
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100222 for (HBasicBlock* block : GetReversePostOrder()) {
223 block->ClearDominanceInformation();
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100224 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100225 reverse_post_order_.clear();
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100226}
227
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000228void HGraph::ClearLoopInformation() {
229 SetHasIrreducibleLoops(false);
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100230 for (HBasicBlock* block : GetReversePostOrder()) {
231 block->SetLoopInformation(nullptr);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000232 }
233}
234
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100235void HBasicBlock::ClearDominanceInformation() {
Vladimir Marko60584552015-09-03 13:35:12 +0000236 dominated_blocks_.clear();
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100237 dominator_ = nullptr;
238}
239
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000240HInstruction* HBasicBlock::GetFirstInstructionDisregardMoves() const {
241 HInstruction* instruction = GetFirstInstruction();
242 while (instruction->IsParallelMove()) {
243 instruction = instruction->GetNext();
244 }
245 return instruction;
246}
247
David Brazdil3f4a5222016-05-06 12:46:21 +0100248static bool UpdateDominatorOfSuccessor(HBasicBlock* block, HBasicBlock* successor) {
249 DCHECK(ContainsElement(block->GetSuccessors(), successor));
250
251 HBasicBlock* old_dominator = successor->GetDominator();
252 HBasicBlock* new_dominator =
253 (old_dominator == nullptr) ? block
254 : CommonDominator::ForPair(old_dominator, block);
255
256 if (old_dominator == new_dominator) {
257 return false;
258 } else {
259 successor->SetDominator(new_dominator);
260 return true;
261 }
262}
263
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100264void HGraph::ComputeDominanceInformation() {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100265 DCHECK(reverse_post_order_.empty());
266 reverse_post_order_.reserve(blocks_.size());
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100267 reverse_post_order_.push_back(entry_block_);
Vladimir Markod76d1392015-09-23 16:07:14 +0100268
Vladimir Marko69d310e2017-10-09 14:12:23 +0100269 // Allocate memory from local ScopedArenaAllocator.
270 ScopedArenaAllocator allocator(GetArenaStack());
Vladimir Markod76d1392015-09-23 16:07:14 +0100271 // Number of visits of a given node, indexed by block id.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100272 ScopedArenaVector<size_t> visits(blocks_.size(), 0u, allocator.Adapter(kArenaAllocGraphBuilder));
Vladimir Markod76d1392015-09-23 16:07:14 +0100273 // Number of successors visited from a given node, indexed by block id.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100274 ScopedArenaVector<size_t> successors_visited(blocks_.size(),
275 0u,
276 allocator.Adapter(kArenaAllocGraphBuilder));
Vladimir Markod76d1392015-09-23 16:07:14 +0100277 // Nodes for which we need to visit successors.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100278 ScopedArenaVector<HBasicBlock*> worklist(allocator.Adapter(kArenaAllocGraphBuilder));
Vladimir Markod76d1392015-09-23 16:07:14 +0100279 constexpr size_t kDefaultWorklistSize = 8;
280 worklist.reserve(kDefaultWorklistSize);
281 worklist.push_back(entry_block_);
282
283 while (!worklist.empty()) {
284 HBasicBlock* current = worklist.back();
285 uint32_t current_id = current->GetBlockId();
286 if (successors_visited[current_id] == current->GetSuccessors().size()) {
287 worklist.pop_back();
288 } else {
Vladimir Markod76d1392015-09-23 16:07:14 +0100289 HBasicBlock* successor = current->GetSuccessors()[successors_visited[current_id]++];
David Brazdil3f4a5222016-05-06 12:46:21 +0100290 UpdateDominatorOfSuccessor(current, successor);
Vladimir Markod76d1392015-09-23 16:07:14 +0100291
292 // Once all the forward edges have been visited, we know the immediate
293 // dominator of the block. We can then start visiting its successors.
Vladimir Markod76d1392015-09-23 16:07:14 +0100294 if (++visits[successor->GetBlockId()] ==
295 successor->GetPredecessors().size() - successor->NumberOfBackEdges()) {
Vladimir Markod76d1392015-09-23 16:07:14 +0100296 reverse_post_order_.push_back(successor);
297 worklist.push_back(successor);
298 }
299 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000300 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000301
David Brazdil3f4a5222016-05-06 12:46:21 +0100302 // Check if the graph has back edges not dominated by their respective headers.
303 // If so, we need to update the dominators of those headers and recursively of
304 // their successors. We do that with a fix-point iteration over all blocks.
305 // The algorithm is guaranteed to terminate because it loops only if the sum
306 // of all dominator chains has decreased in the current iteration.
307 bool must_run_fix_point = false;
308 for (HBasicBlock* block : blocks_) {
309 if (block != nullptr &&
310 block->IsLoopHeader() &&
311 block->GetLoopInformation()->HasBackEdgeNotDominatedByHeader()) {
312 must_run_fix_point = true;
313 break;
314 }
315 }
316 if (must_run_fix_point) {
317 bool update_occurred = true;
318 while (update_occurred) {
319 update_occurred = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100320 for (HBasicBlock* block : GetReversePostOrder()) {
David Brazdil3f4a5222016-05-06 12:46:21 +0100321 for (HBasicBlock* successor : block->GetSuccessors()) {
322 update_occurred |= UpdateDominatorOfSuccessor(block, successor);
323 }
324 }
325 }
326 }
327
328 // Make sure that there are no remaining blocks whose dominator information
329 // needs to be updated.
330 if (kIsDebugBuild) {
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100331 for (HBasicBlock* block : GetReversePostOrder()) {
David Brazdil3f4a5222016-05-06 12:46:21 +0100332 for (HBasicBlock* successor : block->GetSuccessors()) {
333 DCHECK(!UpdateDominatorOfSuccessor(block, successor));
334 }
335 }
336 }
337
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000338 // Populate `dominated_blocks_` information after computing all dominators.
Roland Levillainc9b21f82016-03-23 16:36:59 +0000339 // The potential presence of irreducible loops requires to do it after.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100340 for (HBasicBlock* block : GetReversePostOrder()) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000341 if (!block->IsEntryBlock()) {
342 block->GetDominator()->AddDominatedBlock(block);
343 }
344 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000345}
346
David Brazdilfc6a86a2015-06-26 10:33:45 +0000347HBasicBlock* HGraph::SplitEdge(HBasicBlock* block, HBasicBlock* successor) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100348 HBasicBlock* new_block = new (allocator_) HBasicBlock(this, successor->GetDexPc());
David Brazdil3e187382015-06-26 09:59:52 +0000349 AddBlock(new_block);
David Brazdil3e187382015-06-26 09:59:52 +0000350 // Use `InsertBetween` to ensure the predecessor index and successor index of
351 // `block` and `successor` are preserved.
352 new_block->InsertBetween(block, successor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000353 return new_block;
354}
355
356void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) {
357 // Insert a new node between `block` and `successor` to split the
358 // critical edge.
359 HBasicBlock* new_block = SplitEdge(block, successor);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100360 new_block->AddInstruction(new (allocator_) HGoto(successor->GetDexPc()));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100361 if (successor->IsLoopHeader()) {
362 // If we split at a back edge boundary, make the new block the back edge.
363 HLoopInformation* info = successor->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000364 if (info->IsBackEdge(*block)) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100365 info->RemoveBackEdge(block);
366 info->AddBackEdge(new_block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100367 }
368 }
369}
370
Artem Serovc73ee372017-07-31 15:08:40 +0100371// Reorder phi inputs to match reordering of the block's predecessors.
372static void FixPhisAfterPredecessorsReodering(HBasicBlock* block, size_t first, size_t second) {
373 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
374 HPhi* phi = it.Current()->AsPhi();
375 HInstruction* first_instr = phi->InputAt(first);
376 HInstruction* second_instr = phi->InputAt(second);
377 phi->ReplaceInput(first_instr, second);
378 phi->ReplaceInput(second_instr, first);
379 }
380}
381
382// Make sure that the first predecessor of a loop header is the incoming block.
383void HGraph::OrderLoopHeaderPredecessors(HBasicBlock* header) {
384 DCHECK(header->IsLoopHeader());
385 HLoopInformation* info = header->GetLoopInformation();
386 if (info->IsBackEdge(*header->GetPredecessors()[0])) {
387 HBasicBlock* to_swap = header->GetPredecessors()[0];
388 for (size_t pred = 1, e = header->GetPredecessors().size(); pred < e; ++pred) {
389 HBasicBlock* predecessor = header->GetPredecessors()[pred];
390 if (!info->IsBackEdge(*predecessor)) {
391 header->predecessors_[pred] = to_swap;
392 header->predecessors_[0] = predecessor;
393 FixPhisAfterPredecessorsReodering(header, 0, pred);
394 break;
395 }
396 }
397 }
398}
399
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100400void HGraph::SimplifyLoop(HBasicBlock* header) {
401 HLoopInformation* info = header->GetLoopInformation();
402
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100403 // Make sure the loop has only one pre header. This simplifies SSA building by having
404 // to just look at the pre header to know which locals are initialized at entry of the
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000405 // loop. Also, don't allow the entry block to be a pre header: this simplifies inlining
406 // this graph.
Vladimir Marko60584552015-09-03 13:35:12 +0000407 size_t number_of_incomings = header->GetPredecessors().size() - info->NumberOfBackEdges();
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000408 if (number_of_incomings != 1 || (GetEntryBlock()->GetSingleSuccessor() == header)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100409 HBasicBlock* pre_header = new (allocator_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100410 AddBlock(pre_header);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100411 pre_header->AddInstruction(new (allocator_) HGoto(header->GetDexPc()));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100412
Vladimir Marko60584552015-09-03 13:35:12 +0000413 for (size_t pred = 0; pred < header->GetPredecessors().size(); ++pred) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100414 HBasicBlock* predecessor = header->GetPredecessors()[pred];
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100415 if (!info->IsBackEdge(*predecessor)) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100416 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100417 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100418 }
419 }
420 pre_header->AddSuccessor(header);
421 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100422
Artem Serovc73ee372017-07-31 15:08:40 +0100423 OrderLoopHeaderPredecessors(header);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100424
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100425 HInstruction* first_instruction = header->GetFirstInstruction();
David Brazdildee58d62016-04-07 09:54:26 +0000426 if (first_instruction != nullptr && first_instruction->IsSuspendCheck()) {
427 // Called from DeadBlockElimination. Update SuspendCheck pointer.
428 info->SetSuspendCheck(first_instruction->AsSuspendCheck());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100429 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100430}
431
David Brazdilffee3d32015-07-06 11:48:53 +0100432void HGraph::ComputeTryBlockInformation() {
433 // Iterate in reverse post order to propagate try membership information from
434 // predecessors to their successors.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100435 for (HBasicBlock* block : GetReversePostOrder()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100436 if (block->IsEntryBlock() || block->IsCatchBlock()) {
437 // Catch blocks after simplification have only exceptional predecessors
438 // and hence are never in tries.
439 continue;
440 }
441
442 // Infer try membership from the first predecessor. Having simplified loops,
443 // the first predecessor can never be a back edge and therefore it must have
444 // been visited already and had its try membership set.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100445 HBasicBlock* first_predecessor = block->GetPredecessors()[0];
David Brazdilffee3d32015-07-06 11:48:53 +0100446 DCHECK(!block->IsLoopHeader() || !block->GetLoopInformation()->IsBackEdge(*first_predecessor));
David Brazdilec16f792015-08-19 15:04:01 +0100447 const HTryBoundary* try_entry = first_predecessor->ComputeTryEntryOfSuccessors();
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000448 if (try_entry != nullptr &&
449 (block->GetTryCatchInformation() == nullptr ||
450 try_entry != &block->GetTryCatchInformation()->GetTryEntry())) {
451 // We are either setting try block membership for the first time or it
452 // has changed.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100453 block->SetTryCatchInformation(new (allocator_) TryCatchInformation(*try_entry));
David Brazdilec16f792015-08-19 15:04:01 +0100454 }
David Brazdilffee3d32015-07-06 11:48:53 +0100455 }
456}
457
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100458void HGraph::SimplifyCFG() {
David Brazdildb51efb2015-11-06 01:36:20 +0000459// Simplify the CFG for future analysis, and code generation:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100460 // (1): Split critical edges.
David Brazdildb51efb2015-11-06 01:36:20 +0000461 // (2): Simplify loops by having only one preheader.
Vladimir Markob7d8e8c2015-09-17 15:47:05 +0100462 // NOTE: We're appending new blocks inside the loop, so we need to use index because iterators
463 // can be invalidated. We remember the initial size to avoid iterating over the new blocks.
464 for (size_t block_id = 0u, end = blocks_.size(); block_id != end; ++block_id) {
465 HBasicBlock* block = blocks_[block_id];
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100466 if (block == nullptr) continue;
David Brazdildb51efb2015-11-06 01:36:20 +0000467 if (block->GetSuccessors().size() > 1) {
468 // Only split normal-flow edges. We cannot split exceptional edges as they
469 // are synthesized (approximate real control flow), and we do not need to
470 // anyway. Moves that would be inserted there are performed by the runtime.
David Brazdild26a4112015-11-10 11:07:31 +0000471 ArrayRef<HBasicBlock* const> normal_successors = block->GetNormalSuccessors();
472 for (size_t j = 0, e = normal_successors.size(); j < e; ++j) {
473 HBasicBlock* successor = normal_successors[j];
David Brazdilffee3d32015-07-06 11:48:53 +0100474 DCHECK(!successor->IsCatchBlock());
David Brazdildb51efb2015-11-06 01:36:20 +0000475 if (successor == exit_block_) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000476 // (Throw/Return/ReturnVoid)->TryBoundary->Exit. Special case which we
477 // do not want to split because Goto->Exit is not allowed.
David Brazdildb51efb2015-11-06 01:36:20 +0000478 DCHECK(block->IsSingleTryBoundary());
David Brazdildb51efb2015-11-06 01:36:20 +0000479 } else if (successor->GetPredecessors().size() > 1) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100480 SplitCriticalEdge(block, successor);
David Brazdild26a4112015-11-10 11:07:31 +0000481 // SplitCriticalEdge could have invalidated the `normal_successors`
482 // ArrayRef. We must re-acquire it.
483 normal_successors = block->GetNormalSuccessors();
484 DCHECK_EQ(normal_successors[j]->GetSingleSuccessor(), successor);
485 DCHECK_EQ(e, normal_successors.size());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100486 }
487 }
488 }
489 if (block->IsLoopHeader()) {
490 SimplifyLoop(block);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000491 } else if (!block->IsEntryBlock() &&
492 block->GetFirstInstruction() != nullptr &&
493 block->GetFirstInstruction()->IsSuspendCheck()) {
494 // We are being called by the dead code elimiation pass, and what used to be
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000495 // a loop got dismantled. Just remove the suspend check.
496 block->RemoveInstruction(block->GetFirstInstruction());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100497 }
498 }
499}
500
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000501GraphAnalysisResult HGraph::AnalyzeLoops() const {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100502 // We iterate post order to ensure we visit inner loops before outer loops.
503 // `PopulateRecursive` needs this guarantee to know whether a natural loop
504 // contains an irreducible loop.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100505 for (HBasicBlock* block : GetPostOrder()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100506 if (block->IsLoopHeader()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100507 if (block->IsCatchBlock()) {
508 // TODO: Dealing with exceptional back edges could be tricky because
509 // they only approximate the real control flow. Bail out for now.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000510 return kAnalysisFailThrowCatchLoop;
David Brazdilffee3d32015-07-06 11:48:53 +0100511 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000512 block->GetLoopInformation()->Populate();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100513 }
514 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000515 return kAnalysisSuccess;
516}
517
518void HLoopInformation::Dump(std::ostream& os) {
519 os << "header: " << header_->GetBlockId() << std::endl;
520 os << "pre header: " << GetPreHeader()->GetBlockId() << std::endl;
521 for (HBasicBlock* block : back_edges_) {
522 os << "back edge: " << block->GetBlockId() << std::endl;
523 }
524 for (HBasicBlock* block : header_->GetPredecessors()) {
525 os << "predecessor: " << block->GetBlockId() << std::endl;
526 }
527 for (uint32_t idx : blocks_.Indexes()) {
528 os << " in loop: " << idx << std::endl;
529 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100530}
531
David Brazdil8d5b8b22015-03-24 10:51:52 +0000532void HGraph::InsertConstant(HConstant* constant) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000533 // New constants are inserted before the SuspendCheck at the bottom of the
534 // entry block. Note that this method can be called from the graph builder and
535 // the entry block therefore may not end with SuspendCheck->Goto yet.
536 HInstruction* insert_before = nullptr;
537
538 HInstruction* gota = entry_block_->GetLastInstruction();
539 if (gota != nullptr && gota->IsGoto()) {
540 HInstruction* suspend_check = gota->GetPrevious();
541 if (suspend_check != nullptr && suspend_check->IsSuspendCheck()) {
542 insert_before = suspend_check;
543 } else {
544 insert_before = gota;
545 }
546 }
547
548 if (insert_before == nullptr) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000549 entry_block_->AddInstruction(constant);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000550 } else {
551 entry_block_->InsertInstructionBefore(constant, insert_before);
David Brazdil46e2a392015-03-16 17:31:52 +0000552 }
553}
554
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600555HNullConstant* HGraph::GetNullConstant(uint32_t dex_pc) {
Nicolas Geoffray18e68732015-06-17 23:09:05 +0100556 // For simplicity, don't bother reviving the cached null constant if it is
557 // not null and not in a block. Otherwise, we need to clear the instruction
558 // id and/or any invariants the graph is assuming when adding new instructions.
559 if ((cached_null_constant_ == nullptr) || (cached_null_constant_->GetBlock() == nullptr)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100560 cached_null_constant_ = new (allocator_) HNullConstant(dex_pc);
David Brazdil4833f5a2015-12-16 10:37:39 +0000561 cached_null_constant_->SetReferenceTypeInfo(inexact_object_rti_);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000562 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000563 }
David Brazdil4833f5a2015-12-16 10:37:39 +0000564 if (kIsDebugBuild) {
565 ScopedObjectAccess soa(Thread::Current());
566 DCHECK(cached_null_constant_->GetReferenceTypeInfo().IsValid());
567 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000568 return cached_null_constant_;
569}
570
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100571HCurrentMethod* HGraph::GetCurrentMethod() {
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100572 // For simplicity, don't bother reviving the cached current method if it is
573 // not null and not in a block. Otherwise, we need to clear the instruction
574 // id and/or any invariants the graph is assuming when adding new instructions.
575 if ((cached_current_method_ == nullptr) || (cached_current_method_->GetBlock() == nullptr)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100576 cached_current_method_ = new (allocator_) HCurrentMethod(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100577 Is64BitInstructionSet(instruction_set_) ? DataType::Type::kInt64 : DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600578 entry_block_->GetDexPc());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100579 if (entry_block_->GetFirstInstruction() == nullptr) {
580 entry_block_->AddInstruction(cached_current_method_);
581 } else {
582 entry_block_->InsertInstructionBefore(
583 cached_current_method_, entry_block_->GetFirstInstruction());
584 }
585 }
586 return cached_current_method_;
587}
588
Igor Murashkind01745e2017-04-05 16:40:31 -0700589const char* HGraph::GetMethodName() const {
590 const DexFile::MethodId& method_id = dex_file_.GetMethodId(method_idx_);
591 return dex_file_.GetMethodName(method_id);
592}
593
594std::string HGraph::PrettyMethod(bool with_signature) const {
595 return dex_file_.PrettyMethod(method_idx_, with_signature);
596}
597
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100598HConstant* HGraph::GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000599 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100600 case DataType::Type::kBool:
David Brazdil8d5b8b22015-03-24 10:51:52 +0000601 DCHECK(IsUint<1>(value));
602 FALLTHROUGH_INTENDED;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100603 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100604 case DataType::Type::kInt8:
605 case DataType::Type::kUint16:
606 case DataType::Type::kInt16:
607 case DataType::Type::kInt32:
608 DCHECK(IsInt(DataType::Size(type) * kBitsPerByte, value));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600609 return GetIntConstant(static_cast<int32_t>(value), dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100611 case DataType::Type::kInt64:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600612 return GetLongConstant(value, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000613
614 default:
615 LOG(FATAL) << "Unsupported constant type";
616 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000617 }
David Brazdil46e2a392015-03-16 17:31:52 +0000618}
619
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000620void HGraph::CacheFloatConstant(HFloatConstant* constant) {
621 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
622 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
623 cached_float_constants_.Overwrite(value, constant);
624}
625
626void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
627 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
628 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
629 cached_double_constants_.Overwrite(value, constant);
630}
631
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000632void HLoopInformation::Add(HBasicBlock* block) {
633 blocks_.SetBit(block->GetBlockId());
634}
635
David Brazdil46e2a392015-03-16 17:31:52 +0000636void HLoopInformation::Remove(HBasicBlock* block) {
637 blocks_.ClearBit(block->GetBlockId());
638}
639
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100640void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
641 if (blocks_.IsBitSet(block->GetBlockId())) {
642 return;
643 }
644
645 blocks_.SetBit(block->GetBlockId());
646 block->SetInLoop(this);
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100647 if (block->IsLoopHeader()) {
648 // We're visiting loops in post-order, so inner loops must have been
649 // populated already.
650 DCHECK(block->GetLoopInformation()->IsPopulated());
651 if (block->GetLoopInformation()->IsIrreducible()) {
652 contains_irreducible_loop_ = true;
653 }
654 }
Vladimir Marko60584552015-09-03 13:35:12 +0000655 for (HBasicBlock* predecessor : block->GetPredecessors()) {
656 PopulateRecursive(predecessor);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100657 }
658}
659
David Brazdilc2e8af92016-04-05 17:15:19 +0100660void HLoopInformation::PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized) {
661 size_t block_id = block->GetBlockId();
662
663 // If `block` is in `finalized`, we know its membership in the loop has been
664 // decided and it does not need to be revisited.
665 if (finalized->IsBitSet(block_id)) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000666 return;
667 }
668
David Brazdilc2e8af92016-04-05 17:15:19 +0100669 bool is_finalized = false;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000670 if (block->IsLoopHeader()) {
671 // If we hit a loop header in an irreducible loop, we first check if the
672 // pre header of that loop belongs to the currently analyzed loop. If it does,
673 // then we visit the back edges.
674 // Note that we cannot use GetPreHeader, as the loop may have not been populated
675 // yet.
676 HBasicBlock* pre_header = block->GetPredecessors()[0];
David Brazdilc2e8af92016-04-05 17:15:19 +0100677 PopulateIrreducibleRecursive(pre_header, finalized);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000678 if (blocks_.IsBitSet(pre_header->GetBlockId())) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000679 block->SetInLoop(this);
David Brazdilc2e8af92016-04-05 17:15:19 +0100680 blocks_.SetBit(block_id);
681 finalized->SetBit(block_id);
682 is_finalized = true;
683
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000684 HLoopInformation* info = block->GetLoopInformation();
685 for (HBasicBlock* back_edge : info->GetBackEdges()) {
David Brazdilc2e8af92016-04-05 17:15:19 +0100686 PopulateIrreducibleRecursive(back_edge, finalized);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000687 }
688 }
689 } else {
690 // Visit all predecessors. If one predecessor is part of the loop, this
691 // block is also part of this loop.
692 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdilc2e8af92016-04-05 17:15:19 +0100693 PopulateIrreducibleRecursive(predecessor, finalized);
694 if (!is_finalized && blocks_.IsBitSet(predecessor->GetBlockId())) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000695 block->SetInLoop(this);
David Brazdilc2e8af92016-04-05 17:15:19 +0100696 blocks_.SetBit(block_id);
697 finalized->SetBit(block_id);
698 is_finalized = true;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000699 }
700 }
701 }
David Brazdilc2e8af92016-04-05 17:15:19 +0100702
703 // All predecessors have been recursively visited. Mark finalized if not marked yet.
704 if (!is_finalized) {
705 finalized->SetBit(block_id);
706 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000707}
708
709void HLoopInformation::Populate() {
David Brazdila4b8c212015-05-07 09:59:30 +0100710 DCHECK_EQ(blocks_.NumSetBits(), 0u) << "Loop information has already been populated";
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000711 // Populate this loop: starting with the back edge, recursively add predecessors
712 // that are not already part of that loop. Set the header as part of the loop
713 // to end the recursion.
714 // This is a recursive implementation of the algorithm described in
715 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
David Brazdilc2e8af92016-04-05 17:15:19 +0100716 HGraph* graph = header_->GetGraph();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000717 blocks_.SetBit(header_->GetBlockId());
718 header_->SetInLoop(this);
David Brazdilc2e8af92016-04-05 17:15:19 +0100719
David Brazdil3f4a5222016-05-06 12:46:21 +0100720 bool is_irreducible_loop = HasBackEdgeNotDominatedByHeader();
David Brazdilc2e8af92016-04-05 17:15:19 +0100721
722 if (is_irreducible_loop) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100723 // Allocate memory from local ScopedArenaAllocator.
724 ScopedArenaAllocator allocator(graph->GetArenaStack());
725 ArenaBitVector visited(&allocator,
David Brazdilc2e8af92016-04-05 17:15:19 +0100726 graph->GetBlocks().size(),
727 /* expandable */ false,
728 kArenaAllocGraphBuilder);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100729 visited.ClearAllBits();
David Brazdil5a620592016-05-05 11:27:03 +0100730 // Stop marking blocks at the loop header.
731 visited.SetBit(header_->GetBlockId());
732
David Brazdilc2e8af92016-04-05 17:15:19 +0100733 for (HBasicBlock* back_edge : GetBackEdges()) {
734 PopulateIrreducibleRecursive(back_edge, &visited);
735 }
736 } else {
737 for (HBasicBlock* back_edge : GetBackEdges()) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000738 PopulateRecursive(back_edge);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100739 }
David Brazdila4b8c212015-05-07 09:59:30 +0100740 }
David Brazdilc2e8af92016-04-05 17:15:19 +0100741
Vladimir Markofd66c502016-04-18 15:37:01 +0100742 if (!is_irreducible_loop && graph->IsCompilingOsr()) {
743 // When compiling in OSR mode, all loops in the compiled method may be entered
744 // from the interpreter. We treat this OSR entry point just like an extra entry
745 // to an irreducible loop, so we need to mark the method's loops as irreducible.
746 // This does not apply to inlined loops which do not act as OSR entry points.
747 if (suspend_check_ == nullptr) {
748 // Just building the graph in OSR mode, this loop is not inlined. We never build an
749 // inner graph in OSR mode as we can do OSR transition only from the outer method.
750 is_irreducible_loop = true;
751 } else {
752 // Look at the suspend check's environment to determine if the loop was inlined.
753 DCHECK(suspend_check_->HasEnvironment());
754 if (!suspend_check_->GetEnvironment()->IsFromInlinedInvoke()) {
755 is_irreducible_loop = true;
756 }
757 }
758 }
759 if (is_irreducible_loop) {
David Brazdilc2e8af92016-04-05 17:15:19 +0100760 irreducible_ = true;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100761 contains_irreducible_loop_ = true;
David Brazdilc2e8af92016-04-05 17:15:19 +0100762 graph->SetHasIrreducibleLoops(true);
763 }
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800764 graph->SetHasLoops(true);
David Brazdila4b8c212015-05-07 09:59:30 +0100765}
766
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100767HBasicBlock* HLoopInformation::GetPreHeader() const {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000768 HBasicBlock* block = header_->GetPredecessors()[0];
769 DCHECK(irreducible_ || (block == header_->GetDominator()));
770 return block;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100771}
772
773bool HLoopInformation::Contains(const HBasicBlock& block) const {
774 return blocks_.IsBitSet(block.GetBlockId());
775}
776
777bool HLoopInformation::IsIn(const HLoopInformation& other) const {
778 return other.blocks_.IsBitSet(header_->GetBlockId());
779}
780
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800781bool HLoopInformation::IsDefinedOutOfTheLoop(HInstruction* instruction) const {
782 return !blocks_.IsBitSet(instruction->GetBlock()->GetBlockId());
Aart Bik73f1f3b2015-10-28 15:28:08 -0700783}
784
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100785size_t HLoopInformation::GetLifetimeEnd() const {
786 size_t last_position = 0;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100787 for (HBasicBlock* back_edge : GetBackEdges()) {
788 last_position = std::max(back_edge->GetLifetimeEnd(), last_position);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100789 }
790 return last_position;
791}
792
David Brazdil3f4a5222016-05-06 12:46:21 +0100793bool HLoopInformation::HasBackEdgeNotDominatedByHeader() const {
794 for (HBasicBlock* back_edge : GetBackEdges()) {
795 DCHECK(back_edge->GetDominator() != nullptr);
796 if (!header_->Dominates(back_edge)) {
797 return true;
798 }
799 }
800 return false;
801}
802
Anton Shaminf89381f2016-05-16 16:44:13 +0600803bool HLoopInformation::DominatesAllBackEdges(HBasicBlock* block) {
804 for (HBasicBlock* back_edge : GetBackEdges()) {
805 if (!block->Dominates(back_edge)) {
806 return false;
807 }
808 }
809 return true;
810}
811
David Sehrc757dec2016-11-04 15:48:34 -0700812
813bool HLoopInformation::HasExitEdge() const {
814 // Determine if this loop has at least one exit edge.
815 HBlocksInLoopReversePostOrderIterator it_loop(*this);
816 for (; !it_loop.Done(); it_loop.Advance()) {
817 for (HBasicBlock* successor : it_loop.Current()->GetSuccessors()) {
818 if (!Contains(*successor)) {
819 return true;
820 }
821 }
822 }
823 return false;
824}
825
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100826bool HBasicBlock::Dominates(HBasicBlock* other) const {
827 // Walk up the dominator tree from `other`, to find out if `this`
828 // is an ancestor.
829 HBasicBlock* current = other;
830 while (current != nullptr) {
831 if (current == this) {
832 return true;
833 }
834 current = current->GetDominator();
835 }
836 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100837}
838
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100839static void UpdateInputsUsers(HInstruction* instruction) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100840 HInputsRef inputs = instruction->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100841 for (size_t i = 0; i < inputs.size(); ++i) {
842 inputs[i]->AddUseAt(instruction, i);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100843 }
844 // Environment should be created later.
845 DCHECK(!instruction->HasEnvironment());
846}
847
Artem Serovcced8ba2017-07-19 18:18:09 +0100848void HBasicBlock::ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement) {
849 DCHECK(initial->GetBlock() == this);
850 InsertPhiAfter(replacement, initial);
851 initial->ReplaceWith(replacement);
852 RemovePhi(initial);
853}
854
Roland Levillainccc07a92014-09-16 14:48:16 +0100855void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
856 HInstruction* replacement) {
857 DCHECK(initial->GetBlock() == this);
Mark Mendell805b3b52015-09-18 14:10:29 -0400858 if (initial->IsControlFlow()) {
859 // We can only replace a control flow instruction with another control flow instruction.
860 DCHECK(replacement->IsControlFlow());
861 DCHECK_EQ(replacement->GetId(), -1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100862 DCHECK_EQ(replacement->GetType(), DataType::Type::kVoid);
Mark Mendell805b3b52015-09-18 14:10:29 -0400863 DCHECK_EQ(initial->GetBlock(), this);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100864 DCHECK_EQ(initial->GetType(), DataType::Type::kVoid);
Vladimir Marko46817b82016-03-29 12:21:58 +0100865 DCHECK(initial->GetUses().empty());
866 DCHECK(initial->GetEnvUses().empty());
Mark Mendell805b3b52015-09-18 14:10:29 -0400867 replacement->SetBlock(this);
868 replacement->SetId(GetGraph()->GetNextInstructionId());
869 instructions_.InsertInstructionBefore(replacement, initial);
870 UpdateInputsUsers(replacement);
871 } else {
872 InsertInstructionBefore(replacement, initial);
873 initial->ReplaceWith(replacement);
874 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100875 RemoveInstruction(initial);
876}
877
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100878static void Add(HInstructionList* instruction_list,
879 HBasicBlock* block,
880 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000881 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000882 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100883 instruction->SetBlock(block);
884 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100885 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100886 instruction_list->AddInstruction(instruction);
887}
888
889void HBasicBlock::AddInstruction(HInstruction* instruction) {
890 Add(&instructions_, this, instruction);
891}
892
893void HBasicBlock::AddPhi(HPhi* phi) {
894 Add(&phis_, this, phi);
895}
896
David Brazdilc3d743f2015-04-22 13:40:50 +0100897void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
898 DCHECK(!cursor->IsPhi());
899 DCHECK(!instruction->IsPhi());
900 DCHECK_EQ(instruction->GetId(), -1);
901 DCHECK_NE(cursor->GetId(), -1);
902 DCHECK_EQ(cursor->GetBlock(), this);
903 DCHECK(!instruction->IsControlFlow());
904 instruction->SetBlock(this);
905 instruction->SetId(GetGraph()->GetNextInstructionId());
906 UpdateInputsUsers(instruction);
907 instructions_.InsertInstructionBefore(instruction, cursor);
908}
909
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100910void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
911 DCHECK(!cursor->IsPhi());
912 DCHECK(!instruction->IsPhi());
913 DCHECK_EQ(instruction->GetId(), -1);
914 DCHECK_NE(cursor->GetId(), -1);
915 DCHECK_EQ(cursor->GetBlock(), this);
916 DCHECK(!instruction->IsControlFlow());
917 DCHECK(!cursor->IsControlFlow());
918 instruction->SetBlock(this);
919 instruction->SetId(GetGraph()->GetNextInstructionId());
920 UpdateInputsUsers(instruction);
921 instructions_.InsertInstructionAfter(instruction, cursor);
922}
923
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100924void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
925 DCHECK_EQ(phi->GetId(), -1);
926 DCHECK_NE(cursor->GetId(), -1);
927 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100928 phi->SetBlock(this);
929 phi->SetId(GetGraph()->GetNextInstructionId());
930 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100931 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100932}
933
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100934static void Remove(HInstructionList* instruction_list,
935 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000936 HInstruction* instruction,
937 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100938 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100939 instruction->SetBlock(nullptr);
940 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000941 if (ensure_safety) {
Vladimir Marko46817b82016-03-29 12:21:58 +0100942 DCHECK(instruction->GetUses().empty());
943 DCHECK(instruction->GetEnvUses().empty());
David Brazdil1abb4192015-02-17 18:33:36 +0000944 RemoveAsUser(instruction);
945 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100946}
947
David Brazdil1abb4192015-02-17 18:33:36 +0000948void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100949 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000950 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100951}
952
David Brazdil1abb4192015-02-17 18:33:36 +0000953void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
954 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100955}
956
David Brazdilc7508e92015-04-27 13:28:57 +0100957void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
958 if (instruction->IsPhi()) {
959 RemovePhi(instruction->AsPhi(), ensure_safety);
960 } else {
961 RemoveInstruction(instruction, ensure_safety);
962 }
963}
964
Vladimir Marko69d310e2017-10-09 14:12:23 +0100965void HEnvironment::CopyFrom(ArrayRef<HInstruction* const> locals) {
Vladimir Marko71bf8092015-09-15 15:33:14 +0100966 for (size_t i = 0; i < locals.size(); i++) {
967 HInstruction* instruction = locals[i];
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +0100968 SetRawEnvAt(i, instruction);
969 if (instruction != nullptr) {
970 instruction->AddEnvUseAt(this, i);
971 }
972 }
973}
974
David Brazdiled596192015-01-23 10:39:45 +0000975void HEnvironment::CopyFrom(HEnvironment* env) {
976 for (size_t i = 0; i < env->Size(); i++) {
977 HInstruction* instruction = env->GetInstructionAt(i);
978 SetRawEnvAt(i, instruction);
979 if (instruction != nullptr) {
980 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100981 }
David Brazdiled596192015-01-23 10:39:45 +0000982 }
983}
984
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700985void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
986 HBasicBlock* loop_header) {
987 DCHECK(loop_header->IsLoopHeader());
988 for (size_t i = 0; i < env->Size(); i++) {
989 HInstruction* instruction = env->GetInstructionAt(i);
990 SetRawEnvAt(i, instruction);
991 if (instruction == nullptr) {
992 continue;
993 }
994 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
995 // At the end of the loop pre-header, the corresponding value for instruction
996 // is the first input of the phi.
997 HInstruction* initial = instruction->AsPhi()->InputAt(0);
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700998 SetRawEnvAt(i, initial);
999 initial->AddEnvUseAt(this, i);
1000 } else {
1001 instruction->AddEnvUseAt(this, i);
1002 }
1003 }
1004}
1005
David Brazdil1abb4192015-02-17 18:33:36 +00001006void HEnvironment::RemoveAsUserOfInput(size_t index) const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001007 const HUserRecord<HEnvironment*>& env_use = vregs_[index];
1008 HInstruction* user = env_use.GetInstruction();
1009 auto before_env_use_node = env_use.GetBeforeUseNode();
1010 user->env_uses_.erase_after(before_env_use_node);
1011 user->FixUpUserRecordsAfterEnvUseRemoval(before_env_use_node);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001012}
1013
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001014HInstruction::InstructionKind HInstruction::GetKind() const {
1015 return GetKindInternal();
1016}
1017
Calin Juravle77520bc2015-01-12 18:45:46 +00001018HInstruction* HInstruction::GetNextDisregardingMoves() const {
1019 HInstruction* next = GetNext();
1020 while (next != nullptr && next->IsParallelMove()) {
1021 next = next->GetNext();
1022 }
1023 return next;
1024}
1025
1026HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
1027 HInstruction* previous = GetPrevious();
1028 while (previous != nullptr && previous->IsParallelMove()) {
1029 previous = previous->GetPrevious();
1030 }
1031 return previous;
1032}
1033
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001034void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001035 if (first_instruction_ == nullptr) {
1036 DCHECK(last_instruction_ == nullptr);
1037 first_instruction_ = last_instruction_ = instruction;
1038 } else {
George Burgess IVa4b58ed2017-06-22 15:47:25 -07001039 DCHECK(last_instruction_ != nullptr);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001040 last_instruction_->next_ = instruction;
1041 instruction->previous_ = last_instruction_;
1042 last_instruction_ = instruction;
1043 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001044}
1045
David Brazdilc3d743f2015-04-22 13:40:50 +01001046void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
1047 DCHECK(Contains(cursor));
1048 if (cursor == first_instruction_) {
1049 cursor->previous_ = instruction;
1050 instruction->next_ = cursor;
1051 first_instruction_ = instruction;
1052 } else {
1053 instruction->previous_ = cursor->previous_;
1054 instruction->next_ = cursor;
1055 cursor->previous_ = instruction;
1056 instruction->previous_->next_ = instruction;
1057 }
1058}
1059
1060void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
1061 DCHECK(Contains(cursor));
1062 if (cursor == last_instruction_) {
1063 cursor->next_ = instruction;
1064 instruction->previous_ = cursor;
1065 last_instruction_ = instruction;
1066 } else {
1067 instruction->next_ = cursor->next_;
1068 instruction->previous_ = cursor;
1069 cursor->next_ = instruction;
1070 instruction->next_->previous_ = instruction;
1071 }
1072}
1073
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001074void HInstructionList::RemoveInstruction(HInstruction* instruction) {
1075 if (instruction->previous_ != nullptr) {
1076 instruction->previous_->next_ = instruction->next_;
1077 }
1078 if (instruction->next_ != nullptr) {
1079 instruction->next_->previous_ = instruction->previous_;
1080 }
1081 if (instruction == first_instruction_) {
1082 first_instruction_ = instruction->next_;
1083 }
1084 if (instruction == last_instruction_) {
1085 last_instruction_ = instruction->previous_;
1086 }
1087}
1088
Roland Levillain6b469232014-09-25 10:10:38 +01001089bool HInstructionList::Contains(HInstruction* instruction) const {
1090 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
1091 if (it.Current() == instruction) {
1092 return true;
1093 }
1094 }
1095 return false;
1096}
1097
Roland Levillainccc07a92014-09-16 14:48:16 +01001098bool HInstructionList::FoundBefore(const HInstruction* instruction1,
1099 const HInstruction* instruction2) const {
1100 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
1101 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
1102 if (it.Current() == instruction1) {
1103 return true;
1104 }
1105 if (it.Current() == instruction2) {
1106 return false;
1107 }
1108 }
1109 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
1110 return true;
1111}
1112
Roland Levillain6c82d402014-10-13 16:10:27 +01001113bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
1114 if (other_instruction == this) {
1115 // An instruction does not strictly dominate itself.
1116 return false;
1117 }
Roland Levillainccc07a92014-09-16 14:48:16 +01001118 HBasicBlock* block = GetBlock();
1119 HBasicBlock* other_block = other_instruction->GetBlock();
1120 if (block != other_block) {
1121 return GetBlock()->Dominates(other_instruction->GetBlock());
1122 } else {
1123 // If both instructions are in the same block, ensure this
1124 // instruction comes before `other_instruction`.
1125 if (IsPhi()) {
1126 if (!other_instruction->IsPhi()) {
1127 // Phis appear before non phi-instructions so this instruction
1128 // dominates `other_instruction`.
1129 return true;
1130 } else {
1131 // There is no order among phis.
1132 LOG(FATAL) << "There is no dominance between phis of a same block.";
1133 return false;
1134 }
1135 } else {
1136 // `this` is not a phi.
1137 if (other_instruction->IsPhi()) {
1138 // Phis appear before non phi-instructions so this instruction
1139 // does not dominate `other_instruction`.
1140 return false;
1141 } else {
1142 // Check whether this instruction comes before
1143 // `other_instruction` in the instruction list.
1144 return block->GetInstructions().FoundBefore(this, other_instruction);
1145 }
1146 }
1147 }
1148}
1149
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001150void HInstruction::RemoveEnvironment() {
1151 RemoveEnvironmentUses(this);
1152 environment_ = nullptr;
1153}
1154
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001155void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001156 DCHECK(other != nullptr);
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001157 // Note: fixup_end remains valid across splice_after().
1158 auto fixup_end = other->uses_.empty() ? other->uses_.begin() : ++other->uses_.begin();
1159 other->uses_.splice_after(other->uses_.before_begin(), uses_);
1160 other->FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001161
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001162 // Note: env_fixup_end remains valid across splice_after().
1163 auto env_fixup_end =
1164 other->env_uses_.empty() ? other->env_uses_.begin() : ++other->env_uses_.begin();
1165 other->env_uses_.splice_after(other->env_uses_.before_begin(), env_uses_);
1166 other->FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001167
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001168 DCHECK(uses_.empty());
1169 DCHECK(env_uses_.empty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001170}
1171
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00001172void HInstruction::ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement) {
1173 const HUseList<HInstruction*>& uses = GetUses();
1174 for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) {
1175 HInstruction* user = it->GetUser();
1176 size_t index = it->GetIndex();
1177 // Increment `it` now because `*it` may disappear thanks to user->ReplaceInput().
1178 ++it;
1179 if (dominator->StrictlyDominates(user)) {
1180 user->ReplaceInput(replacement, index);
1181 }
1182 }
1183}
1184
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001185void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001186 HUserRecord<HInstruction*> input_use = InputRecordAt(index);
Vladimir Markoc6b56272016-04-20 18:45:25 +01001187 if (input_use.GetInstruction() == replacement) {
1188 // Nothing to do.
1189 return;
1190 }
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001191 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001192 // Note: fixup_end remains valid across splice_after().
1193 auto fixup_end =
1194 replacement->uses_.empty() ? replacement->uses_.begin() : ++replacement->uses_.begin();
1195 replacement->uses_.splice_after(replacement->uses_.before_begin(),
1196 input_use.GetInstruction()->uses_,
1197 before_use_node);
1198 replacement->FixUpUserRecordsAfterUseInsertion(fixup_end);
1199 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001200}
1201
Nicolas Geoffray39468442014-09-02 15:17:15 +01001202size_t HInstruction::EnvironmentSize() const {
1203 return HasEnvironment() ? environment_->Size() : 0;
1204}
1205
Mingyao Yanga9dbe832016-12-15 12:02:53 -08001206void HVariableInputSizeInstruction::AddInput(HInstruction* input) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001207 DCHECK(input->GetBlock() != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001208 inputs_.push_back(HUserRecord<HInstruction*>(input));
1209 input->AddUseAt(this, inputs_.size() - 1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001210}
1211
Mingyao Yanga9dbe832016-12-15 12:02:53 -08001212void HVariableInputSizeInstruction::InsertInputAt(size_t index, HInstruction* input) {
1213 inputs_.insert(inputs_.begin() + index, HUserRecord<HInstruction*>(input));
1214 input->AddUseAt(this, index);
1215 // Update indexes in use nodes of inputs that have been pushed further back by the insert().
1216 for (size_t i = index + 1u, e = inputs_.size(); i < e; ++i) {
1217 DCHECK_EQ(inputs_[i].GetUseNode()->GetIndex(), i - 1u);
1218 inputs_[i].GetUseNode()->SetIndex(i);
1219 }
1220}
1221
1222void HVariableInputSizeInstruction::RemoveInputAt(size_t index) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001223 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001224 inputs_.erase(inputs_.begin() + index);
Vladimir Marko372f10e2016-05-17 16:30:10 +01001225 // Update indexes in use nodes of inputs that have been pulled forward by the erase().
1226 for (size_t i = index, e = inputs_.size(); i < e; ++i) {
1227 DCHECK_EQ(inputs_[i].GetUseNode()->GetIndex(), i + 1u);
1228 inputs_[i].GetUseNode()->SetIndex(i);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001229 }
David Brazdil2d7352b2015-04-20 14:52:42 +01001230}
1231
Igor Murashkind01745e2017-04-05 16:40:31 -07001232void HVariableInputSizeInstruction::RemoveAllInputs() {
1233 RemoveAsUserOfAllInputs();
1234 DCHECK(!HasNonEnvironmentUses());
1235
1236 inputs_.clear();
1237 DCHECK_EQ(0u, InputCount());
1238}
1239
Igor Murashkin6ef45672017-08-08 13:59:55 -07001240size_t HConstructorFence::RemoveConstructorFences(HInstruction* instruction) {
Igor Murashkind01745e2017-04-05 16:40:31 -07001241 DCHECK(instruction->GetBlock() != nullptr);
1242 // Removing constructor fences only makes sense for instructions with an object return type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001243 DCHECK_EQ(DataType::Type::kReference, instruction->GetType());
Igor Murashkind01745e2017-04-05 16:40:31 -07001244
Igor Murashkin6ef45672017-08-08 13:59:55 -07001245 // Return how many instructions were removed for statistic purposes.
1246 size_t remove_count = 0;
1247
Igor Murashkind01745e2017-04-05 16:40:31 -07001248 // Efficient implementation that simultaneously (in one pass):
1249 // * Scans the uses list for all constructor fences.
1250 // * Deletes that constructor fence from the uses list of `instruction`.
1251 // * Deletes `instruction` from the constructor fence's inputs.
1252 // * Deletes the constructor fence if it now has 0 inputs.
1253
1254 const HUseList<HInstruction*>& uses = instruction->GetUses();
1255 // Warning: Although this is "const", we might mutate the list when calling RemoveInputAt.
1256 for (auto it = uses.begin(), end = uses.end(); it != end; ) {
1257 const HUseListNode<HInstruction*>& use_node = *it;
1258 HInstruction* const use_instruction = use_node.GetUser();
1259
1260 // Advance the iterator immediately once we fetch the use_node.
1261 // Warning: If the input is removed, the current iterator becomes invalid.
1262 ++it;
1263
1264 if (use_instruction->IsConstructorFence()) {
1265 HConstructorFence* ctor_fence = use_instruction->AsConstructorFence();
1266 size_t input_index = use_node.GetIndex();
1267
1268 // Process the candidate instruction for removal
1269 // from the graph.
1270
1271 // Constructor fence instructions are never
1272 // used by other instructions.
1273 //
1274 // If we wanted to make this more generic, it
1275 // could be a runtime if statement.
1276 DCHECK(!ctor_fence->HasUses());
1277
1278 // A constructor fence's return type is "kPrimVoid"
1279 // and therefore it can't have any environment uses.
1280 DCHECK(!ctor_fence->HasEnvironmentUses());
1281
1282 // Remove the inputs first, otherwise removing the instruction
1283 // will try to remove its uses while we are already removing uses
1284 // and this operation will fail.
1285 DCHECK_EQ(instruction, ctor_fence->InputAt(input_index));
1286
1287 // Removing the input will also remove the `use_node`.
1288 // (Do not look at `use_node` after this, it will be a dangling reference).
1289 ctor_fence->RemoveInputAt(input_index);
1290
1291 // Once all inputs are removed, the fence is considered dead and
1292 // is removed.
1293 if (ctor_fence->InputCount() == 0u) {
1294 ctor_fence->GetBlock()->RemoveInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001295 ++remove_count;
Igor Murashkind01745e2017-04-05 16:40:31 -07001296 }
1297 }
1298 }
1299
1300 if (kIsDebugBuild) {
1301 // Post-condition checks:
1302 // * None of the uses of `instruction` are a constructor fence.
1303 // * The `instruction` itself did not get removed from a block.
1304 for (const HUseListNode<HInstruction*>& use_node : instruction->GetUses()) {
1305 CHECK(!use_node.GetUser()->IsConstructorFence());
1306 }
1307 CHECK(instruction->GetBlock() != nullptr);
1308 }
Igor Murashkin6ef45672017-08-08 13:59:55 -07001309
1310 return remove_count;
Igor Murashkind01745e2017-04-05 16:40:31 -07001311}
1312
Igor Murashkindd018df2017-08-09 10:38:31 -07001313void HConstructorFence::Merge(HConstructorFence* other) {
1314 // Do not delete yourself from the graph.
1315 DCHECK(this != other);
1316 // Don't try to merge with an instruction not associated with a block.
1317 DCHECK(other->GetBlock() != nullptr);
1318 // A constructor fence's return type is "kPrimVoid"
1319 // and therefore it cannot have any environment uses.
1320 DCHECK(!other->HasEnvironmentUses());
1321
1322 auto has_input = [](HInstruction* haystack, HInstruction* needle) {
1323 // Check if `haystack` has `needle` as any of its inputs.
1324 for (size_t input_count = 0; input_count < haystack->InputCount(); ++input_count) {
1325 if (haystack->InputAt(input_count) == needle) {
1326 return true;
1327 }
1328 }
1329 return false;
1330 };
1331
1332 // Add any inputs from `other` into `this` if it wasn't already an input.
1333 for (size_t input_count = 0; input_count < other->InputCount(); ++input_count) {
1334 HInstruction* other_input = other->InputAt(input_count);
1335 if (!has_input(this, other_input)) {
1336 AddInput(other_input);
1337 }
1338 }
1339
1340 other->GetBlock()->RemoveInstruction(other);
1341}
1342
1343HInstruction* HConstructorFence::GetAssociatedAllocation(bool ignore_inputs) {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001344 HInstruction* new_instance_inst = GetPrevious();
1345 // Check if the immediately preceding instruction is a new-instance/new-array.
1346 // Otherwise this fence is for protecting final fields.
1347 if (new_instance_inst != nullptr &&
1348 (new_instance_inst->IsNewInstance() || new_instance_inst->IsNewArray())) {
Igor Murashkindd018df2017-08-09 10:38:31 -07001349 if (ignore_inputs) {
1350 // If inputs are ignored, simply check if the predecessor is
1351 // *any* HNewInstance/HNewArray.
1352 //
1353 // Inputs are normally only ignored for prepare_for_register_allocation,
1354 // at which point *any* prior HNewInstance/Array can be considered
1355 // associated.
1356 return new_instance_inst;
1357 } else {
1358 // Normal case: There must be exactly 1 input and the previous instruction
1359 // must be that input.
1360 if (InputCount() == 1u && InputAt(0) == new_instance_inst) {
1361 return new_instance_inst;
1362 }
1363 }
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001364 }
Igor Murashkindd018df2017-08-09 10:38:31 -07001365 return nullptr;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001366}
1367
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001368#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001369void H##name::Accept(HGraphVisitor* visitor) { \
1370 visitor->Visit##name(this); \
1371}
1372
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001373FOR_EACH_CONCRETE_INSTRUCTION(DEFINE_ACCEPT)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001374
1375#undef DEFINE_ACCEPT
1376
1377void HGraphVisitor::VisitInsertionOrder() {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001378 const ArenaVector<HBasicBlock*>& blocks = graph_->GetBlocks();
1379 for (HBasicBlock* block : blocks) {
David Brazdil46e2a392015-03-16 17:31:52 +00001380 if (block != nullptr) {
1381 VisitBasicBlock(block);
1382 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001383 }
1384}
1385
Roland Levillain633021e2014-10-01 14:12:25 +01001386void HGraphVisitor::VisitReversePostOrder() {
Vladimir Marko2c45bc92016-10-25 16:54:12 +01001387 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
1388 VisitBasicBlock(block);
Roland Levillain633021e2014-10-01 14:12:25 +01001389 }
1390}
1391
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001392void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001393 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001394 it.Current()->Accept(this);
1395 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001396 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001397 it.Current()->Accept(this);
1398 }
1399}
1400
Mark Mendelle82549b2015-05-06 10:55:34 -04001401HConstant* HTypeConversion::TryStaticEvaluation() const {
1402 HGraph* graph = GetBlock()->GetGraph();
1403 if (GetInput()->IsIntConstant()) {
1404 int32_t value = GetInput()->AsIntConstant()->GetValue();
1405 switch (GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001406 case DataType::Type::kInt64:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001407 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001408 case DataType::Type::kFloat32:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001409 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001410 case DataType::Type::kFloat64:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001411 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001412 default:
1413 return nullptr;
1414 }
1415 } else if (GetInput()->IsLongConstant()) {
1416 int64_t value = GetInput()->AsLongConstant()->GetValue();
1417 switch (GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001418 case DataType::Type::kInt32:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001419 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001420 case DataType::Type::kFloat32:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001421 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001422 case DataType::Type::kFloat64:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001423 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001424 default:
1425 return nullptr;
1426 }
1427 } else if (GetInput()->IsFloatConstant()) {
1428 float value = GetInput()->AsFloatConstant()->GetValue();
1429 switch (GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001430 case DataType::Type::kInt32:
Mark Mendelle82549b2015-05-06 10:55:34 -04001431 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001432 return graph->GetIntConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001433 if (value >= kPrimIntMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001434 return graph->GetIntConstant(kPrimIntMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001435 if (value <= kPrimIntMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001436 return graph->GetIntConstant(kPrimIntMin, GetDexPc());
1437 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001438 case DataType::Type::kInt64:
Mark Mendelle82549b2015-05-06 10:55:34 -04001439 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001440 return graph->GetLongConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001441 if (value >= kPrimLongMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001442 return graph->GetLongConstant(kPrimLongMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001443 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001444 return graph->GetLongConstant(kPrimLongMin, GetDexPc());
1445 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001446 case DataType::Type::kFloat64:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001447 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001448 default:
1449 return nullptr;
1450 }
1451 } else if (GetInput()->IsDoubleConstant()) {
1452 double value = GetInput()->AsDoubleConstant()->GetValue();
1453 switch (GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001454 case DataType::Type::kInt32:
Mark Mendelle82549b2015-05-06 10:55:34 -04001455 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001456 return graph->GetIntConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001457 if (value >= kPrimIntMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001458 return graph->GetIntConstant(kPrimIntMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001459 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001460 return graph->GetIntConstant(kPrimIntMin, GetDexPc());
1461 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001462 case DataType::Type::kInt64:
Mark Mendelle82549b2015-05-06 10:55:34 -04001463 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001464 return graph->GetLongConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001465 if (value >= kPrimLongMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001466 return graph->GetLongConstant(kPrimLongMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001467 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001468 return graph->GetLongConstant(kPrimLongMin, GetDexPc());
1469 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001470 case DataType::Type::kFloat32:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001471 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001472 default:
1473 return nullptr;
1474 }
1475 }
1476 return nullptr;
1477}
1478
Roland Levillain9240d6a2014-10-20 16:47:04 +01001479HConstant* HUnaryOperation::TryStaticEvaluation() const {
1480 if (GetInput()->IsIntConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001481 return Evaluate(GetInput()->AsIntConstant());
Roland Levillain9240d6a2014-10-20 16:47:04 +01001482 } else if (GetInput()->IsLongConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001483 return Evaluate(GetInput()->AsLongConstant());
Roland Levillain31dd3d62016-02-16 12:21:02 +00001484 } else if (kEnableFloatingPointStaticEvaluation) {
1485 if (GetInput()->IsFloatConstant()) {
1486 return Evaluate(GetInput()->AsFloatConstant());
1487 } else if (GetInput()->IsDoubleConstant()) {
1488 return Evaluate(GetInput()->AsDoubleConstant());
1489 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01001490 }
1491 return nullptr;
1492}
1493
1494HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillaine53bd812016-02-24 14:54:18 +00001495 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
1496 return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsIntConstant());
Roland Levillain9867bc72015-08-05 10:21:34 +01001497 } else if (GetLeft()->IsLongConstant()) {
1498 if (GetRight()->IsIntConstant()) {
Roland Levillaine53bd812016-02-24 14:54:18 +00001499 // The binop(long, int) case is only valid for shifts and rotations.
1500 DCHECK(IsShl() || IsShr() || IsUShr() || IsRor()) << DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01001501 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsIntConstant());
1502 } else if (GetRight()->IsLongConstant()) {
1503 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsLongConstant());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +00001504 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00001505 } else if (GetLeft()->IsNullConstant() && GetRight()->IsNullConstant()) {
Roland Levillaine53bd812016-02-24 14:54:18 +00001506 // The binop(null, null) case is only valid for equal and not-equal conditions.
1507 DCHECK(IsEqual() || IsNotEqual()) << DebugName();
Vladimir Marko9e23df52015-11-10 17:14:35 +00001508 return Evaluate(GetLeft()->AsNullConstant(), GetRight()->AsNullConstant());
Roland Levillain31dd3d62016-02-16 12:21:02 +00001509 } else if (kEnableFloatingPointStaticEvaluation) {
1510 if (GetLeft()->IsFloatConstant() && GetRight()->IsFloatConstant()) {
1511 return Evaluate(GetLeft()->AsFloatConstant(), GetRight()->AsFloatConstant());
1512 } else if (GetLeft()->IsDoubleConstant() && GetRight()->IsDoubleConstant()) {
1513 return Evaluate(GetLeft()->AsDoubleConstant(), GetRight()->AsDoubleConstant());
1514 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001515 }
1516 return nullptr;
1517}
Dave Allison20dfc792014-06-16 20:44:29 -07001518
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001519HConstant* HBinaryOperation::GetConstantRight() const {
1520 if (GetRight()->IsConstant()) {
1521 return GetRight()->AsConstant();
1522 } else if (IsCommutative() && GetLeft()->IsConstant()) {
1523 return GetLeft()->AsConstant();
1524 } else {
1525 return nullptr;
1526 }
1527}
1528
1529// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001530// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001531HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
1532 HInstruction* most_constant_right = GetConstantRight();
1533 if (most_constant_right == nullptr) {
1534 return nullptr;
1535 } else if (most_constant_right == GetLeft()) {
1536 return GetRight();
1537 } else {
1538 return GetLeft();
1539 }
1540}
1541
Roland Levillain31dd3d62016-02-16 12:21:02 +00001542std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs) {
1543 switch (rhs) {
1544 case ComparisonBias::kNoBias:
1545 return os << "no_bias";
1546 case ComparisonBias::kGtBias:
1547 return os << "gt_bias";
1548 case ComparisonBias::kLtBias:
1549 return os << "lt_bias";
1550 default:
1551 LOG(FATAL) << "Unknown ComparisonBias: " << static_cast<int>(rhs);
1552 UNREACHABLE();
1553 }
1554}
1555
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001556bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
1557 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001558}
1559
Vladimir Marko372f10e2016-05-17 16:30:10 +01001560bool HInstruction::Equals(const HInstruction* other) const {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001561 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001562 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001563 if (!InstructionDataEquals(other)) return false;
1564 if (GetType() != other->GetType()) return false;
Vladimir Markoe9004912016-06-16 16:50:52 +01001565 HConstInputsRef inputs = GetInputs();
1566 HConstInputsRef other_inputs = other->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +01001567 if (inputs.size() != other_inputs.size()) return false;
1568 for (size_t i = 0; i != inputs.size(); ++i) {
1569 if (inputs[i] != other_inputs[i]) return false;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001570 }
Vladimir Marko372f10e2016-05-17 16:30:10 +01001571
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001572 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001573 return true;
1574}
1575
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001576std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
1577#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
1578 switch (rhs) {
1579 FOR_EACH_INSTRUCTION(DECLARE_CASE)
1580 default:
1581 os << "Unknown instruction kind " << static_cast<int>(rhs);
1582 break;
1583 }
1584#undef DECLARE_CASE
1585 return os;
1586}
1587
Alexandre Rames22aa54b2016-10-18 09:32:29 +01001588void HInstruction::MoveBefore(HInstruction* cursor, bool do_checks) {
1589 if (do_checks) {
1590 DCHECK(!IsPhi());
1591 DCHECK(!IsControlFlow());
1592 DCHECK(CanBeMoved() ||
1593 // HShouldDeoptimizeFlag can only be moved by CHAGuardOptimization.
1594 IsShouldDeoptimizeFlag());
1595 DCHECK(!cursor->IsPhi());
1596 }
David Brazdild6c205e2016-06-07 14:20:52 +01001597
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001598 next_->previous_ = previous_;
1599 if (previous_ != nullptr) {
1600 previous_->next_ = next_;
1601 }
1602 if (block_->instructions_.first_instruction_ == this) {
1603 block_->instructions_.first_instruction_ = next_;
1604 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001605 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001606
1607 previous_ = cursor->previous_;
1608 if (previous_ != nullptr) {
1609 previous_->next_ = this;
1610 }
1611 next_ = cursor;
1612 cursor->previous_ = this;
1613 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001614
1615 if (block_->instructions_.first_instruction_ == cursor) {
1616 block_->instructions_.first_instruction_ = this;
1617 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001618}
1619
Vladimir Markofb337ea2015-11-25 15:25:10 +00001620void HInstruction::MoveBeforeFirstUserAndOutOfLoops() {
1621 DCHECK(!CanThrow());
1622 DCHECK(!HasSideEffects());
1623 DCHECK(!HasEnvironmentUses());
1624 DCHECK(HasNonEnvironmentUses());
1625 DCHECK(!IsPhi()); // Makes no sense for Phi.
1626 DCHECK_EQ(InputCount(), 0u);
1627
1628 // Find the target block.
Vladimir Marko46817b82016-03-29 12:21:58 +01001629 auto uses_it = GetUses().begin();
1630 auto uses_end = GetUses().end();
1631 HBasicBlock* target_block = uses_it->GetUser()->GetBlock();
1632 ++uses_it;
1633 while (uses_it != uses_end && uses_it->GetUser()->GetBlock() == target_block) {
1634 ++uses_it;
Vladimir Markofb337ea2015-11-25 15:25:10 +00001635 }
Vladimir Marko46817b82016-03-29 12:21:58 +01001636 if (uses_it != uses_end) {
Vladimir Markofb337ea2015-11-25 15:25:10 +00001637 // This instruction has uses in two or more blocks. Find the common dominator.
1638 CommonDominator finder(target_block);
Vladimir Marko46817b82016-03-29 12:21:58 +01001639 for (; uses_it != uses_end; ++uses_it) {
1640 finder.Update(uses_it->GetUser()->GetBlock());
Vladimir Markofb337ea2015-11-25 15:25:10 +00001641 }
1642 target_block = finder.Get();
1643 DCHECK(target_block != nullptr);
1644 }
1645 // Move to the first dominator not in a loop.
1646 while (target_block->IsInLoop()) {
1647 target_block = target_block->GetDominator();
1648 DCHECK(target_block != nullptr);
1649 }
1650
1651 // Find insertion position.
1652 HInstruction* insert_pos = nullptr;
Vladimir Marko46817b82016-03-29 12:21:58 +01001653 for (const HUseListNode<HInstruction*>& use : GetUses()) {
1654 if (use.GetUser()->GetBlock() == target_block &&
1655 (insert_pos == nullptr || use.GetUser()->StrictlyDominates(insert_pos))) {
1656 insert_pos = use.GetUser();
Vladimir Markofb337ea2015-11-25 15:25:10 +00001657 }
1658 }
1659 if (insert_pos == nullptr) {
1660 // No user in `target_block`, insert before the control flow instruction.
1661 insert_pos = target_block->GetLastInstruction();
1662 DCHECK(insert_pos->IsControlFlow());
1663 // Avoid splitting HCondition from HIf to prevent unnecessary materialization.
1664 if (insert_pos->IsIf()) {
1665 HInstruction* if_input = insert_pos->AsIf()->InputAt(0);
1666 if (if_input == insert_pos->GetPrevious()) {
1667 insert_pos = if_input;
1668 }
1669 }
1670 }
1671 MoveBefore(insert_pos);
1672}
1673
David Brazdilfc6a86a2015-06-26 10:33:45 +00001674HBasicBlock* HBasicBlock::SplitBefore(HInstruction* cursor) {
David Brazdil9bc43612015-11-05 21:25:24 +00001675 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented.";
David Brazdilfc6a86a2015-06-26 10:33:45 +00001676 DCHECK_EQ(cursor->GetBlock(), this);
1677
Vladimir Markoca6fff82017-10-03 14:49:14 +01001678 HBasicBlock* new_block =
1679 new (GetGraph()->GetAllocator()) HBasicBlock(GetGraph(), cursor->GetDexPc());
David Brazdilfc6a86a2015-06-26 10:33:45 +00001680 new_block->instructions_.first_instruction_ = cursor;
1681 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1682 instructions_.last_instruction_ = cursor->previous_;
1683 if (cursor->previous_ == nullptr) {
1684 instructions_.first_instruction_ = nullptr;
1685 } else {
1686 cursor->previous_->next_ = nullptr;
1687 cursor->previous_ = nullptr;
1688 }
1689
1690 new_block->instructions_.SetBlockOfInstructions(new_block);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001691 AddInstruction(new (GetGraph()->GetAllocator()) HGoto(new_block->GetDexPc()));
David Brazdilfc6a86a2015-06-26 10:33:45 +00001692
Vladimir Marko60584552015-09-03 13:35:12 +00001693 for (HBasicBlock* successor : GetSuccessors()) {
Vladimir Marko60584552015-09-03 13:35:12 +00001694 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001695 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001696 new_block->successors_.swap(successors_);
1697 DCHECK(successors_.empty());
David Brazdilfc6a86a2015-06-26 10:33:45 +00001698 AddSuccessor(new_block);
1699
David Brazdil56e1acc2015-06-30 15:41:36 +01001700 GetGraph()->AddBlock(new_block);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001701 return new_block;
1702}
1703
David Brazdild7558da2015-09-22 13:04:14 +01001704HBasicBlock* HBasicBlock::CreateImmediateDominator() {
David Brazdil9bc43612015-11-05 21:25:24 +00001705 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented.";
David Brazdild7558da2015-09-22 13:04:14 +01001706 DCHECK(!IsCatchBlock()) << "Support for updating try/catch information not implemented.";
1707
Vladimir Markoca6fff82017-10-03 14:49:14 +01001708 HBasicBlock* new_block = new (GetGraph()->GetAllocator()) HBasicBlock(GetGraph(), GetDexPc());
David Brazdild7558da2015-09-22 13:04:14 +01001709
1710 for (HBasicBlock* predecessor : GetPredecessors()) {
David Brazdild7558da2015-09-22 13:04:14 +01001711 predecessor->successors_[predecessor->GetSuccessorIndexOf(this)] = new_block;
1712 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001713 new_block->predecessors_.swap(predecessors_);
1714 DCHECK(predecessors_.empty());
David Brazdild7558da2015-09-22 13:04:14 +01001715 AddPredecessor(new_block);
1716
1717 GetGraph()->AddBlock(new_block);
1718 return new_block;
1719}
1720
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001721HBasicBlock* HBasicBlock::SplitBeforeForInlining(HInstruction* cursor) {
1722 DCHECK_EQ(cursor->GetBlock(), this);
1723
Vladimir Markoca6fff82017-10-03 14:49:14 +01001724 HBasicBlock* new_block =
1725 new (GetGraph()->GetAllocator()) HBasicBlock(GetGraph(), cursor->GetDexPc());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001726 new_block->instructions_.first_instruction_ = cursor;
1727 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1728 instructions_.last_instruction_ = cursor->previous_;
1729 if (cursor->previous_ == nullptr) {
1730 instructions_.first_instruction_ = nullptr;
1731 } else {
1732 cursor->previous_->next_ = nullptr;
1733 cursor->previous_ = nullptr;
1734 }
1735
1736 new_block->instructions_.SetBlockOfInstructions(new_block);
1737
1738 for (HBasicBlock* successor : GetSuccessors()) {
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001739 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
1740 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001741 new_block->successors_.swap(successors_);
1742 DCHECK(successors_.empty());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001743
1744 for (HBasicBlock* dominated : GetDominatedBlocks()) {
1745 dominated->dominator_ = new_block;
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001746 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001747 new_block->dominated_blocks_.swap(dominated_blocks_);
1748 DCHECK(dominated_blocks_.empty());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001749 return new_block;
1750}
1751
1752HBasicBlock* HBasicBlock::SplitAfterForInlining(HInstruction* cursor) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001753 DCHECK(!cursor->IsControlFlow());
1754 DCHECK_NE(instructions_.last_instruction_, cursor);
1755 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001756
Vladimir Markoca6fff82017-10-03 14:49:14 +01001757 HBasicBlock* new_block = new (GetGraph()->GetAllocator()) HBasicBlock(GetGraph(), GetDexPc());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001758 new_block->instructions_.first_instruction_ = cursor->GetNext();
1759 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1760 cursor->next_->previous_ = nullptr;
1761 cursor->next_ = nullptr;
1762 instructions_.last_instruction_ = cursor;
1763
1764 new_block->instructions_.SetBlockOfInstructions(new_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001765 for (HBasicBlock* successor : GetSuccessors()) {
Vladimir Marko60584552015-09-03 13:35:12 +00001766 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001767 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001768 new_block->successors_.swap(successors_);
1769 DCHECK(successors_.empty());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001770
Vladimir Marko60584552015-09-03 13:35:12 +00001771 for (HBasicBlock* dominated : GetDominatedBlocks()) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001772 dominated->dominator_ = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001773 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001774 new_block->dominated_blocks_.swap(dominated_blocks_);
1775 DCHECK(dominated_blocks_.empty());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001776 return new_block;
1777}
1778
David Brazdilec16f792015-08-19 15:04:01 +01001779const HTryBoundary* HBasicBlock::ComputeTryEntryOfSuccessors() const {
David Brazdilffee3d32015-07-06 11:48:53 +01001780 if (EndsWithTryBoundary()) {
1781 HTryBoundary* try_boundary = GetLastInstruction()->AsTryBoundary();
1782 if (try_boundary->IsEntry()) {
David Brazdilec16f792015-08-19 15:04:01 +01001783 DCHECK(!IsTryBlock());
David Brazdilffee3d32015-07-06 11:48:53 +01001784 return try_boundary;
1785 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001786 DCHECK(IsTryBlock());
1787 DCHECK(try_catch_information_->GetTryEntry().HasSameExceptionHandlersAs(*try_boundary));
David Brazdilffee3d32015-07-06 11:48:53 +01001788 return nullptr;
1789 }
David Brazdilec16f792015-08-19 15:04:01 +01001790 } else if (IsTryBlock()) {
1791 return &try_catch_information_->GetTryEntry();
David Brazdilffee3d32015-07-06 11:48:53 +01001792 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001793 return nullptr;
David Brazdilffee3d32015-07-06 11:48:53 +01001794 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00001795}
1796
David Brazdild7558da2015-09-22 13:04:14 +01001797bool HBasicBlock::HasThrowingInstructions() const {
1798 for (HInstructionIterator it(GetInstructions()); !it.Done(); it.Advance()) {
1799 if (it.Current()->CanThrow()) {
1800 return true;
1801 }
1802 }
1803 return false;
1804}
1805
David Brazdilfc6a86a2015-06-26 10:33:45 +00001806static bool HasOnlyOneInstruction(const HBasicBlock& block) {
1807 return block.GetPhis().IsEmpty()
1808 && !block.GetInstructions().IsEmpty()
1809 && block.GetFirstInstruction() == block.GetLastInstruction();
1810}
1811
David Brazdil46e2a392015-03-16 17:31:52 +00001812bool HBasicBlock::IsSingleGoto() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001813 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsGoto();
1814}
1815
Mads Ager16e52892017-07-14 13:11:37 +02001816bool HBasicBlock::IsSingleReturn() const {
1817 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsReturn();
1818}
1819
Mingyao Yang46721ef2017-10-05 14:45:17 -07001820bool HBasicBlock::IsSingleReturnOrReturnVoidAllowingPhis() const {
1821 return (GetFirstInstruction() == GetLastInstruction()) &&
1822 (GetLastInstruction()->IsReturn() || GetLastInstruction()->IsReturnVoid());
1823}
1824
David Brazdilfc6a86a2015-06-26 10:33:45 +00001825bool HBasicBlock::IsSingleTryBoundary() const {
1826 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsTryBoundary();
David Brazdil46e2a392015-03-16 17:31:52 +00001827}
1828
David Brazdil8d5b8b22015-03-24 10:51:52 +00001829bool HBasicBlock::EndsWithControlFlowInstruction() const {
1830 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
1831}
1832
David Brazdilb2bd1c52015-03-25 11:17:37 +00001833bool HBasicBlock::EndsWithIf() const {
1834 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
1835}
1836
David Brazdilffee3d32015-07-06 11:48:53 +01001837bool HBasicBlock::EndsWithTryBoundary() const {
1838 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsTryBoundary();
1839}
1840
David Brazdilb2bd1c52015-03-25 11:17:37 +00001841bool HBasicBlock::HasSinglePhi() const {
1842 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
1843}
1844
David Brazdild26a4112015-11-10 11:07:31 +00001845ArrayRef<HBasicBlock* const> HBasicBlock::GetNormalSuccessors() const {
1846 if (EndsWithTryBoundary()) {
1847 // The normal-flow successor of HTryBoundary is always stored at index zero.
1848 DCHECK_EQ(successors_[0], GetLastInstruction()->AsTryBoundary()->GetNormalFlowSuccessor());
1849 return ArrayRef<HBasicBlock* const>(successors_).SubArray(0u, 1u);
1850 } else {
1851 // All successors of blocks not ending with TryBoundary are normal.
1852 return ArrayRef<HBasicBlock* const>(successors_);
1853 }
1854}
1855
1856ArrayRef<HBasicBlock* const> HBasicBlock::GetExceptionalSuccessors() const {
1857 if (EndsWithTryBoundary()) {
1858 return GetLastInstruction()->AsTryBoundary()->GetExceptionHandlers();
1859 } else {
1860 // Blocks not ending with TryBoundary do not have exceptional successors.
1861 return ArrayRef<HBasicBlock* const>();
1862 }
1863}
1864
David Brazdilffee3d32015-07-06 11:48:53 +01001865bool HTryBoundary::HasSameExceptionHandlersAs(const HTryBoundary& other) const {
David Brazdild26a4112015-11-10 11:07:31 +00001866 ArrayRef<HBasicBlock* const> handlers1 = GetExceptionHandlers();
1867 ArrayRef<HBasicBlock* const> handlers2 = other.GetExceptionHandlers();
1868
1869 size_t length = handlers1.size();
1870 if (length != handlers2.size()) {
David Brazdilffee3d32015-07-06 11:48:53 +01001871 return false;
1872 }
1873
David Brazdilb618ade2015-07-29 10:31:29 +01001874 // Exception handlers need to be stored in the same order.
David Brazdild26a4112015-11-10 11:07:31 +00001875 for (size_t i = 0; i < length; ++i) {
1876 if (handlers1[i] != handlers2[i]) {
David Brazdilffee3d32015-07-06 11:48:53 +01001877 return false;
1878 }
1879 }
1880 return true;
1881}
1882
David Brazdil2d7352b2015-04-20 14:52:42 +01001883size_t HInstructionList::CountSize() const {
1884 size_t size = 0;
1885 HInstruction* current = first_instruction_;
1886 for (; current != nullptr; current = current->GetNext()) {
1887 size++;
1888 }
1889 return size;
1890}
1891
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001892void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
1893 for (HInstruction* current = first_instruction_;
1894 current != nullptr;
1895 current = current->GetNext()) {
1896 current->SetBlock(block);
1897 }
1898}
1899
1900void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
1901 DCHECK(Contains(cursor));
1902 if (!instruction_list.IsEmpty()) {
1903 if (cursor == last_instruction_) {
1904 last_instruction_ = instruction_list.last_instruction_;
1905 } else {
1906 cursor->next_->previous_ = instruction_list.last_instruction_;
1907 }
1908 instruction_list.last_instruction_->next_ = cursor->next_;
1909 cursor->next_ = instruction_list.first_instruction_;
1910 instruction_list.first_instruction_->previous_ = cursor;
1911 }
1912}
1913
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001914void HInstructionList::AddBefore(HInstruction* cursor, const HInstructionList& instruction_list) {
1915 DCHECK(Contains(cursor));
1916 if (!instruction_list.IsEmpty()) {
1917 if (cursor == first_instruction_) {
1918 first_instruction_ = instruction_list.first_instruction_;
1919 } else {
1920 cursor->previous_->next_ = instruction_list.first_instruction_;
1921 }
1922 instruction_list.last_instruction_->next_ = cursor;
1923 instruction_list.first_instruction_->previous_ = cursor->previous_;
1924 cursor->previous_ = instruction_list.last_instruction_;
1925 }
1926}
1927
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001928void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +00001929 if (IsEmpty()) {
1930 first_instruction_ = instruction_list.first_instruction_;
1931 last_instruction_ = instruction_list.last_instruction_;
1932 } else {
1933 AddAfter(last_instruction_, instruction_list);
1934 }
1935}
1936
David Brazdil04ff4e82015-12-10 13:54:52 +00001937// Should be called on instructions in a dead block in post order. This method
1938// assumes `insn` has been removed from all users with the exception of catch
1939// phis because of missing exceptional edges in the graph. It removes the
1940// instruction from catch phi uses, together with inputs of other catch phis in
1941// the catch block at the same index, as these must be dead too.
1942static void RemoveUsesOfDeadInstruction(HInstruction* insn) {
1943 DCHECK(!insn->HasEnvironmentUses());
1944 while (insn->HasNonEnvironmentUses()) {
Vladimir Marko46817b82016-03-29 12:21:58 +01001945 const HUseListNode<HInstruction*>& use = insn->GetUses().front();
1946 size_t use_index = use.GetIndex();
1947 HBasicBlock* user_block = use.GetUser()->GetBlock();
1948 DCHECK(use.GetUser()->IsPhi() && user_block->IsCatchBlock());
David Brazdil04ff4e82015-12-10 13:54:52 +00001949 for (HInstructionIterator phi_it(user_block->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1950 phi_it.Current()->AsPhi()->RemoveInputAt(use_index);
1951 }
1952 }
1953}
1954
David Brazdil2d7352b2015-04-20 14:52:42 +01001955void HBasicBlock::DisconnectAndDelete() {
1956 // Dominators must be removed after all the blocks they dominate. This way
1957 // a loop header is removed last, a requirement for correct loop information
1958 // iteration.
Vladimir Marko60584552015-09-03 13:35:12 +00001959 DCHECK(dominated_blocks_.empty());
David Brazdil46e2a392015-03-16 17:31:52 +00001960
David Brazdil9eeebf62016-03-24 11:18:15 +00001961 // The following steps gradually remove the block from all its dependants in
1962 // post order (b/27683071).
1963
1964 // (1) Store a basic block that we'll use in step (5) to find loops to be updated.
1965 // We need to do this before step (4) which destroys the predecessor list.
1966 HBasicBlock* loop_update_start = this;
1967 if (IsLoopHeader()) {
1968 HLoopInformation* loop_info = GetLoopInformation();
1969 // All other blocks in this loop should have been removed because the header
1970 // was their dominator.
1971 // Note that we do not remove `this` from `loop_info` as it is unreachable.
1972 DCHECK(!loop_info->IsIrreducible());
1973 DCHECK_EQ(loop_info->GetBlocks().NumSetBits(), 1u);
1974 DCHECK_EQ(static_cast<uint32_t>(loop_info->GetBlocks().GetHighestBitSet()), GetBlockId());
1975 loop_update_start = loop_info->GetPreHeader();
David Brazdil2d7352b2015-04-20 14:52:42 +01001976 }
1977
David Brazdil9eeebf62016-03-24 11:18:15 +00001978 // (2) Disconnect the block from its successors and update their phis.
1979 for (HBasicBlock* successor : successors_) {
1980 // Delete this block from the list of predecessors.
1981 size_t this_index = successor->GetPredecessorIndexOf(this);
1982 successor->predecessors_.erase(successor->predecessors_.begin() + this_index);
1983
1984 // Check that `successor` has other predecessors, otherwise `this` is the
1985 // dominator of `successor` which violates the order DCHECKed at the top.
1986 DCHECK(!successor->predecessors_.empty());
1987
1988 // Remove this block's entries in the successor's phis. Skip exceptional
1989 // successors because catch phi inputs do not correspond to predecessor
1990 // blocks but throwing instructions. The inputs of the catch phis will be
1991 // updated in step (3).
1992 if (!successor->IsCatchBlock()) {
1993 if (successor->predecessors_.size() == 1u) {
1994 // The successor has just one predecessor left. Replace phis with the only
1995 // remaining input.
1996 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1997 HPhi* phi = phi_it.Current()->AsPhi();
1998 phi->ReplaceWith(phi->InputAt(1 - this_index));
1999 successor->RemovePhi(phi);
2000 }
2001 } else {
2002 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
2003 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
2004 }
2005 }
2006 }
2007 }
2008 successors_.clear();
2009
2010 // (3) Remove instructions and phis. Instructions should have no remaining uses
2011 // except in catch phis. If an instruction is used by a catch phi at `index`,
2012 // remove `index`-th input of all phis in the catch block since they are
2013 // guaranteed dead. Note that we may miss dead inputs this way but the
2014 // graph will always remain consistent.
2015 for (HBackwardInstructionIterator it(GetInstructions()); !it.Done(); it.Advance()) {
2016 HInstruction* insn = it.Current();
2017 RemoveUsesOfDeadInstruction(insn);
2018 RemoveInstruction(insn);
2019 }
2020 for (HInstructionIterator it(GetPhis()); !it.Done(); it.Advance()) {
2021 HPhi* insn = it.Current()->AsPhi();
2022 RemoveUsesOfDeadInstruction(insn);
2023 RemovePhi(insn);
2024 }
2025
2026 // (4) Disconnect the block from its predecessors and update their
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002027 // control-flow instructions.
Vladimir Marko60584552015-09-03 13:35:12 +00002028 for (HBasicBlock* predecessor : predecessors_) {
David Brazdil9eeebf62016-03-24 11:18:15 +00002029 // We should not see any back edges as they would have been removed by step (3).
2030 DCHECK(!IsInLoop() || !GetLoopInformation()->IsBackEdge(*predecessor));
2031
David Brazdil2d7352b2015-04-20 14:52:42 +01002032 HInstruction* last_instruction = predecessor->GetLastInstruction();
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002033 if (last_instruction->IsTryBoundary() && !IsCatchBlock()) {
2034 // This block is the only normal-flow successor of the TryBoundary which
2035 // makes `predecessor` dead. Since DCE removes blocks in post order,
2036 // exception handlers of this TryBoundary were already visited and any
2037 // remaining handlers therefore must be live. We remove `predecessor` from
2038 // their list of predecessors.
2039 DCHECK_EQ(last_instruction->AsTryBoundary()->GetNormalFlowSuccessor(), this);
2040 while (predecessor->GetSuccessors().size() > 1) {
2041 HBasicBlock* handler = predecessor->GetSuccessors()[1];
2042 DCHECK(handler->IsCatchBlock());
2043 predecessor->RemoveSuccessor(handler);
2044 handler->RemovePredecessor(predecessor);
2045 }
2046 }
2047
David Brazdil2d7352b2015-04-20 14:52:42 +01002048 predecessor->RemoveSuccessor(this);
Mark Mendellfe57faa2015-09-18 09:26:15 -04002049 uint32_t num_pred_successors = predecessor->GetSuccessors().size();
2050 if (num_pred_successors == 1u) {
2051 // If we have one successor after removing one, then we must have
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002052 // had an HIf, HPackedSwitch or HTryBoundary, as they have more than one
2053 // successor. Replace those with a HGoto.
2054 DCHECK(last_instruction->IsIf() ||
2055 last_instruction->IsPackedSwitch() ||
2056 (last_instruction->IsTryBoundary() && IsCatchBlock()));
Mark Mendellfe57faa2015-09-18 09:26:15 -04002057 predecessor->RemoveInstruction(last_instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002058 predecessor->AddInstruction(new (graph_->GetAllocator()) HGoto(last_instruction->GetDexPc()));
Mark Mendellfe57faa2015-09-18 09:26:15 -04002059 } else if (num_pred_successors == 0u) {
David Brazdil2d7352b2015-04-20 14:52:42 +01002060 // The predecessor has no remaining successors and therefore must be dead.
2061 // We deliberately leave it without a control-flow instruction so that the
David Brazdilbadd8262016-02-02 16:28:56 +00002062 // GraphChecker fails unless it is not removed during the pass too.
Mark Mendellfe57faa2015-09-18 09:26:15 -04002063 predecessor->RemoveInstruction(last_instruction);
2064 } else {
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002065 // There are multiple successors left. The removed block might be a successor
2066 // of a PackedSwitch which will be completely removed (perhaps replaced with
2067 // a Goto), or we are deleting a catch block from a TryBoundary. In either
2068 // case, leave `last_instruction` as is for now.
2069 DCHECK(last_instruction->IsPackedSwitch() ||
2070 (last_instruction->IsTryBoundary() && IsCatchBlock()));
David Brazdil2d7352b2015-04-20 14:52:42 +01002071 }
David Brazdil46e2a392015-03-16 17:31:52 +00002072 }
Vladimir Marko60584552015-09-03 13:35:12 +00002073 predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01002074
David Brazdil9eeebf62016-03-24 11:18:15 +00002075 // (5) Remove the block from all loops it is included in. Skip the inner-most
2076 // loop if this is the loop header (see definition of `loop_update_start`)
2077 // because the loop header's predecessor list has been destroyed in step (4).
2078 for (HLoopInformationOutwardIterator it(*loop_update_start); !it.Done(); it.Advance()) {
2079 HLoopInformation* loop_info = it.Current();
2080 loop_info->Remove(this);
2081 if (loop_info->IsBackEdge(*this)) {
2082 // If this was the last back edge of the loop, we deliberately leave the
2083 // loop in an inconsistent state and will fail GraphChecker unless the
2084 // entire loop is removed during the pass.
2085 loop_info->RemoveBackEdge(this);
David Brazdil2d7352b2015-04-20 14:52:42 +01002086 }
2087 }
David Brazdil2d7352b2015-04-20 14:52:42 +01002088
David Brazdil9eeebf62016-03-24 11:18:15 +00002089 // (6) Disconnect from the dominator.
David Brazdil2d7352b2015-04-20 14:52:42 +01002090 dominator_->RemoveDominatedBlock(this);
2091 SetDominator(nullptr);
2092
David Brazdil9eeebf62016-03-24 11:18:15 +00002093 // (7) Delete from the graph, update reverse post order.
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002094 graph_->DeleteDeadEmptyBlock(this);
David Brazdil2d7352b2015-04-20 14:52:42 +01002095 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002096}
2097
Aart Bik6b69e0a2017-01-11 10:20:43 -08002098void HBasicBlock::MergeInstructionsWith(HBasicBlock* other) {
2099 DCHECK(EndsWithControlFlowInstruction());
2100 RemoveInstruction(GetLastInstruction());
2101 instructions_.Add(other->GetInstructions());
2102 other->instructions_.SetBlockOfInstructions(this);
2103 other->instructions_.Clear();
2104}
2105
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002106void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01002107 DCHECK_EQ(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00002108 DCHECK(ContainsElement(dominated_blocks_, other));
2109 DCHECK_EQ(GetSingleSuccessor(), other);
2110 DCHECK_EQ(other->GetSinglePredecessor(), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002111 DCHECK(other->GetPhis().IsEmpty());
2112
David Brazdil2d7352b2015-04-20 14:52:42 +01002113 // Move instructions from `other` to `this`.
Aart Bik6b69e0a2017-01-11 10:20:43 -08002114 MergeInstructionsWith(other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002115
David Brazdil2d7352b2015-04-20 14:52:42 +01002116 // Remove `other` from the loops it is included in.
2117 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
2118 HLoopInformation* loop_info = it.Current();
2119 loop_info->Remove(other);
2120 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002121 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01002122 }
2123 }
2124
2125 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00002126 successors_.clear();
Vladimir Marko661b69b2016-11-09 14:11:37 +00002127 for (HBasicBlock* successor : other->GetSuccessors()) {
2128 successor->predecessors_[successor->GetPredecessorIndexOf(other)] = this;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002129 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00002130 successors_.swap(other->successors_);
2131 DCHECK(other->successors_.empty());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002132
David Brazdil2d7352b2015-04-20 14:52:42 +01002133 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00002134 RemoveDominatedBlock(other);
2135 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
David Brazdil2d7352b2015-04-20 14:52:42 +01002136 dominated->SetDominator(this);
2137 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00002138 dominated_blocks_.insert(
2139 dominated_blocks_.end(), other->dominated_blocks_.begin(), other->dominated_blocks_.end());
Vladimir Marko60584552015-09-03 13:35:12 +00002140 other->dominated_blocks_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01002141 other->dominator_ = nullptr;
2142
2143 // Clear the list of predecessors of `other` in preparation of deleting it.
Vladimir Marko60584552015-09-03 13:35:12 +00002144 other->predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01002145
2146 // Delete `other` from the graph. The function updates reverse post order.
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002147 graph_->DeleteDeadEmptyBlock(other);
David Brazdil2d7352b2015-04-20 14:52:42 +01002148 other->SetGraph(nullptr);
2149}
2150
2151void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
2152 DCHECK_NE(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00002153 DCHECK(GetDominatedBlocks().empty());
2154 DCHECK(GetSuccessors().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01002155 DCHECK(!EndsWithControlFlowInstruction());
Vladimir Marko60584552015-09-03 13:35:12 +00002156 DCHECK(other->GetSinglePredecessor()->IsEntryBlock());
David Brazdil2d7352b2015-04-20 14:52:42 +01002157 DCHECK(other->GetPhis().IsEmpty());
2158 DCHECK(!other->IsInLoop());
2159
2160 // Move instructions from `other` to `this`.
2161 instructions_.Add(other->GetInstructions());
2162 other->instructions_.SetBlockOfInstructions(this);
2163
2164 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00002165 successors_.clear();
Vladimir Marko661b69b2016-11-09 14:11:37 +00002166 for (HBasicBlock* successor : other->GetSuccessors()) {
2167 successor->predecessors_[successor->GetPredecessorIndexOf(other)] = this;
David Brazdil2d7352b2015-04-20 14:52:42 +01002168 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00002169 successors_.swap(other->successors_);
2170 DCHECK(other->successors_.empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01002171
2172 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00002173 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002174 dominated->SetDominator(this);
2175 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00002176 dominated_blocks_.insert(
2177 dominated_blocks_.end(), other->dominated_blocks_.begin(), other->dominated_blocks_.end());
Vladimir Marko60584552015-09-03 13:35:12 +00002178 other->dominated_blocks_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002179 other->dominator_ = nullptr;
2180 other->graph_ = nullptr;
2181}
2182
2183void HBasicBlock::ReplaceWith(HBasicBlock* other) {
Vladimir Marko60584552015-09-03 13:35:12 +00002184 while (!GetPredecessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002185 HBasicBlock* predecessor = GetPredecessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002186 predecessor->ReplaceSuccessor(this, other);
2187 }
Vladimir Marko60584552015-09-03 13:35:12 +00002188 while (!GetSuccessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002189 HBasicBlock* successor = GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002190 successor->ReplacePredecessor(this, other);
2191 }
Vladimir Marko60584552015-09-03 13:35:12 +00002192 for (HBasicBlock* dominated : GetDominatedBlocks()) {
2193 other->AddDominatedBlock(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002194 }
2195 GetDominator()->ReplaceDominatedBlock(this, other);
2196 other->SetDominator(GetDominator());
2197 dominator_ = nullptr;
2198 graph_ = nullptr;
2199}
2200
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002201void HGraph::DeleteDeadEmptyBlock(HBasicBlock* block) {
David Brazdil2d7352b2015-04-20 14:52:42 +01002202 DCHECK_EQ(block->GetGraph(), this);
Vladimir Marko60584552015-09-03 13:35:12 +00002203 DCHECK(block->GetSuccessors().empty());
2204 DCHECK(block->GetPredecessors().empty());
2205 DCHECK(block->GetDominatedBlocks().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01002206 DCHECK(block->GetDominator() == nullptr);
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002207 DCHECK(block->GetInstructions().IsEmpty());
2208 DCHECK(block->GetPhis().IsEmpty());
David Brazdil2d7352b2015-04-20 14:52:42 +01002209
David Brazdilc7af85d2015-05-26 12:05:55 +01002210 if (block->IsExitBlock()) {
Serguei Katkov7ba99662016-03-02 16:25:36 +06002211 SetExitBlock(nullptr);
David Brazdilc7af85d2015-05-26 12:05:55 +01002212 }
2213
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002214 RemoveElement(reverse_post_order_, block);
2215 blocks_[block->GetBlockId()] = nullptr;
David Brazdil86ea7ee2016-02-16 09:26:07 +00002216 block->SetGraph(nullptr);
David Brazdil2d7352b2015-04-20 14:52:42 +01002217}
2218
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +00002219void HGraph::UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
2220 HBasicBlock* reference,
2221 bool replace_if_back_edge) {
2222 if (block->IsLoopHeader()) {
2223 // Clear the information of which blocks are contained in that loop. Since the
2224 // information is stored as a bit vector based on block ids, we have to update
2225 // it, as those block ids were specific to the callee graph and we are now adding
2226 // these blocks to the caller graph.
2227 block->GetLoopInformation()->ClearAllBlocks();
2228 }
2229
2230 // If not already in a loop, update the loop information.
2231 if (!block->IsInLoop()) {
2232 block->SetLoopInformation(reference->GetLoopInformation());
2233 }
2234
2235 // If the block is in a loop, update all its outward loops.
2236 HLoopInformation* loop_info = block->GetLoopInformation();
2237 if (loop_info != nullptr) {
2238 for (HLoopInformationOutwardIterator loop_it(*block);
2239 !loop_it.Done();
2240 loop_it.Advance()) {
2241 loop_it.Current()->Add(block);
2242 }
2243 if (replace_if_back_edge && loop_info->IsBackEdge(*reference)) {
2244 loop_info->ReplaceBackEdge(reference, block);
2245 }
2246 }
2247
2248 // Copy TryCatchInformation if `reference` is a try block, not if it is a catch block.
2249 TryCatchInformation* try_catch_info = reference->IsTryBlock()
2250 ? reference->GetTryCatchInformation()
2251 : nullptr;
2252 block->SetTryCatchInformation(try_catch_info);
2253}
2254
Calin Juravle2e768302015-07-28 14:41:11 +00002255HInstruction* HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
David Brazdilc7af85d2015-05-26 12:05:55 +01002256 DCHECK(HasExitBlock()) << "Unimplemented scenario";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002257 // Update the environments in this graph to have the invoke's environment
2258 // as parent.
2259 {
Vladimir Marko2c45bc92016-10-25 16:54:12 +01002260 // Skip the entry block, we do not need to update the entry's suspend check.
2261 for (HBasicBlock* block : GetReversePostOrderSkipEntryBlock()) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002262 for (HInstructionIterator instr_it(block->GetInstructions());
2263 !instr_it.Done();
2264 instr_it.Advance()) {
2265 HInstruction* current = instr_it.Current();
2266 if (current->NeedsEnvironment()) {
David Brazdildee58d62016-04-07 09:54:26 +00002267 DCHECK(current->HasEnvironment());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002268 current->GetEnvironment()->SetAndCopyParentChain(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002269 outer_graph->GetAllocator(), invoke->GetEnvironment());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002270 }
2271 }
2272 }
2273 }
2274 outer_graph->UpdateMaximumNumberOfOutVRegs(GetMaximumNumberOfOutVRegs());
Mingyao Yang69d75ff2017-02-07 13:06:06 -08002275
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002276 if (HasBoundsChecks()) {
2277 outer_graph->SetHasBoundsChecks(true);
2278 }
Mingyao Yang69d75ff2017-02-07 13:06:06 -08002279 if (HasLoops()) {
2280 outer_graph->SetHasLoops(true);
2281 }
2282 if (HasIrreducibleLoops()) {
2283 outer_graph->SetHasIrreducibleLoops(true);
2284 }
2285 if (HasTryCatch()) {
2286 outer_graph->SetHasTryCatch(true);
2287 }
Aart Bikb13c65b2017-03-21 20:14:07 -07002288 if (HasSIMD()) {
2289 outer_graph->SetHasSIMD(true);
2290 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002291
Calin Juravle2e768302015-07-28 14:41:11 +00002292 HInstruction* return_value = nullptr;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002293 if (GetBlocks().size() == 3) {
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002294 // Inliner already made sure we don't inline methods that always throw.
2295 DCHECK(!GetBlocks()[1]->GetLastInstruction()->IsThrow());
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00002296 // Simple case of an entry block, a body block, and an exit block.
2297 // Put the body block's instruction into `invoke`'s block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002298 HBasicBlock* body = GetBlocks()[1];
2299 DCHECK(GetBlocks()[0]->IsEntryBlock());
2300 DCHECK(GetBlocks()[2]->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002301 DCHECK(!body->IsExitBlock());
Nicolas Geoffray788f2f02016-01-22 12:41:38 +00002302 DCHECK(!body->IsInLoop());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002303 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002304
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00002305 // Note that we add instructions before the invoke only to simplify polymorphic inlining.
2306 invoke->GetBlock()->instructions_.AddBefore(invoke, body->GetInstructions());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002307 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002308
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002309 // Replace the invoke with the return value of the inlined graph.
2310 if (last->IsReturn()) {
Calin Juravle2e768302015-07-28 14:41:11 +00002311 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002312 } else {
2313 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002314 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002315
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002316 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002317 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002318 // Need to inline multiple blocks. We split `invoke`'s block
2319 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00002320 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002321 // with the second half.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002322 ArenaAllocator* allocator = outer_graph->GetAllocator();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002323 HBasicBlock* at = invoke->GetBlock();
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00002324 // Note that we split before the invoke only to simplify polymorphic inlining.
2325 HBasicBlock* to = at->SplitBeforeForInlining(invoke);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002326
Vladimir Markoec7802a2015-10-01 20:57:57 +01002327 HBasicBlock* first = entry_block_->GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002328 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01002329 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002330 exit_block_->ReplaceWith(to);
2331
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002332 // Update the meta information surrounding blocks:
2333 // (1) the graph they are now in,
2334 // (2) the reverse post order of that graph,
Nicolas Geoffray788f2f02016-01-22 12:41:38 +00002335 // (3) their potential loop information, inner and outer,
David Brazdil95177982015-10-30 12:56:58 -05002336 // (4) try block membership.
David Brazdil59a850e2015-11-10 13:04:30 +00002337 // Note that we do not need to update catch phi inputs because they
2338 // correspond to the register file of the outer method which the inlinee
2339 // cannot modify.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002340
2341 // We don't add the entry block, the exit block, and the first block, which
2342 // has been merged with `at`.
2343 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
2344
2345 // We add the `to` block.
2346 static constexpr int kNumberOfNewBlocksInCaller = 1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002347 size_t blocks_added = (reverse_post_order_.size() - kNumberOfSkippedBlocksInCallee)
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002348 + kNumberOfNewBlocksInCaller;
2349
2350 // Find the location of `at` in the outer graph's reverse post order. The new
2351 // blocks will be added after it.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002352 size_t index_of_at = IndexOfElement(outer_graph->reverse_post_order_, at);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002353 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
2354
David Brazdil95177982015-10-30 12:56:58 -05002355 // Do a reverse post order of the blocks in the callee and do (1), (2), (3)
2356 // and (4) to the blocks that apply.
Vladimir Marko2c45bc92016-10-25 16:54:12 +01002357 for (HBasicBlock* current : GetReversePostOrder()) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002358 if (current != exit_block_ && current != entry_block_ && current != first) {
David Brazdil95177982015-10-30 12:56:58 -05002359 DCHECK(current->GetTryCatchInformation() == nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002360 DCHECK(current->GetGraph() == this);
2361 current->SetGraph(outer_graph);
2362 outer_graph->AddBlock(current);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002363 outer_graph->reverse_post_order_[++index_of_at] = current;
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +00002364 UpdateLoopAndTryInformationOfNewBlock(current, at, /* replace_if_back_edge */ false);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002365 }
2366 }
2367
David Brazdil95177982015-10-30 12:56:58 -05002368 // Do (1), (2), (3) and (4) to `to`.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002369 to->SetGraph(outer_graph);
2370 outer_graph->AddBlock(to);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002371 outer_graph->reverse_post_order_[++index_of_at] = to;
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +00002372 // Only `to` can become a back edge, as the inlined blocks
2373 // are predecessors of `to`.
2374 UpdateLoopAndTryInformationOfNewBlock(to, at, /* replace_if_back_edge */ true);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00002375
David Brazdil3f523062016-02-29 16:53:33 +00002376 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002377 // to not `HReturn` but `HGoto` instead. Special case throwing blocks
2378 // to now get the outer graph exit block as successor. Note that the inliner
2379 // currently doesn't support inlining methods with try/catch.
2380 HPhi* return_value_phi = nullptr;
2381 bool rerun_dominance = false;
2382 bool rerun_loop_analysis = false;
2383 for (size_t pred = 0; pred < to->GetPredecessors().size(); ++pred) {
2384 HBasicBlock* predecessor = to->GetPredecessors()[pred];
David Brazdil3f523062016-02-29 16:53:33 +00002385 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002386 if (last->IsThrow()) {
2387 DCHECK(!at->IsTryBlock());
2388 predecessor->ReplaceSuccessor(to, outer_graph->GetExitBlock());
2389 --pred;
2390 // We need to re-run dominance information, as the exit block now has
2391 // a new dominator.
2392 rerun_dominance = true;
2393 if (predecessor->GetLoopInformation() != nullptr) {
2394 // The exit block and blocks post dominated by the exit block do not belong
2395 // to any loop. Because we do not compute the post dominators, we need to re-run
2396 // loop analysis to get the loop information correct.
2397 rerun_loop_analysis = true;
2398 }
2399 } else {
2400 if (last->IsReturnVoid()) {
2401 DCHECK(return_value == nullptr);
2402 DCHECK(return_value_phi == nullptr);
2403 } else {
David Brazdil3f523062016-02-29 16:53:33 +00002404 DCHECK(last->IsReturn());
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002405 if (return_value_phi != nullptr) {
2406 return_value_phi->AddInput(last->InputAt(0));
2407 } else if (return_value == nullptr) {
2408 return_value = last->InputAt(0);
2409 } else {
2410 // There will be multiple returns.
2411 return_value_phi = new (allocator) HPhi(
2412 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()), to->GetDexPc());
2413 to->AddPhi(return_value_phi);
2414 return_value_phi->AddInput(return_value);
2415 return_value_phi->AddInput(last->InputAt(0));
2416 return_value = return_value_phi;
2417 }
David Brazdil3f523062016-02-29 16:53:33 +00002418 }
2419 predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc()));
2420 predecessor->RemoveInstruction(last);
2421 }
2422 }
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002423 if (rerun_loop_analysis) {
Nicolas Geoffray1eede6a2017-03-02 16:14:53 +00002424 DCHECK(!outer_graph->HasIrreducibleLoops())
2425 << "Recomputing loop information in graphs with irreducible loops "
2426 << "is unsupported, as it could lead to loop header changes";
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002427 outer_graph->ClearLoopInformation();
2428 outer_graph->ClearDominanceInformation();
2429 outer_graph->BuildDominatorTree();
2430 } else if (rerun_dominance) {
2431 outer_graph->ClearDominanceInformation();
2432 outer_graph->ComputeDominanceInformation();
2433 }
David Brazdil3f523062016-02-29 16:53:33 +00002434 }
David Brazdil05144f42015-04-16 15:18:00 +01002435
2436 // Walk over the entry block and:
2437 // - Move constants from the entry block to the outer_graph's entry block,
2438 // - Replace HParameterValue instructions with their real value.
2439 // - Remove suspend checks, that hold an environment.
2440 // We must do this after the other blocks have been inlined, otherwise ids of
2441 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01002442 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01002443 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
2444 HInstruction* current = it.Current();
Calin Juravle214bbcd2015-10-20 14:54:07 +01002445 HInstruction* replacement = nullptr;
David Brazdil05144f42015-04-16 15:18:00 +01002446 if (current->IsNullConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002447 replacement = outer_graph->GetNullConstant(current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01002448 } else if (current->IsIntConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002449 replacement = outer_graph->GetIntConstant(
2450 current->AsIntConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01002451 } else if (current->IsLongConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002452 replacement = outer_graph->GetLongConstant(
2453 current->AsLongConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002454 } else if (current->IsFloatConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002455 replacement = outer_graph->GetFloatConstant(
2456 current->AsFloatConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002457 } else if (current->IsDoubleConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002458 replacement = outer_graph->GetDoubleConstant(
2459 current->AsDoubleConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01002460 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002461 if (kIsDebugBuild
2462 && invoke->IsInvokeStaticOrDirect()
2463 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
2464 // Ensure we do not use the last input of `invoke`, as it
2465 // contains a clinit check which is not an actual argument.
2466 size_t last_input_index = invoke->InputCount() - 1;
2467 DCHECK(parameter_index != last_input_index);
2468 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01002469 replacement = invoke->InputAt(parameter_index++);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002470 } else if (current->IsCurrentMethod()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002471 replacement = outer_graph->GetCurrentMethod();
David Brazdil05144f42015-04-16 15:18:00 +01002472 } else {
2473 DCHECK(current->IsGoto() || current->IsSuspendCheck());
2474 entry_block_->RemoveInstruction(current);
2475 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01002476 if (replacement != nullptr) {
2477 current->ReplaceWith(replacement);
2478 // If the current is the return value then we need to update the latter.
2479 if (current == return_value) {
2480 DCHECK_EQ(entry_block_, return_value->GetBlock());
2481 return_value = replacement;
2482 }
2483 }
2484 }
2485
Calin Juravle2e768302015-07-28 14:41:11 +00002486 return return_value;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002487}
2488
Mingyao Yang3584bce2015-05-19 16:01:59 -07002489/*
2490 * Loop will be transformed to:
2491 * old_pre_header
2492 * |
2493 * if_block
2494 * / \
Aart Bik3fc7f352015-11-20 22:03:03 -08002495 * true_block false_block
Mingyao Yang3584bce2015-05-19 16:01:59 -07002496 * \ /
2497 * new_pre_header
2498 * |
2499 * header
2500 */
2501void HGraph::TransformLoopHeaderForBCE(HBasicBlock* header) {
2502 DCHECK(header->IsLoopHeader());
Aart Bik3fc7f352015-11-20 22:03:03 -08002503 HBasicBlock* old_pre_header = header->GetDominator();
Mingyao Yang3584bce2015-05-19 16:01:59 -07002504
Aart Bik3fc7f352015-11-20 22:03:03 -08002505 // Need extra block to avoid critical edge.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002506 HBasicBlock* if_block = new (allocator_) HBasicBlock(this, header->GetDexPc());
2507 HBasicBlock* true_block = new (allocator_) HBasicBlock(this, header->GetDexPc());
2508 HBasicBlock* false_block = new (allocator_) HBasicBlock(this, header->GetDexPc());
2509 HBasicBlock* new_pre_header = new (allocator_) HBasicBlock(this, header->GetDexPc());
Mingyao Yang3584bce2015-05-19 16:01:59 -07002510 AddBlock(if_block);
Aart Bik3fc7f352015-11-20 22:03:03 -08002511 AddBlock(true_block);
2512 AddBlock(false_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002513 AddBlock(new_pre_header);
2514
Aart Bik3fc7f352015-11-20 22:03:03 -08002515 header->ReplacePredecessor(old_pre_header, new_pre_header);
2516 old_pre_header->successors_.clear();
2517 old_pre_header->dominated_blocks_.clear();
Mingyao Yang3584bce2015-05-19 16:01:59 -07002518
Aart Bik3fc7f352015-11-20 22:03:03 -08002519 old_pre_header->AddSuccessor(if_block);
2520 if_block->AddSuccessor(true_block); // True successor
2521 if_block->AddSuccessor(false_block); // False successor
2522 true_block->AddSuccessor(new_pre_header);
2523 false_block->AddSuccessor(new_pre_header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002524
Aart Bik3fc7f352015-11-20 22:03:03 -08002525 old_pre_header->dominated_blocks_.push_back(if_block);
2526 if_block->SetDominator(old_pre_header);
2527 if_block->dominated_blocks_.push_back(true_block);
2528 true_block->SetDominator(if_block);
2529 if_block->dominated_blocks_.push_back(false_block);
2530 false_block->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00002531 if_block->dominated_blocks_.push_back(new_pre_header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002532 new_pre_header->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00002533 new_pre_header->dominated_blocks_.push_back(header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002534 header->SetDominator(new_pre_header);
2535
Aart Bik3fc7f352015-11-20 22:03:03 -08002536 // Fix reverse post order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002537 size_t index_of_header = IndexOfElement(reverse_post_order_, header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002538 MakeRoomFor(&reverse_post_order_, 4, index_of_header - 1);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002539 reverse_post_order_[index_of_header++] = if_block;
Aart Bik3fc7f352015-11-20 22:03:03 -08002540 reverse_post_order_[index_of_header++] = true_block;
2541 reverse_post_order_[index_of_header++] = false_block;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002542 reverse_post_order_[index_of_header++] = new_pre_header;
Mingyao Yang3584bce2015-05-19 16:01:59 -07002543
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +00002544 // The pre_header can never be a back edge of a loop.
2545 DCHECK((old_pre_header->GetLoopInformation() == nullptr) ||
2546 !old_pre_header->GetLoopInformation()->IsBackEdge(*old_pre_header));
2547 UpdateLoopAndTryInformationOfNewBlock(
2548 if_block, old_pre_header, /* replace_if_back_edge */ false);
2549 UpdateLoopAndTryInformationOfNewBlock(
2550 true_block, old_pre_header, /* replace_if_back_edge */ false);
2551 UpdateLoopAndTryInformationOfNewBlock(
2552 false_block, old_pre_header, /* replace_if_back_edge */ false);
2553 UpdateLoopAndTryInformationOfNewBlock(
2554 new_pre_header, old_pre_header, /* replace_if_back_edge */ false);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002555}
2556
Aart Bikf8f5a162017-02-06 15:35:29 -08002557HBasicBlock* HGraph::TransformLoopForVectorization(HBasicBlock* header,
2558 HBasicBlock* body,
2559 HBasicBlock* exit) {
2560 DCHECK(header->IsLoopHeader());
2561 HLoopInformation* loop = header->GetLoopInformation();
2562
2563 // Add new loop blocks.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002564 HBasicBlock* new_pre_header = new (allocator_) HBasicBlock(this, header->GetDexPc());
2565 HBasicBlock* new_header = new (allocator_) HBasicBlock(this, header->GetDexPc());
2566 HBasicBlock* new_body = new (allocator_) HBasicBlock(this, header->GetDexPc());
Aart Bikf8f5a162017-02-06 15:35:29 -08002567 AddBlock(new_pre_header);
2568 AddBlock(new_header);
2569 AddBlock(new_body);
2570
2571 // Set up control flow.
2572 header->ReplaceSuccessor(exit, new_pre_header);
2573 new_pre_header->AddSuccessor(new_header);
2574 new_header->AddSuccessor(exit);
2575 new_header->AddSuccessor(new_body);
2576 new_body->AddSuccessor(new_header);
2577
2578 // Set up dominators.
2579 header->ReplaceDominatedBlock(exit, new_pre_header);
2580 new_pre_header->SetDominator(header);
2581 new_pre_header->dominated_blocks_.push_back(new_header);
2582 new_header->SetDominator(new_pre_header);
2583 new_header->dominated_blocks_.push_back(new_body);
2584 new_body->SetDominator(new_header);
2585 new_header->dominated_blocks_.push_back(exit);
2586 exit->SetDominator(new_header);
2587
2588 // Fix reverse post order.
2589 size_t index_of_header = IndexOfElement(reverse_post_order_, header);
2590 MakeRoomFor(&reverse_post_order_, 2, index_of_header);
2591 reverse_post_order_[++index_of_header] = new_pre_header;
2592 reverse_post_order_[++index_of_header] = new_header;
2593 size_t index_of_body = IndexOfElement(reverse_post_order_, body);
2594 MakeRoomFor(&reverse_post_order_, 1, index_of_body - 1);
2595 reverse_post_order_[index_of_body] = new_body;
2596
Aart Bikb07d1bc2017-04-05 10:03:15 -07002597 // Add gotos and suspend check (client must add conditional in header).
Vladimir Markoca6fff82017-10-03 14:49:14 +01002598 new_pre_header->AddInstruction(new (allocator_) HGoto());
2599 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(header->GetDexPc());
Aart Bikf8f5a162017-02-06 15:35:29 -08002600 new_header->AddInstruction(suspend_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002601 new_body->AddInstruction(new (allocator_) HGoto());
Aart Bikb07d1bc2017-04-05 10:03:15 -07002602 suspend_check->CopyEnvironmentFromWithLoopPhiAdjustment(
2603 loop->GetSuspendCheck()->GetEnvironment(), header);
Aart Bikf8f5a162017-02-06 15:35:29 -08002604
2605 // Update loop information.
2606 new_header->AddBackEdge(new_body);
2607 new_header->GetLoopInformation()->SetSuspendCheck(suspend_check);
2608 new_header->GetLoopInformation()->Populate();
2609 new_pre_header->SetLoopInformation(loop->GetPreHeader()->GetLoopInformation()); // outward
2610 HLoopInformationOutwardIterator it(*new_header);
2611 for (it.Advance(); !it.Done(); it.Advance()) {
2612 it.Current()->Add(new_pre_header);
2613 it.Current()->Add(new_header);
2614 it.Current()->Add(new_body);
2615 }
2616 return new_pre_header;
2617}
2618
David Brazdilf5552582015-12-27 13:36:12 +00002619static void CheckAgainstUpperBound(ReferenceTypeInfo rti, ReferenceTypeInfo upper_bound_rti)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002620 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdilf5552582015-12-27 13:36:12 +00002621 if (rti.IsValid()) {
2622 DCHECK(upper_bound_rti.IsSupertypeOf(rti))
2623 << " upper_bound_rti: " << upper_bound_rti
2624 << " rti: " << rti;
Nicolas Geoffray18401b72016-03-11 13:35:51 +00002625 DCHECK(!upper_bound_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes() || rti.IsExact())
2626 << " upper_bound_rti: " << upper_bound_rti
2627 << " rti: " << rti;
David Brazdilf5552582015-12-27 13:36:12 +00002628 }
2629}
2630
Calin Juravle2e768302015-07-28 14:41:11 +00002631void HInstruction::SetReferenceTypeInfo(ReferenceTypeInfo rti) {
2632 if (kIsDebugBuild) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002633 DCHECK_EQ(GetType(), DataType::Type::kReference);
Calin Juravle2e768302015-07-28 14:41:11 +00002634 ScopedObjectAccess soa(Thread::Current());
2635 DCHECK(rti.IsValid()) << "Invalid RTI for " << DebugName();
2636 if (IsBoundType()) {
2637 // Having the test here spares us from making the method virtual just for
2638 // the sake of a DCHECK.
David Brazdilf5552582015-12-27 13:36:12 +00002639 CheckAgainstUpperBound(rti, AsBoundType()->GetUpperBound());
Calin Juravle2e768302015-07-28 14:41:11 +00002640 }
2641 }
Vladimir Markoa1de9182016-02-25 11:37:38 +00002642 reference_type_handle_ = rti.GetTypeHandle();
2643 SetPackedFlag<kFlagReferenceTypeIsExact>(rti.IsExact());
Calin Juravle2e768302015-07-28 14:41:11 +00002644}
2645
David Brazdilf5552582015-12-27 13:36:12 +00002646void HBoundType::SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null) {
2647 if (kIsDebugBuild) {
2648 ScopedObjectAccess soa(Thread::Current());
2649 DCHECK(upper_bound.IsValid());
2650 DCHECK(!upper_bound_.IsValid()) << "Upper bound should only be set once.";
2651 CheckAgainstUpperBound(GetReferenceTypeInfo(), upper_bound);
2652 }
2653 upper_bound_ = upper_bound;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002654 SetPackedFlag<kFlagUpperCanBeNull>(can_be_null);
David Brazdilf5552582015-12-27 13:36:12 +00002655}
2656
Vladimir Markoa1de9182016-02-25 11:37:38 +00002657ReferenceTypeInfo ReferenceTypeInfo::Create(TypeHandle type_handle, bool is_exact) {
Calin Juravle2e768302015-07-28 14:41:11 +00002658 if (kIsDebugBuild) {
2659 ScopedObjectAccess soa(Thread::Current());
2660 DCHECK(IsValidHandle(type_handle));
Nicolas Geoffray18401b72016-03-11 13:35:51 +00002661 if (!is_exact) {
2662 DCHECK(!type_handle->CannotBeAssignedFromOtherTypes())
2663 << "Callers of ReferenceTypeInfo::Create should ensure is_exact is properly computed";
2664 }
Calin Juravle2e768302015-07-28 14:41:11 +00002665 }
Vladimir Markoa1de9182016-02-25 11:37:38 +00002666 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravle2e768302015-07-28 14:41:11 +00002667}
2668
Calin Juravleacf735c2015-02-12 15:25:22 +00002669std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
2670 ScopedObjectAccess soa(Thread::Current());
2671 os << "["
Calin Juravle2e768302015-07-28 14:41:11 +00002672 << " is_valid=" << rhs.IsValid()
David Sehr709b0702016-10-13 09:12:37 -07002673 << " type=" << (!rhs.IsValid() ? "?" : mirror::Class::PrettyClass(rhs.GetTypeHandle().Get()))
Calin Juravleacf735c2015-02-12 15:25:22 +00002674 << " is_exact=" << rhs.IsExact()
2675 << " ]";
2676 return os;
2677}
2678
Mark Mendellc4701932015-04-10 13:18:51 -04002679bool HInstruction::HasAnyEnvironmentUseBefore(HInstruction* other) {
2680 // For now, assume that instructions in different blocks may use the
2681 // environment.
2682 // TODO: Use the control flow to decide if this is true.
2683 if (GetBlock() != other->GetBlock()) {
2684 return true;
2685 }
2686
2687 // We know that we are in the same block. Walk from 'this' to 'other',
2688 // checking to see if there is any instruction with an environment.
2689 HInstruction* current = this;
2690 for (; current != other && current != nullptr; current = current->GetNext()) {
2691 // This is a conservative check, as the instruction result may not be in
2692 // the referenced environment.
2693 if (current->HasEnvironment()) {
2694 return true;
2695 }
2696 }
2697
2698 // We should have been called with 'this' before 'other' in the block.
2699 // Just confirm this.
2700 DCHECK(current != nullptr);
2701 return false;
2702}
2703
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002704void HInvoke::SetIntrinsic(Intrinsics intrinsic,
Aart Bik5d75afe2015-12-14 11:57:01 -08002705 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
2706 IntrinsicSideEffects side_effects,
2707 IntrinsicExceptions exceptions) {
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002708 intrinsic_ = intrinsic;
2709 IntrinsicOptimizations opt(this);
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002710
Aart Bik5d75afe2015-12-14 11:57:01 -08002711 // Adjust method's side effects from intrinsic table.
2712 switch (side_effects) {
2713 case kNoSideEffects: SetSideEffects(SideEffects::None()); break;
2714 case kReadSideEffects: SetSideEffects(SideEffects::AllReads()); break;
2715 case kWriteSideEffects: SetSideEffects(SideEffects::AllWrites()); break;
2716 case kAllSideEffects: SetSideEffects(SideEffects::AllExceptGCDependency()); break;
2717 }
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002718
2719 if (needs_env_or_cache == kNoEnvironmentOrCache) {
2720 opt.SetDoesNotNeedDexCache();
2721 opt.SetDoesNotNeedEnvironment();
2722 } else {
2723 // If we need an environment, that means there will be a call, which can trigger GC.
2724 SetSideEffects(GetSideEffects().Union(SideEffects::CanTriggerGC()));
2725 }
Aart Bik5d75afe2015-12-14 11:57:01 -08002726 // Adjust method's exception status from intrinsic table.
Aart Bik09e8d5f2016-01-22 16:49:55 -08002727 SetCanThrow(exceptions == kCanThrow);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002728}
2729
David Brazdil6de19382016-01-08 17:37:10 +00002730bool HNewInstance::IsStringAlloc() const {
2731 ScopedObjectAccess soa(Thread::Current());
2732 return GetReferenceTypeInfo().IsStringClass();
2733}
2734
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002735bool HInvoke::NeedsEnvironment() const {
2736 if (!IsIntrinsic()) {
2737 return true;
2738 }
2739 IntrinsicOptimizations opt(*this);
2740 return !opt.GetDoesNotNeedEnvironment();
2741}
2742
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002743const DexFile& HInvokeStaticOrDirect::GetDexFileForPcRelativeDexCache() const {
2744 ArtMethod* caller = GetEnvironment()->GetMethod();
2745 ScopedObjectAccess soa(Thread::Current());
2746 // `caller` is null for a top-level graph representing a method whose declaring
2747 // class was not resolved.
2748 return caller == nullptr ? GetBlock()->GetGraph()->GetDexFile() : *caller->GetDexFile();
2749}
2750
Vladimir Markodc151b22015-10-15 18:02:30 +01002751bool HInvokeStaticOrDirect::NeedsDexCacheOfDeclaringClass() const {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01002752 if (GetMethodLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002753 return false;
2754 }
2755 if (!IsIntrinsic()) {
2756 return true;
2757 }
2758 IntrinsicOptimizations opt(*this);
2759 return !opt.GetDoesNotNeedDexCache();
2760}
2761
Vladimir Markof64242a2015-12-01 14:58:23 +00002762std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs) {
2763 switch (rhs) {
2764 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
Vladimir Marko65979462017-05-19 17:25:12 +01002765 return os << "StringInit";
Vladimir Markof64242a2015-12-01 14:58:23 +00002766 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Marko65979462017-05-19 17:25:12 +01002767 return os << "Recursive";
2768 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
2769 return os << "BootImageLinkTimePcRelative";
Vladimir Markof64242a2015-12-01 14:58:23 +00002770 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Vladimir Marko19d7d502017-05-24 13:04:14 +01002771 return os << "DirectAddress";
Vladimir Marko0eb882b2017-05-15 13:39:18 +01002772 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry:
2773 return os << "BssEntry";
Vladimir Markoe7197bf2017-06-02 17:00:23 +01002774 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall:
2775 return os << "RuntimeCall";
Vladimir Markof64242a2015-12-01 14:58:23 +00002776 default:
2777 LOG(FATAL) << "Unknown MethodLoadKind: " << static_cast<int>(rhs);
2778 UNREACHABLE();
2779 }
2780}
2781
Vladimir Markofbb184a2015-11-13 14:47:00 +00002782std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs) {
2783 switch (rhs) {
2784 case HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit:
2785 return os << "explicit";
2786 case HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit:
2787 return os << "implicit";
2788 case HInvokeStaticOrDirect::ClinitCheckRequirement::kNone:
2789 return os << "none";
2790 default:
Vladimir Markof64242a2015-12-01 14:58:23 +00002791 LOG(FATAL) << "Unknown ClinitCheckRequirement: " << static_cast<int>(rhs);
2792 UNREACHABLE();
Vladimir Markofbb184a2015-11-13 14:47:00 +00002793 }
2794}
2795
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002796bool HLoadClass::InstructionDataEquals(const HInstruction* other) const {
2797 const HLoadClass* other_load_class = other->AsLoadClass();
2798 // TODO: To allow GVN for HLoadClass from different dex files, we should compare the type
2799 // names rather than type indexes. However, we shall also have to re-think the hash code.
2800 if (type_index_ != other_load_class->type_index_ ||
2801 GetPackedFields() != other_load_class->GetPackedFields()) {
2802 return false;
2803 }
Nicolas Geoffray9b1583e2016-12-13 13:43:31 +00002804 switch (GetLoadKind()) {
2805 case LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01002806 case LoadKind::kBootImageClassTable:
Nicolas Geoffray1ea9efc2017-01-16 22:57:39 +00002807 case LoadKind::kJitTableAddress: {
2808 ScopedObjectAccess soa(Thread::Current());
2809 return GetClass().Get() == other_load_class->GetClass().Get();
2810 }
Nicolas Geoffray9b1583e2016-12-13 13:43:31 +00002811 default:
Vladimir Marko48886c22017-01-06 11:45:47 +00002812 DCHECK(HasTypeReference(GetLoadKind()));
Nicolas Geoffray9b1583e2016-12-13 13:43:31 +00002813 return IsSameDexFile(GetDexFile(), other_load_class->GetDexFile());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002814 }
2815}
2816
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002817void HLoadClass::SetLoadKind(LoadKind load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002818 SetPackedField<LoadKindField>(load_kind);
2819
Vladimir Marko847e6ce2017-06-02 13:55:07 +01002820 if (load_kind != LoadKind::kRuntimeCall &&
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002821 load_kind != LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002822 RemoveAsUserOfInput(0u);
2823 SetRawInputAt(0u, nullptr);
2824 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002825
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002826 if (!NeedsEnvironment()) {
2827 RemoveEnvironment();
2828 SetSideEffects(SideEffects::None());
2829 }
2830}
2831
2832std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs) {
2833 switch (rhs) {
2834 case HLoadClass::LoadKind::kReferrersClass:
2835 return os << "ReferrersClass";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002836 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
2837 return os << "BootImageLinkTimePcRelative";
2838 case HLoadClass::LoadKind::kBootImageAddress:
2839 return os << "BootImageAddress";
Vladimir Marko94ec2db2017-09-06 17:21:03 +01002840 case HLoadClass::LoadKind::kBootImageClassTable:
2841 return os << "BootImageClassTable";
Vladimir Marko6bec91c2017-01-09 15:03:12 +00002842 case HLoadClass::LoadKind::kBssEntry:
2843 return os << "BssEntry";
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00002844 case HLoadClass::LoadKind::kJitTableAddress:
2845 return os << "JitTableAddress";
Vladimir Marko847e6ce2017-06-02 13:55:07 +01002846 case HLoadClass::LoadKind::kRuntimeCall:
2847 return os << "RuntimeCall";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002848 default:
2849 LOG(FATAL) << "Unknown HLoadClass::LoadKind: " << static_cast<int>(rhs);
2850 UNREACHABLE();
2851 }
2852}
2853
Vladimir Marko372f10e2016-05-17 16:30:10 +01002854bool HLoadString::InstructionDataEquals(const HInstruction* other) const {
2855 const HLoadString* other_load_string = other->AsLoadString();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002856 // TODO: To allow GVN for HLoadString from different dex files, we should compare the strings
2857 // rather than their indexes. However, we shall also have to re-think the hash code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002858 if (string_index_ != other_load_string->string_index_ ||
2859 GetPackedFields() != other_load_string->GetPackedFields()) {
2860 return false;
2861 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00002862 switch (GetLoadKind()) {
2863 case LoadKind::kBootImageAddress:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002864 case LoadKind::kBootImageInternTable:
Nicolas Geoffray1ea9efc2017-01-16 22:57:39 +00002865 case LoadKind::kJitTableAddress: {
2866 ScopedObjectAccess soa(Thread::Current());
2867 return GetString().Get() == other_load_string->GetString().Get();
2868 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00002869 default:
2870 return IsSameDexFile(GetDexFile(), other_load_string->GetDexFile());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002871 }
2872}
2873
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00002874void HLoadString::SetLoadKind(LoadKind load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002875 // Once sharpened, the load kind should not be changed again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01002876 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002877 SetPackedField<LoadKindField>(load_kind);
2878
Vladimir Marko847e6ce2017-06-02 13:55:07 +01002879 if (load_kind != LoadKind::kRuntimeCall) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002880 RemoveAsUserOfInput(0u);
2881 SetRawInputAt(0u, nullptr);
2882 }
2883 if (!NeedsEnvironment()) {
2884 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01002885 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002886 }
2887}
2888
2889std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs) {
2890 switch (rhs) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002891 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
2892 return os << "BootImageLinkTimePcRelative";
2893 case HLoadString::LoadKind::kBootImageAddress:
2894 return os << "BootImageAddress";
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002895 case HLoadString::LoadKind::kBootImageInternTable:
2896 return os << "BootImageInternTable";
Vladimir Markoaad75c62016-10-03 08:46:48 +00002897 case HLoadString::LoadKind::kBssEntry:
2898 return os << "BssEntry";
Mingyao Yangbe44dcf2016-11-30 14:17:32 -08002899 case HLoadString::LoadKind::kJitTableAddress:
2900 return os << "JitTableAddress";
Vladimir Marko847e6ce2017-06-02 13:55:07 +01002901 case HLoadString::LoadKind::kRuntimeCall:
2902 return os << "RuntimeCall";
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002903 default:
2904 LOG(FATAL) << "Unknown HLoadString::LoadKind: " << static_cast<int>(rhs);
2905 UNREACHABLE();
2906 }
2907}
2908
Mark Mendellc4701932015-04-10 13:18:51 -04002909void HInstruction::RemoveEnvironmentUsers() {
Vladimir Marko46817b82016-03-29 12:21:58 +01002910 for (const HUseListNode<HEnvironment*>& use : GetEnvUses()) {
2911 HEnvironment* user = use.GetUser();
2912 user->SetRawEnvAt(use.GetIndex(), nullptr);
Mark Mendellc4701932015-04-10 13:18:51 -04002913 }
Vladimir Marko46817b82016-03-29 12:21:58 +01002914 env_uses_.clear();
Mark Mendellc4701932015-04-10 13:18:51 -04002915}
2916
Artem Serovcced8ba2017-07-19 18:18:09 +01002917HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr) {
2918 HInstruction* clone = instr->Clone(instr->GetBlock()->GetGraph()->GetAllocator());
2919 HBasicBlock* block = instr->GetBlock();
2920
2921 if (instr->IsPhi()) {
2922 HPhi* phi = instr->AsPhi();
2923 DCHECK(!phi->HasEnvironment());
2924 HPhi* phi_clone = clone->AsPhi();
2925 block->ReplaceAndRemovePhiWith(phi, phi_clone);
2926 } else {
2927 block->ReplaceAndRemoveInstructionWith(instr, clone);
2928 if (instr->HasEnvironment()) {
2929 clone->CopyEnvironmentFrom(instr->GetEnvironment());
2930 HLoopInformation* loop_info = block->GetLoopInformation();
2931 if (instr->IsSuspendCheck() && loop_info != nullptr) {
2932 loop_info->SetSuspendCheck(clone->AsSuspendCheck());
2933 }
2934 }
2935 }
2936 return clone;
2937}
2938
Roland Levillainc9b21f82016-03-23 16:36:59 +00002939// Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -05002940HInstruction* HGraph::InsertOppositeCondition(HInstruction* cond, HInstruction* cursor) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002941 ArenaAllocator* allocator = GetAllocator();
Mark Mendellf6529172015-11-17 11:16:56 -05002942
2943 if (cond->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002944 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType())) {
Mark Mendellf6529172015-11-17 11:16:56 -05002945 // Can't reverse floating point conditions. We have to use HBooleanNot in that case.
2946 HInstruction* lhs = cond->InputAt(0);
2947 HInstruction* rhs = cond->InputAt(1);
David Brazdil5c004852015-11-23 09:44:52 +00002948 HInstruction* replacement = nullptr;
Mark Mendellf6529172015-11-17 11:16:56 -05002949 switch (cond->AsCondition()->GetOppositeCondition()) { // get *opposite*
2950 case kCondEQ: replacement = new (allocator) HEqual(lhs, rhs); break;
2951 case kCondNE: replacement = new (allocator) HNotEqual(lhs, rhs); break;
2952 case kCondLT: replacement = new (allocator) HLessThan(lhs, rhs); break;
2953 case kCondLE: replacement = new (allocator) HLessThanOrEqual(lhs, rhs); break;
2954 case kCondGT: replacement = new (allocator) HGreaterThan(lhs, rhs); break;
2955 case kCondGE: replacement = new (allocator) HGreaterThanOrEqual(lhs, rhs); break;
2956 case kCondB: replacement = new (allocator) HBelow(lhs, rhs); break;
2957 case kCondBE: replacement = new (allocator) HBelowOrEqual(lhs, rhs); break;
2958 case kCondA: replacement = new (allocator) HAbove(lhs, rhs); break;
2959 case kCondAE: replacement = new (allocator) HAboveOrEqual(lhs, rhs); break;
David Brazdil5c004852015-11-23 09:44:52 +00002960 default:
2961 LOG(FATAL) << "Unexpected condition";
2962 UNREACHABLE();
Mark Mendellf6529172015-11-17 11:16:56 -05002963 }
2964 cursor->GetBlock()->InsertInstructionBefore(replacement, cursor);
2965 return replacement;
2966 } else if (cond->IsIntConstant()) {
2967 HIntConstant* int_const = cond->AsIntConstant();
Roland Levillain1a653882016-03-18 18:05:57 +00002968 if (int_const->IsFalse()) {
Mark Mendellf6529172015-11-17 11:16:56 -05002969 return GetIntConstant(1);
2970 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002971 DCHECK(int_const->IsTrue()) << int_const->GetValue();
Mark Mendellf6529172015-11-17 11:16:56 -05002972 return GetIntConstant(0);
2973 }
2974 } else {
2975 HInstruction* replacement = new (allocator) HBooleanNot(cond);
2976 cursor->GetBlock()->InsertInstructionBefore(replacement, cursor);
2977 return replacement;
2978 }
2979}
2980
Roland Levillainc9285912015-12-18 10:38:42 +00002981std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs) {
2982 os << "["
2983 << " source=" << rhs.GetSource()
2984 << " destination=" << rhs.GetDestination()
2985 << " type=" << rhs.GetType()
2986 << " instruction=";
2987 if (rhs.GetInstruction() != nullptr) {
2988 os << rhs.GetInstruction()->DebugName() << ' ' << rhs.GetInstruction()->GetId();
2989 } else {
2990 os << "null";
2991 }
2992 os << " ]";
2993 return os;
2994}
2995
Roland Levillain86503782016-02-11 19:07:30 +00002996std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs) {
2997 switch (rhs) {
2998 case TypeCheckKind::kUnresolvedCheck:
2999 return os << "unresolved_check";
3000 case TypeCheckKind::kExactCheck:
3001 return os << "exact_check";
3002 case TypeCheckKind::kClassHierarchyCheck:
3003 return os << "class_hierarchy_check";
3004 case TypeCheckKind::kAbstractClassCheck:
3005 return os << "abstract_class_check";
3006 case TypeCheckKind::kInterfaceCheck:
3007 return os << "interface_check";
3008 case TypeCheckKind::kArrayObjectCheck:
3009 return os << "array_object_check";
3010 case TypeCheckKind::kArrayCheck:
3011 return os << "array_check";
3012 default:
3013 LOG(FATAL) << "Unknown TypeCheckKind: " << static_cast<int>(rhs);
3014 UNREACHABLE();
3015 }
3016}
3017
Andreas Gampe26de38b2016-07-27 17:53:11 -07003018std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind) {
3019 switch (kind) {
3020 case MemBarrierKind::kAnyStore:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003021 return os << "AnyStore";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003022 case MemBarrierKind::kLoadAny:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003023 return os << "LoadAny";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003024 case MemBarrierKind::kStoreStore:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003025 return os << "StoreStore";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003026 case MemBarrierKind::kAnyAny:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003027 return os << "AnyAny";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003028 case MemBarrierKind::kNTStoreStore:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003029 return os << "NTStoreStore";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003030
3031 default:
3032 LOG(FATAL) << "Unknown MemBarrierKind: " << static_cast<int>(kind);
3033 UNREACHABLE();
3034 }
3035}
3036
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003037} // namespace art