blob: d39c2aded5b90b74f73b3701c62fe3cabf4ca06d [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 Geoffraydbb9aef2017-11-23 10:44:11 +0000510 VLOG(compiler) << "Not compiled: Exceptional back edges";
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000511 return kAnalysisFailThrowCatchLoop;
David Brazdilffee3d32015-07-06 11:48:53 +0100512 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000513 block->GetLoopInformation()->Populate();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100514 }
515 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000516 return kAnalysisSuccess;
517}
518
519void HLoopInformation::Dump(std::ostream& os) {
520 os << "header: " << header_->GetBlockId() << std::endl;
521 os << "pre header: " << GetPreHeader()->GetBlockId() << std::endl;
522 for (HBasicBlock* block : back_edges_) {
523 os << "back edge: " << block->GetBlockId() << std::endl;
524 }
525 for (HBasicBlock* block : header_->GetPredecessors()) {
526 os << "predecessor: " << block->GetBlockId() << std::endl;
527 }
528 for (uint32_t idx : blocks_.Indexes()) {
529 os << " in loop: " << idx << std::endl;
530 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100531}
532
David Brazdil8d5b8b22015-03-24 10:51:52 +0000533void HGraph::InsertConstant(HConstant* constant) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000534 // New constants are inserted before the SuspendCheck at the bottom of the
535 // entry block. Note that this method can be called from the graph builder and
536 // the entry block therefore may not end with SuspendCheck->Goto yet.
537 HInstruction* insert_before = nullptr;
538
539 HInstruction* gota = entry_block_->GetLastInstruction();
540 if (gota != nullptr && gota->IsGoto()) {
541 HInstruction* suspend_check = gota->GetPrevious();
542 if (suspend_check != nullptr && suspend_check->IsSuspendCheck()) {
543 insert_before = suspend_check;
544 } else {
545 insert_before = gota;
546 }
547 }
548
549 if (insert_before == nullptr) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000550 entry_block_->AddInstruction(constant);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000551 } else {
552 entry_block_->InsertInstructionBefore(constant, insert_before);
David Brazdil46e2a392015-03-16 17:31:52 +0000553 }
554}
555
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600556HNullConstant* HGraph::GetNullConstant(uint32_t dex_pc) {
Nicolas Geoffray18e68732015-06-17 23:09:05 +0100557 // For simplicity, don't bother reviving the cached null constant if it is
558 // not null and not in a block. Otherwise, we need to clear the instruction
559 // id and/or any invariants the graph is assuming when adding new instructions.
560 if ((cached_null_constant_ == nullptr) || (cached_null_constant_->GetBlock() == nullptr)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100561 cached_null_constant_ = new (allocator_) HNullConstant(dex_pc);
David Brazdil4833f5a2015-12-16 10:37:39 +0000562 cached_null_constant_->SetReferenceTypeInfo(inexact_object_rti_);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000563 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000564 }
David Brazdil4833f5a2015-12-16 10:37:39 +0000565 if (kIsDebugBuild) {
566 ScopedObjectAccess soa(Thread::Current());
567 DCHECK(cached_null_constant_->GetReferenceTypeInfo().IsValid());
568 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000569 return cached_null_constant_;
570}
571
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100572HCurrentMethod* HGraph::GetCurrentMethod() {
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100573 // For simplicity, don't bother reviving the cached current method if it is
574 // not null and not in a block. Otherwise, we need to clear the instruction
575 // id and/or any invariants the graph is assuming when adding new instructions.
576 if ((cached_current_method_ == nullptr) || (cached_current_method_->GetBlock() == nullptr)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100577 cached_current_method_ = new (allocator_) HCurrentMethod(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100578 Is64BitInstructionSet(instruction_set_) ? DataType::Type::kInt64 : DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600579 entry_block_->GetDexPc());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100580 if (entry_block_->GetFirstInstruction() == nullptr) {
581 entry_block_->AddInstruction(cached_current_method_);
582 } else {
583 entry_block_->InsertInstructionBefore(
584 cached_current_method_, entry_block_->GetFirstInstruction());
585 }
586 }
587 return cached_current_method_;
588}
589
Igor Murashkind01745e2017-04-05 16:40:31 -0700590const char* HGraph::GetMethodName() const {
591 const DexFile::MethodId& method_id = dex_file_.GetMethodId(method_idx_);
592 return dex_file_.GetMethodName(method_id);
593}
594
595std::string HGraph::PrettyMethod(bool with_signature) const {
596 return dex_file_.PrettyMethod(method_idx_, with_signature);
597}
598
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100599HConstant* HGraph::GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000600 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100601 case DataType::Type::kBool:
David Brazdil8d5b8b22015-03-24 10:51:52 +0000602 DCHECK(IsUint<1>(value));
603 FALLTHROUGH_INTENDED;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100604 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100605 case DataType::Type::kInt8:
606 case DataType::Type::kUint16:
607 case DataType::Type::kInt16:
608 case DataType::Type::kInt32:
609 DCHECK(IsInt(DataType::Size(type) * kBitsPerByte, value));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600610 return GetIntConstant(static_cast<int32_t>(value), dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000611
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100612 case DataType::Type::kInt64:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600613 return GetLongConstant(value, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000614
615 default:
616 LOG(FATAL) << "Unsupported constant type";
617 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000618 }
David Brazdil46e2a392015-03-16 17:31:52 +0000619}
620
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621void HGraph::CacheFloatConstant(HFloatConstant* constant) {
622 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
623 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
624 cached_float_constants_.Overwrite(value, constant);
625}
626
627void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
628 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
629 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
630 cached_double_constants_.Overwrite(value, constant);
631}
632
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000633void HLoopInformation::Add(HBasicBlock* block) {
634 blocks_.SetBit(block->GetBlockId());
635}
636
David Brazdil46e2a392015-03-16 17:31:52 +0000637void HLoopInformation::Remove(HBasicBlock* block) {
638 blocks_.ClearBit(block->GetBlockId());
639}
640
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100641void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
642 if (blocks_.IsBitSet(block->GetBlockId())) {
643 return;
644 }
645
646 blocks_.SetBit(block->GetBlockId());
647 block->SetInLoop(this);
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100648 if (block->IsLoopHeader()) {
649 // We're visiting loops in post-order, so inner loops must have been
650 // populated already.
651 DCHECK(block->GetLoopInformation()->IsPopulated());
652 if (block->GetLoopInformation()->IsIrreducible()) {
653 contains_irreducible_loop_ = true;
654 }
655 }
Vladimir Marko60584552015-09-03 13:35:12 +0000656 for (HBasicBlock* predecessor : block->GetPredecessors()) {
657 PopulateRecursive(predecessor);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100658 }
659}
660
David Brazdilc2e8af92016-04-05 17:15:19 +0100661void HLoopInformation::PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized) {
662 size_t block_id = block->GetBlockId();
663
664 // If `block` is in `finalized`, we know its membership in the loop has been
665 // decided and it does not need to be revisited.
666 if (finalized->IsBitSet(block_id)) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000667 return;
668 }
669
David Brazdilc2e8af92016-04-05 17:15:19 +0100670 bool is_finalized = false;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000671 if (block->IsLoopHeader()) {
672 // If we hit a loop header in an irreducible loop, we first check if the
673 // pre header of that loop belongs to the currently analyzed loop. If it does,
674 // then we visit the back edges.
675 // Note that we cannot use GetPreHeader, as the loop may have not been populated
676 // yet.
677 HBasicBlock* pre_header = block->GetPredecessors()[0];
David Brazdilc2e8af92016-04-05 17:15:19 +0100678 PopulateIrreducibleRecursive(pre_header, finalized);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000679 if (blocks_.IsBitSet(pre_header->GetBlockId())) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000680 block->SetInLoop(this);
David Brazdilc2e8af92016-04-05 17:15:19 +0100681 blocks_.SetBit(block_id);
682 finalized->SetBit(block_id);
683 is_finalized = true;
684
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000685 HLoopInformation* info = block->GetLoopInformation();
686 for (HBasicBlock* back_edge : info->GetBackEdges()) {
David Brazdilc2e8af92016-04-05 17:15:19 +0100687 PopulateIrreducibleRecursive(back_edge, finalized);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000688 }
689 }
690 } else {
691 // Visit all predecessors. If one predecessor is part of the loop, this
692 // block is also part of this loop.
693 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdilc2e8af92016-04-05 17:15:19 +0100694 PopulateIrreducibleRecursive(predecessor, finalized);
695 if (!is_finalized && blocks_.IsBitSet(predecessor->GetBlockId())) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000696 block->SetInLoop(this);
David Brazdilc2e8af92016-04-05 17:15:19 +0100697 blocks_.SetBit(block_id);
698 finalized->SetBit(block_id);
699 is_finalized = true;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000700 }
701 }
702 }
David Brazdilc2e8af92016-04-05 17:15:19 +0100703
704 // All predecessors have been recursively visited. Mark finalized if not marked yet.
705 if (!is_finalized) {
706 finalized->SetBit(block_id);
707 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000708}
709
710void HLoopInformation::Populate() {
David Brazdila4b8c212015-05-07 09:59:30 +0100711 DCHECK_EQ(blocks_.NumSetBits(), 0u) << "Loop information has already been populated";
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000712 // Populate this loop: starting with the back edge, recursively add predecessors
713 // that are not already part of that loop. Set the header as part of the loop
714 // to end the recursion.
715 // This is a recursive implementation of the algorithm described in
716 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
David Brazdilc2e8af92016-04-05 17:15:19 +0100717 HGraph* graph = header_->GetGraph();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000718 blocks_.SetBit(header_->GetBlockId());
719 header_->SetInLoop(this);
David Brazdilc2e8af92016-04-05 17:15:19 +0100720
David Brazdil3f4a5222016-05-06 12:46:21 +0100721 bool is_irreducible_loop = HasBackEdgeNotDominatedByHeader();
David Brazdilc2e8af92016-04-05 17:15:19 +0100722
723 if (is_irreducible_loop) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100724 // Allocate memory from local ScopedArenaAllocator.
725 ScopedArenaAllocator allocator(graph->GetArenaStack());
726 ArenaBitVector visited(&allocator,
David Brazdilc2e8af92016-04-05 17:15:19 +0100727 graph->GetBlocks().size(),
728 /* expandable */ false,
729 kArenaAllocGraphBuilder);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100730 visited.ClearAllBits();
David Brazdil5a620592016-05-05 11:27:03 +0100731 // Stop marking blocks at the loop header.
732 visited.SetBit(header_->GetBlockId());
733
David Brazdilc2e8af92016-04-05 17:15:19 +0100734 for (HBasicBlock* back_edge : GetBackEdges()) {
735 PopulateIrreducibleRecursive(back_edge, &visited);
736 }
737 } else {
738 for (HBasicBlock* back_edge : GetBackEdges()) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000739 PopulateRecursive(back_edge);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100740 }
David Brazdila4b8c212015-05-07 09:59:30 +0100741 }
David Brazdilc2e8af92016-04-05 17:15:19 +0100742
Vladimir Markofd66c502016-04-18 15:37:01 +0100743 if (!is_irreducible_loop && graph->IsCompilingOsr()) {
744 // When compiling in OSR mode, all loops in the compiled method may be entered
745 // from the interpreter. We treat this OSR entry point just like an extra entry
746 // to an irreducible loop, so we need to mark the method's loops as irreducible.
747 // This does not apply to inlined loops which do not act as OSR entry points.
748 if (suspend_check_ == nullptr) {
749 // Just building the graph in OSR mode, this loop is not inlined. We never build an
750 // inner graph in OSR mode as we can do OSR transition only from the outer method.
751 is_irreducible_loop = true;
752 } else {
753 // Look at the suspend check's environment to determine if the loop was inlined.
754 DCHECK(suspend_check_->HasEnvironment());
755 if (!suspend_check_->GetEnvironment()->IsFromInlinedInvoke()) {
756 is_irreducible_loop = true;
757 }
758 }
759 }
760 if (is_irreducible_loop) {
David Brazdilc2e8af92016-04-05 17:15:19 +0100761 irreducible_ = true;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100762 contains_irreducible_loop_ = true;
David Brazdilc2e8af92016-04-05 17:15:19 +0100763 graph->SetHasIrreducibleLoops(true);
764 }
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800765 graph->SetHasLoops(true);
David Brazdila4b8c212015-05-07 09:59:30 +0100766}
767
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100768HBasicBlock* HLoopInformation::GetPreHeader() const {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000769 HBasicBlock* block = header_->GetPredecessors()[0];
770 DCHECK(irreducible_ || (block == header_->GetDominator()));
771 return block;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100772}
773
774bool HLoopInformation::Contains(const HBasicBlock& block) const {
775 return blocks_.IsBitSet(block.GetBlockId());
776}
777
778bool HLoopInformation::IsIn(const HLoopInformation& other) const {
779 return other.blocks_.IsBitSet(header_->GetBlockId());
780}
781
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800782bool HLoopInformation::IsDefinedOutOfTheLoop(HInstruction* instruction) const {
783 return !blocks_.IsBitSet(instruction->GetBlock()->GetBlockId());
Aart Bik73f1f3b2015-10-28 15:28:08 -0700784}
785
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100786size_t HLoopInformation::GetLifetimeEnd() const {
787 size_t last_position = 0;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100788 for (HBasicBlock* back_edge : GetBackEdges()) {
789 last_position = std::max(back_edge->GetLifetimeEnd(), last_position);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100790 }
791 return last_position;
792}
793
David Brazdil3f4a5222016-05-06 12:46:21 +0100794bool HLoopInformation::HasBackEdgeNotDominatedByHeader() const {
795 for (HBasicBlock* back_edge : GetBackEdges()) {
796 DCHECK(back_edge->GetDominator() != nullptr);
797 if (!header_->Dominates(back_edge)) {
798 return true;
799 }
800 }
801 return false;
802}
803
Anton Shaminf89381f2016-05-16 16:44:13 +0600804bool HLoopInformation::DominatesAllBackEdges(HBasicBlock* block) {
805 for (HBasicBlock* back_edge : GetBackEdges()) {
806 if (!block->Dominates(back_edge)) {
807 return false;
808 }
809 }
810 return true;
811}
812
David Sehrc757dec2016-11-04 15:48:34 -0700813
814bool HLoopInformation::HasExitEdge() const {
815 // Determine if this loop has at least one exit edge.
816 HBlocksInLoopReversePostOrderIterator it_loop(*this);
817 for (; !it_loop.Done(); it_loop.Advance()) {
818 for (HBasicBlock* successor : it_loop.Current()->GetSuccessors()) {
819 if (!Contains(*successor)) {
820 return true;
821 }
822 }
823 }
824 return false;
825}
826
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100827bool HBasicBlock::Dominates(HBasicBlock* other) const {
828 // Walk up the dominator tree from `other`, to find out if `this`
829 // is an ancestor.
830 HBasicBlock* current = other;
831 while (current != nullptr) {
832 if (current == this) {
833 return true;
834 }
835 current = current->GetDominator();
836 }
837 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100838}
839
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100840static void UpdateInputsUsers(HInstruction* instruction) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100841 HInputsRef inputs = instruction->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100842 for (size_t i = 0; i < inputs.size(); ++i) {
843 inputs[i]->AddUseAt(instruction, i);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100844 }
845 // Environment should be created later.
846 DCHECK(!instruction->HasEnvironment());
847}
848
Artem Serovcced8ba2017-07-19 18:18:09 +0100849void HBasicBlock::ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement) {
850 DCHECK(initial->GetBlock() == this);
851 InsertPhiAfter(replacement, initial);
852 initial->ReplaceWith(replacement);
853 RemovePhi(initial);
854}
855
Roland Levillainccc07a92014-09-16 14:48:16 +0100856void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
857 HInstruction* replacement) {
858 DCHECK(initial->GetBlock() == this);
Mark Mendell805b3b52015-09-18 14:10:29 -0400859 if (initial->IsControlFlow()) {
860 // We can only replace a control flow instruction with another control flow instruction.
861 DCHECK(replacement->IsControlFlow());
862 DCHECK_EQ(replacement->GetId(), -1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100863 DCHECK_EQ(replacement->GetType(), DataType::Type::kVoid);
Mark Mendell805b3b52015-09-18 14:10:29 -0400864 DCHECK_EQ(initial->GetBlock(), this);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100865 DCHECK_EQ(initial->GetType(), DataType::Type::kVoid);
Vladimir Marko46817b82016-03-29 12:21:58 +0100866 DCHECK(initial->GetUses().empty());
867 DCHECK(initial->GetEnvUses().empty());
Mark Mendell805b3b52015-09-18 14:10:29 -0400868 replacement->SetBlock(this);
869 replacement->SetId(GetGraph()->GetNextInstructionId());
870 instructions_.InsertInstructionBefore(replacement, initial);
871 UpdateInputsUsers(replacement);
872 } else {
873 InsertInstructionBefore(replacement, initial);
874 initial->ReplaceWith(replacement);
875 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100876 RemoveInstruction(initial);
877}
878
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100879static void Add(HInstructionList* instruction_list,
880 HBasicBlock* block,
881 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000882 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000883 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100884 instruction->SetBlock(block);
885 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100886 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100887 instruction_list->AddInstruction(instruction);
888}
889
890void HBasicBlock::AddInstruction(HInstruction* instruction) {
891 Add(&instructions_, this, instruction);
892}
893
894void HBasicBlock::AddPhi(HPhi* phi) {
895 Add(&phis_, this, phi);
896}
897
David Brazdilc3d743f2015-04-22 13:40:50 +0100898void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
899 DCHECK(!cursor->IsPhi());
900 DCHECK(!instruction->IsPhi());
901 DCHECK_EQ(instruction->GetId(), -1);
902 DCHECK_NE(cursor->GetId(), -1);
903 DCHECK_EQ(cursor->GetBlock(), this);
904 DCHECK(!instruction->IsControlFlow());
905 instruction->SetBlock(this);
906 instruction->SetId(GetGraph()->GetNextInstructionId());
907 UpdateInputsUsers(instruction);
908 instructions_.InsertInstructionBefore(instruction, cursor);
909}
910
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100911void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
912 DCHECK(!cursor->IsPhi());
913 DCHECK(!instruction->IsPhi());
914 DCHECK_EQ(instruction->GetId(), -1);
915 DCHECK_NE(cursor->GetId(), -1);
916 DCHECK_EQ(cursor->GetBlock(), this);
917 DCHECK(!instruction->IsControlFlow());
918 DCHECK(!cursor->IsControlFlow());
919 instruction->SetBlock(this);
920 instruction->SetId(GetGraph()->GetNextInstructionId());
921 UpdateInputsUsers(instruction);
922 instructions_.InsertInstructionAfter(instruction, cursor);
923}
924
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100925void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
926 DCHECK_EQ(phi->GetId(), -1);
927 DCHECK_NE(cursor->GetId(), -1);
928 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100929 phi->SetBlock(this);
930 phi->SetId(GetGraph()->GetNextInstructionId());
931 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100932 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100933}
934
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100935static void Remove(HInstructionList* instruction_list,
936 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000937 HInstruction* instruction,
938 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100939 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100940 instruction->SetBlock(nullptr);
941 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000942 if (ensure_safety) {
Vladimir Marko46817b82016-03-29 12:21:58 +0100943 DCHECK(instruction->GetUses().empty());
944 DCHECK(instruction->GetEnvUses().empty());
David Brazdil1abb4192015-02-17 18:33:36 +0000945 RemoveAsUser(instruction);
946 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100947}
948
David Brazdil1abb4192015-02-17 18:33:36 +0000949void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100950 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000951 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100952}
953
David Brazdil1abb4192015-02-17 18:33:36 +0000954void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
955 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100956}
957
David Brazdilc7508e92015-04-27 13:28:57 +0100958void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
959 if (instruction->IsPhi()) {
960 RemovePhi(instruction->AsPhi(), ensure_safety);
961 } else {
962 RemoveInstruction(instruction, ensure_safety);
963 }
964}
965
Vladimir Marko69d310e2017-10-09 14:12:23 +0100966void HEnvironment::CopyFrom(ArrayRef<HInstruction* const> locals) {
Vladimir Marko71bf8092015-09-15 15:33:14 +0100967 for (size_t i = 0; i < locals.size(); i++) {
968 HInstruction* instruction = locals[i];
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +0100969 SetRawEnvAt(i, instruction);
970 if (instruction != nullptr) {
971 instruction->AddEnvUseAt(this, i);
972 }
973 }
974}
975
David Brazdiled596192015-01-23 10:39:45 +0000976void HEnvironment::CopyFrom(HEnvironment* env) {
977 for (size_t i = 0; i < env->Size(); i++) {
978 HInstruction* instruction = env->GetInstructionAt(i);
979 SetRawEnvAt(i, instruction);
980 if (instruction != nullptr) {
981 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100982 }
David Brazdiled596192015-01-23 10:39:45 +0000983 }
984}
985
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700986void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
987 HBasicBlock* loop_header) {
988 DCHECK(loop_header->IsLoopHeader());
989 for (size_t i = 0; i < env->Size(); i++) {
990 HInstruction* instruction = env->GetInstructionAt(i);
991 SetRawEnvAt(i, instruction);
992 if (instruction == nullptr) {
993 continue;
994 }
995 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
996 // At the end of the loop pre-header, the corresponding value for instruction
997 // is the first input of the phi.
998 HInstruction* initial = instruction->AsPhi()->InputAt(0);
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700999 SetRawEnvAt(i, initial);
1000 initial->AddEnvUseAt(this, i);
1001 } else {
1002 instruction->AddEnvUseAt(this, i);
1003 }
1004 }
1005}
1006
David Brazdil1abb4192015-02-17 18:33:36 +00001007void HEnvironment::RemoveAsUserOfInput(size_t index) const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001008 const HUserRecord<HEnvironment*>& env_use = vregs_[index];
1009 HInstruction* user = env_use.GetInstruction();
1010 auto before_env_use_node = env_use.GetBeforeUseNode();
1011 user->env_uses_.erase_after(before_env_use_node);
1012 user->FixUpUserRecordsAfterEnvUseRemoval(before_env_use_node);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001013}
1014
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001015HInstruction::InstructionKind HInstruction::GetKind() const {
1016 return GetKindInternal();
1017}
1018
Calin Juravle77520bc2015-01-12 18:45:46 +00001019HInstruction* HInstruction::GetNextDisregardingMoves() const {
1020 HInstruction* next = GetNext();
1021 while (next != nullptr && next->IsParallelMove()) {
1022 next = next->GetNext();
1023 }
1024 return next;
1025}
1026
1027HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
1028 HInstruction* previous = GetPrevious();
1029 while (previous != nullptr && previous->IsParallelMove()) {
1030 previous = previous->GetPrevious();
1031 }
1032 return previous;
1033}
1034
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001035void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001036 if (first_instruction_ == nullptr) {
1037 DCHECK(last_instruction_ == nullptr);
1038 first_instruction_ = last_instruction_ = instruction;
1039 } else {
George Burgess IVa4b58ed2017-06-22 15:47:25 -07001040 DCHECK(last_instruction_ != nullptr);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001041 last_instruction_->next_ = instruction;
1042 instruction->previous_ = last_instruction_;
1043 last_instruction_ = instruction;
1044 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001045}
1046
David Brazdilc3d743f2015-04-22 13:40:50 +01001047void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
1048 DCHECK(Contains(cursor));
1049 if (cursor == first_instruction_) {
1050 cursor->previous_ = instruction;
1051 instruction->next_ = cursor;
1052 first_instruction_ = instruction;
1053 } else {
1054 instruction->previous_ = cursor->previous_;
1055 instruction->next_ = cursor;
1056 cursor->previous_ = instruction;
1057 instruction->previous_->next_ = instruction;
1058 }
1059}
1060
1061void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
1062 DCHECK(Contains(cursor));
1063 if (cursor == last_instruction_) {
1064 cursor->next_ = instruction;
1065 instruction->previous_ = cursor;
1066 last_instruction_ = instruction;
1067 } else {
1068 instruction->next_ = cursor->next_;
1069 instruction->previous_ = cursor;
1070 cursor->next_ = instruction;
1071 instruction->next_->previous_ = instruction;
1072 }
1073}
1074
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001075void HInstructionList::RemoveInstruction(HInstruction* instruction) {
1076 if (instruction->previous_ != nullptr) {
1077 instruction->previous_->next_ = instruction->next_;
1078 }
1079 if (instruction->next_ != nullptr) {
1080 instruction->next_->previous_ = instruction->previous_;
1081 }
1082 if (instruction == first_instruction_) {
1083 first_instruction_ = instruction->next_;
1084 }
1085 if (instruction == last_instruction_) {
1086 last_instruction_ = instruction->previous_;
1087 }
1088}
1089
Roland Levillain6b469232014-09-25 10:10:38 +01001090bool HInstructionList::Contains(HInstruction* instruction) const {
1091 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
1092 if (it.Current() == instruction) {
1093 return true;
1094 }
1095 }
1096 return false;
1097}
1098
Roland Levillainccc07a92014-09-16 14:48:16 +01001099bool HInstructionList::FoundBefore(const HInstruction* instruction1,
1100 const HInstruction* instruction2) const {
1101 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
1102 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
1103 if (it.Current() == instruction1) {
1104 return true;
1105 }
1106 if (it.Current() == instruction2) {
1107 return false;
1108 }
1109 }
1110 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
1111 return true;
1112}
1113
Artem Serov1de1e112017-07-20 16:33:59 +01001114bool HInstruction::Dominates(HInstruction* other_instruction, bool strictly) const {
Roland Levillain6c82d402014-10-13 16:10:27 +01001115 if (other_instruction == this) {
1116 // An instruction does not strictly dominate itself.
Artem Serov1de1e112017-07-20 16:33:59 +01001117 return !strictly;
Roland Levillain6c82d402014-10-13 16:10:27 +01001118 }
Roland Levillainccc07a92014-09-16 14:48:16 +01001119 HBasicBlock* block = GetBlock();
1120 HBasicBlock* other_block = other_instruction->GetBlock();
1121 if (block != other_block) {
1122 return GetBlock()->Dominates(other_instruction->GetBlock());
1123 } else {
1124 // If both instructions are in the same block, ensure this
1125 // instruction comes before `other_instruction`.
1126 if (IsPhi()) {
1127 if (!other_instruction->IsPhi()) {
1128 // Phis appear before non phi-instructions so this instruction
1129 // dominates `other_instruction`.
1130 return true;
1131 } else {
1132 // There is no order among phis.
1133 LOG(FATAL) << "There is no dominance between phis of a same block.";
1134 return false;
1135 }
1136 } else {
1137 // `this` is not a phi.
1138 if (other_instruction->IsPhi()) {
1139 // Phis appear before non phi-instructions so this instruction
1140 // does not dominate `other_instruction`.
1141 return false;
1142 } else {
1143 // Check whether this instruction comes before
1144 // `other_instruction` in the instruction list.
1145 return block->GetInstructions().FoundBefore(this, other_instruction);
1146 }
1147 }
1148 }
1149}
1150
Artem Serov1de1e112017-07-20 16:33:59 +01001151bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
1152 return Dominates(other_instruction, /* strictly */ true);
1153}
1154
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001155void HInstruction::RemoveEnvironment() {
1156 RemoveEnvironmentUses(this);
1157 environment_ = nullptr;
1158}
1159
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001160void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001161 DCHECK(other != nullptr);
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001162 // Note: fixup_end remains valid across splice_after().
1163 auto fixup_end = other->uses_.empty() ? other->uses_.begin() : ++other->uses_.begin();
1164 other->uses_.splice_after(other->uses_.before_begin(), uses_);
1165 other->FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001166
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001167 // Note: env_fixup_end remains valid across splice_after().
1168 auto env_fixup_end =
1169 other->env_uses_.empty() ? other->env_uses_.begin() : ++other->env_uses_.begin();
1170 other->env_uses_.splice_after(other->env_uses_.before_begin(), env_uses_);
1171 other->FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001172
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001173 DCHECK(uses_.empty());
1174 DCHECK(env_uses_.empty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001175}
1176
Artem Serov1de1e112017-07-20 16:33:59 +01001177void HInstruction::ReplaceUsesDominatedBy(HInstruction* dominator,
1178 HInstruction* replacement,
1179 bool strictly) {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00001180 const HUseList<HInstruction*>& uses = GetUses();
1181 for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) {
1182 HInstruction* user = it->GetUser();
1183 size_t index = it->GetIndex();
1184 // Increment `it` now because `*it` may disappear thanks to user->ReplaceInput().
1185 ++it;
Artem Serov1de1e112017-07-20 16:33:59 +01001186 if (dominator->Dominates(user, strictly)) {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00001187 user->ReplaceInput(replacement, index);
1188 }
1189 }
1190}
1191
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001192void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001193 HUserRecord<HInstruction*> input_use = InputRecordAt(index);
Vladimir Markoc6b56272016-04-20 18:45:25 +01001194 if (input_use.GetInstruction() == replacement) {
1195 // Nothing to do.
1196 return;
1197 }
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001198 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001199 // Note: fixup_end remains valid across splice_after().
1200 auto fixup_end =
1201 replacement->uses_.empty() ? replacement->uses_.begin() : ++replacement->uses_.begin();
1202 replacement->uses_.splice_after(replacement->uses_.before_begin(),
1203 input_use.GetInstruction()->uses_,
1204 before_use_node);
1205 replacement->FixUpUserRecordsAfterUseInsertion(fixup_end);
1206 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001207}
1208
Nicolas Geoffray39468442014-09-02 15:17:15 +01001209size_t HInstruction::EnvironmentSize() const {
1210 return HasEnvironment() ? environment_->Size() : 0;
1211}
1212
Mingyao Yanga9dbe832016-12-15 12:02:53 -08001213void HVariableInputSizeInstruction::AddInput(HInstruction* input) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001214 DCHECK(input->GetBlock() != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001215 inputs_.push_back(HUserRecord<HInstruction*>(input));
1216 input->AddUseAt(this, inputs_.size() - 1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001217}
1218
Mingyao Yanga9dbe832016-12-15 12:02:53 -08001219void HVariableInputSizeInstruction::InsertInputAt(size_t index, HInstruction* input) {
1220 inputs_.insert(inputs_.begin() + index, HUserRecord<HInstruction*>(input));
1221 input->AddUseAt(this, index);
1222 // Update indexes in use nodes of inputs that have been pushed further back by the insert().
1223 for (size_t i = index + 1u, e = inputs_.size(); i < e; ++i) {
1224 DCHECK_EQ(inputs_[i].GetUseNode()->GetIndex(), i - 1u);
1225 inputs_[i].GetUseNode()->SetIndex(i);
1226 }
1227}
1228
1229void HVariableInputSizeInstruction::RemoveInputAt(size_t index) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001230 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001231 inputs_.erase(inputs_.begin() + index);
Vladimir Marko372f10e2016-05-17 16:30:10 +01001232 // Update indexes in use nodes of inputs that have been pulled forward by the erase().
1233 for (size_t i = index, e = inputs_.size(); i < e; ++i) {
1234 DCHECK_EQ(inputs_[i].GetUseNode()->GetIndex(), i + 1u);
1235 inputs_[i].GetUseNode()->SetIndex(i);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001236 }
David Brazdil2d7352b2015-04-20 14:52:42 +01001237}
1238
Igor Murashkind01745e2017-04-05 16:40:31 -07001239void HVariableInputSizeInstruction::RemoveAllInputs() {
1240 RemoveAsUserOfAllInputs();
1241 DCHECK(!HasNonEnvironmentUses());
1242
1243 inputs_.clear();
1244 DCHECK_EQ(0u, InputCount());
1245}
1246
Igor Murashkin6ef45672017-08-08 13:59:55 -07001247size_t HConstructorFence::RemoveConstructorFences(HInstruction* instruction) {
Igor Murashkind01745e2017-04-05 16:40:31 -07001248 DCHECK(instruction->GetBlock() != nullptr);
1249 // Removing constructor fences only makes sense for instructions with an object return type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001250 DCHECK_EQ(DataType::Type::kReference, instruction->GetType());
Igor Murashkind01745e2017-04-05 16:40:31 -07001251
Igor Murashkin6ef45672017-08-08 13:59:55 -07001252 // Return how many instructions were removed for statistic purposes.
1253 size_t remove_count = 0;
1254
Igor Murashkind01745e2017-04-05 16:40:31 -07001255 // Efficient implementation that simultaneously (in one pass):
1256 // * Scans the uses list for all constructor fences.
1257 // * Deletes that constructor fence from the uses list of `instruction`.
1258 // * Deletes `instruction` from the constructor fence's inputs.
1259 // * Deletes the constructor fence if it now has 0 inputs.
1260
1261 const HUseList<HInstruction*>& uses = instruction->GetUses();
1262 // Warning: Although this is "const", we might mutate the list when calling RemoveInputAt.
1263 for (auto it = uses.begin(), end = uses.end(); it != end; ) {
1264 const HUseListNode<HInstruction*>& use_node = *it;
1265 HInstruction* const use_instruction = use_node.GetUser();
1266
1267 // Advance the iterator immediately once we fetch the use_node.
1268 // Warning: If the input is removed, the current iterator becomes invalid.
1269 ++it;
1270
1271 if (use_instruction->IsConstructorFence()) {
1272 HConstructorFence* ctor_fence = use_instruction->AsConstructorFence();
1273 size_t input_index = use_node.GetIndex();
1274
1275 // Process the candidate instruction for removal
1276 // from the graph.
1277
1278 // Constructor fence instructions are never
1279 // used by other instructions.
1280 //
1281 // If we wanted to make this more generic, it
1282 // could be a runtime if statement.
1283 DCHECK(!ctor_fence->HasUses());
1284
1285 // A constructor fence's return type is "kPrimVoid"
1286 // and therefore it can't have any environment uses.
1287 DCHECK(!ctor_fence->HasEnvironmentUses());
1288
1289 // Remove the inputs first, otherwise removing the instruction
1290 // will try to remove its uses while we are already removing uses
1291 // and this operation will fail.
1292 DCHECK_EQ(instruction, ctor_fence->InputAt(input_index));
1293
1294 // Removing the input will also remove the `use_node`.
1295 // (Do not look at `use_node` after this, it will be a dangling reference).
1296 ctor_fence->RemoveInputAt(input_index);
1297
1298 // Once all inputs are removed, the fence is considered dead and
1299 // is removed.
1300 if (ctor_fence->InputCount() == 0u) {
1301 ctor_fence->GetBlock()->RemoveInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001302 ++remove_count;
Igor Murashkind01745e2017-04-05 16:40:31 -07001303 }
1304 }
1305 }
1306
1307 if (kIsDebugBuild) {
1308 // Post-condition checks:
1309 // * None of the uses of `instruction` are a constructor fence.
1310 // * The `instruction` itself did not get removed from a block.
1311 for (const HUseListNode<HInstruction*>& use_node : instruction->GetUses()) {
1312 CHECK(!use_node.GetUser()->IsConstructorFence());
1313 }
1314 CHECK(instruction->GetBlock() != nullptr);
1315 }
Igor Murashkin6ef45672017-08-08 13:59:55 -07001316
1317 return remove_count;
Igor Murashkind01745e2017-04-05 16:40:31 -07001318}
1319
Igor Murashkindd018df2017-08-09 10:38:31 -07001320void HConstructorFence::Merge(HConstructorFence* other) {
1321 // Do not delete yourself from the graph.
1322 DCHECK(this != other);
1323 // Don't try to merge with an instruction not associated with a block.
1324 DCHECK(other->GetBlock() != nullptr);
1325 // A constructor fence's return type is "kPrimVoid"
1326 // and therefore it cannot have any environment uses.
1327 DCHECK(!other->HasEnvironmentUses());
1328
1329 auto has_input = [](HInstruction* haystack, HInstruction* needle) {
1330 // Check if `haystack` has `needle` as any of its inputs.
1331 for (size_t input_count = 0; input_count < haystack->InputCount(); ++input_count) {
1332 if (haystack->InputAt(input_count) == needle) {
1333 return true;
1334 }
1335 }
1336 return false;
1337 };
1338
1339 // Add any inputs from `other` into `this` if it wasn't already an input.
1340 for (size_t input_count = 0; input_count < other->InputCount(); ++input_count) {
1341 HInstruction* other_input = other->InputAt(input_count);
1342 if (!has_input(this, other_input)) {
1343 AddInput(other_input);
1344 }
1345 }
1346
1347 other->GetBlock()->RemoveInstruction(other);
1348}
1349
1350HInstruction* HConstructorFence::GetAssociatedAllocation(bool ignore_inputs) {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001351 HInstruction* new_instance_inst = GetPrevious();
1352 // Check if the immediately preceding instruction is a new-instance/new-array.
1353 // Otherwise this fence is for protecting final fields.
1354 if (new_instance_inst != nullptr &&
1355 (new_instance_inst->IsNewInstance() || new_instance_inst->IsNewArray())) {
Igor Murashkindd018df2017-08-09 10:38:31 -07001356 if (ignore_inputs) {
1357 // If inputs are ignored, simply check if the predecessor is
1358 // *any* HNewInstance/HNewArray.
1359 //
1360 // Inputs are normally only ignored for prepare_for_register_allocation,
1361 // at which point *any* prior HNewInstance/Array can be considered
1362 // associated.
1363 return new_instance_inst;
1364 } else {
1365 // Normal case: There must be exactly 1 input and the previous instruction
1366 // must be that input.
1367 if (InputCount() == 1u && InputAt(0) == new_instance_inst) {
1368 return new_instance_inst;
1369 }
1370 }
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001371 }
Igor Murashkindd018df2017-08-09 10:38:31 -07001372 return nullptr;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001373}
1374
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001375#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001376void H##name::Accept(HGraphVisitor* visitor) { \
1377 visitor->Visit##name(this); \
1378}
1379
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001380FOR_EACH_CONCRETE_INSTRUCTION(DEFINE_ACCEPT)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001381
1382#undef DEFINE_ACCEPT
1383
1384void HGraphVisitor::VisitInsertionOrder() {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001385 const ArenaVector<HBasicBlock*>& blocks = graph_->GetBlocks();
1386 for (HBasicBlock* block : blocks) {
David Brazdil46e2a392015-03-16 17:31:52 +00001387 if (block != nullptr) {
1388 VisitBasicBlock(block);
1389 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001390 }
1391}
1392
Roland Levillain633021e2014-10-01 14:12:25 +01001393void HGraphVisitor::VisitReversePostOrder() {
Vladimir Marko2c45bc92016-10-25 16:54:12 +01001394 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
1395 VisitBasicBlock(block);
Roland Levillain633021e2014-10-01 14:12:25 +01001396 }
1397}
1398
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001399void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001400 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001401 it.Current()->Accept(this);
1402 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001403 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001404 it.Current()->Accept(this);
1405 }
1406}
1407
Mark Mendelle82549b2015-05-06 10:55:34 -04001408HConstant* HTypeConversion::TryStaticEvaluation() const {
1409 HGraph* graph = GetBlock()->GetGraph();
1410 if (GetInput()->IsIntConstant()) {
1411 int32_t value = GetInput()->AsIntConstant()->GetValue();
1412 switch (GetResultType()) {
Mingyao Yang75bb2f32017-11-30 14:45:44 -08001413 case DataType::Type::kInt8:
1414 return graph->GetIntConstant(static_cast<int8_t>(value), GetDexPc());
1415 case DataType::Type::kUint8:
1416 return graph->GetIntConstant(static_cast<uint8_t>(value), GetDexPc());
1417 case DataType::Type::kInt16:
1418 return graph->GetIntConstant(static_cast<int16_t>(value), GetDexPc());
1419 case DataType::Type::kUint16:
1420 return graph->GetIntConstant(static_cast<uint16_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001421 case DataType::Type::kInt64:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001422 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001423 case DataType::Type::kFloat32:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001424 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001425 case DataType::Type::kFloat64:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001426 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001427 default:
1428 return nullptr;
1429 }
1430 } else if (GetInput()->IsLongConstant()) {
1431 int64_t value = GetInput()->AsLongConstant()->GetValue();
1432 switch (GetResultType()) {
Mingyao Yang75bb2f32017-11-30 14:45:44 -08001433 case DataType::Type::kInt8:
1434 return graph->GetIntConstant(static_cast<int8_t>(value), GetDexPc());
1435 case DataType::Type::kUint8:
1436 return graph->GetIntConstant(static_cast<uint8_t>(value), GetDexPc());
1437 case DataType::Type::kInt16:
1438 return graph->GetIntConstant(static_cast<int16_t>(value), GetDexPc());
1439 case DataType::Type::kUint16:
1440 return graph->GetIntConstant(static_cast<uint16_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001441 case DataType::Type::kInt32:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001442 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001443 case DataType::Type::kFloat32:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001444 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001445 case DataType::Type::kFloat64:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001446 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001447 default:
1448 return nullptr;
1449 }
1450 } else if (GetInput()->IsFloatConstant()) {
1451 float value = GetInput()->AsFloatConstant()->GetValue();
1452 switch (GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001453 case DataType::Type::kInt32:
Mark Mendelle82549b2015-05-06 10:55:34 -04001454 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001455 return graph->GetIntConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001456 if (value >= kPrimIntMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001457 return graph->GetIntConstant(kPrimIntMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001458 if (value <= kPrimIntMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001459 return graph->GetIntConstant(kPrimIntMin, GetDexPc());
1460 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001461 case DataType::Type::kInt64:
Mark Mendelle82549b2015-05-06 10:55:34 -04001462 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001463 return graph->GetLongConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001464 if (value >= kPrimLongMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001465 return graph->GetLongConstant(kPrimLongMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001466 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001467 return graph->GetLongConstant(kPrimLongMin, GetDexPc());
1468 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001469 case DataType::Type::kFloat64:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001470 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001471 default:
1472 return nullptr;
1473 }
1474 } else if (GetInput()->IsDoubleConstant()) {
1475 double value = GetInput()->AsDoubleConstant()->GetValue();
1476 switch (GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001477 case DataType::Type::kInt32:
Mark Mendelle82549b2015-05-06 10:55:34 -04001478 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001479 return graph->GetIntConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001480 if (value >= kPrimIntMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001481 return graph->GetIntConstant(kPrimIntMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001482 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001483 return graph->GetIntConstant(kPrimIntMin, GetDexPc());
1484 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001485 case DataType::Type::kInt64:
Mark Mendelle82549b2015-05-06 10:55:34 -04001486 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001487 return graph->GetLongConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001488 if (value >= kPrimLongMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001489 return graph->GetLongConstant(kPrimLongMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001490 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001491 return graph->GetLongConstant(kPrimLongMin, GetDexPc());
1492 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001493 case DataType::Type::kFloat32:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001494 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001495 default:
1496 return nullptr;
1497 }
1498 }
1499 return nullptr;
1500}
1501
Roland Levillain9240d6a2014-10-20 16:47:04 +01001502HConstant* HUnaryOperation::TryStaticEvaluation() const {
1503 if (GetInput()->IsIntConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001504 return Evaluate(GetInput()->AsIntConstant());
Roland Levillain9240d6a2014-10-20 16:47:04 +01001505 } else if (GetInput()->IsLongConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001506 return Evaluate(GetInput()->AsLongConstant());
Roland Levillain31dd3d62016-02-16 12:21:02 +00001507 } else if (kEnableFloatingPointStaticEvaluation) {
1508 if (GetInput()->IsFloatConstant()) {
1509 return Evaluate(GetInput()->AsFloatConstant());
1510 } else if (GetInput()->IsDoubleConstant()) {
1511 return Evaluate(GetInput()->AsDoubleConstant());
1512 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01001513 }
1514 return nullptr;
1515}
1516
1517HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillaine53bd812016-02-24 14:54:18 +00001518 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
1519 return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsIntConstant());
Roland Levillain9867bc72015-08-05 10:21:34 +01001520 } else if (GetLeft()->IsLongConstant()) {
1521 if (GetRight()->IsIntConstant()) {
Roland Levillaine53bd812016-02-24 14:54:18 +00001522 // The binop(long, int) case is only valid for shifts and rotations.
1523 DCHECK(IsShl() || IsShr() || IsUShr() || IsRor()) << DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01001524 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsIntConstant());
1525 } else if (GetRight()->IsLongConstant()) {
1526 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsLongConstant());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +00001527 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00001528 } else if (GetLeft()->IsNullConstant() && GetRight()->IsNullConstant()) {
Roland Levillaine53bd812016-02-24 14:54:18 +00001529 // The binop(null, null) case is only valid for equal and not-equal conditions.
1530 DCHECK(IsEqual() || IsNotEqual()) << DebugName();
Vladimir Marko9e23df52015-11-10 17:14:35 +00001531 return Evaluate(GetLeft()->AsNullConstant(), GetRight()->AsNullConstant());
Roland Levillain31dd3d62016-02-16 12:21:02 +00001532 } else if (kEnableFloatingPointStaticEvaluation) {
1533 if (GetLeft()->IsFloatConstant() && GetRight()->IsFloatConstant()) {
1534 return Evaluate(GetLeft()->AsFloatConstant(), GetRight()->AsFloatConstant());
1535 } else if (GetLeft()->IsDoubleConstant() && GetRight()->IsDoubleConstant()) {
1536 return Evaluate(GetLeft()->AsDoubleConstant(), GetRight()->AsDoubleConstant());
1537 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001538 }
1539 return nullptr;
1540}
Dave Allison20dfc792014-06-16 20:44:29 -07001541
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001542HConstant* HBinaryOperation::GetConstantRight() const {
1543 if (GetRight()->IsConstant()) {
1544 return GetRight()->AsConstant();
1545 } else if (IsCommutative() && GetLeft()->IsConstant()) {
1546 return GetLeft()->AsConstant();
1547 } else {
1548 return nullptr;
1549 }
1550}
1551
1552// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001553// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001554HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
1555 HInstruction* most_constant_right = GetConstantRight();
1556 if (most_constant_right == nullptr) {
1557 return nullptr;
1558 } else if (most_constant_right == GetLeft()) {
1559 return GetRight();
1560 } else {
1561 return GetLeft();
1562 }
1563}
1564
Roland Levillain31dd3d62016-02-16 12:21:02 +00001565std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs) {
1566 switch (rhs) {
1567 case ComparisonBias::kNoBias:
1568 return os << "no_bias";
1569 case ComparisonBias::kGtBias:
1570 return os << "gt_bias";
1571 case ComparisonBias::kLtBias:
1572 return os << "lt_bias";
1573 default:
1574 LOG(FATAL) << "Unknown ComparisonBias: " << static_cast<int>(rhs);
1575 UNREACHABLE();
1576 }
1577}
1578
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001579bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
1580 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001581}
1582
Vladimir Marko372f10e2016-05-17 16:30:10 +01001583bool HInstruction::Equals(const HInstruction* other) const {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001584 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001585 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001586 if (!InstructionDataEquals(other)) return false;
1587 if (GetType() != other->GetType()) return false;
Vladimir Markoe9004912016-06-16 16:50:52 +01001588 HConstInputsRef inputs = GetInputs();
1589 HConstInputsRef other_inputs = other->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +01001590 if (inputs.size() != other_inputs.size()) return false;
1591 for (size_t i = 0; i != inputs.size(); ++i) {
1592 if (inputs[i] != other_inputs[i]) return false;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001593 }
Vladimir Marko372f10e2016-05-17 16:30:10 +01001594
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001595 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001596 return true;
1597}
1598
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001599std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
1600#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
1601 switch (rhs) {
1602 FOR_EACH_INSTRUCTION(DECLARE_CASE)
1603 default:
1604 os << "Unknown instruction kind " << static_cast<int>(rhs);
1605 break;
1606 }
1607#undef DECLARE_CASE
1608 return os;
1609}
1610
Alexandre Rames22aa54b2016-10-18 09:32:29 +01001611void HInstruction::MoveBefore(HInstruction* cursor, bool do_checks) {
1612 if (do_checks) {
1613 DCHECK(!IsPhi());
1614 DCHECK(!IsControlFlow());
1615 DCHECK(CanBeMoved() ||
1616 // HShouldDeoptimizeFlag can only be moved by CHAGuardOptimization.
1617 IsShouldDeoptimizeFlag());
1618 DCHECK(!cursor->IsPhi());
1619 }
David Brazdild6c205e2016-06-07 14:20:52 +01001620
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001621 next_->previous_ = previous_;
1622 if (previous_ != nullptr) {
1623 previous_->next_ = next_;
1624 }
1625 if (block_->instructions_.first_instruction_ == this) {
1626 block_->instructions_.first_instruction_ = next_;
1627 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001628 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001629
1630 previous_ = cursor->previous_;
1631 if (previous_ != nullptr) {
1632 previous_->next_ = this;
1633 }
1634 next_ = cursor;
1635 cursor->previous_ = this;
1636 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001637
1638 if (block_->instructions_.first_instruction_ == cursor) {
1639 block_->instructions_.first_instruction_ = this;
1640 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001641}
1642
Vladimir Markofb337ea2015-11-25 15:25:10 +00001643void HInstruction::MoveBeforeFirstUserAndOutOfLoops() {
1644 DCHECK(!CanThrow());
1645 DCHECK(!HasSideEffects());
1646 DCHECK(!HasEnvironmentUses());
1647 DCHECK(HasNonEnvironmentUses());
1648 DCHECK(!IsPhi()); // Makes no sense for Phi.
1649 DCHECK_EQ(InputCount(), 0u);
1650
1651 // Find the target block.
Vladimir Marko46817b82016-03-29 12:21:58 +01001652 auto uses_it = GetUses().begin();
1653 auto uses_end = GetUses().end();
1654 HBasicBlock* target_block = uses_it->GetUser()->GetBlock();
1655 ++uses_it;
1656 while (uses_it != uses_end && uses_it->GetUser()->GetBlock() == target_block) {
1657 ++uses_it;
Vladimir Markofb337ea2015-11-25 15:25:10 +00001658 }
Vladimir Marko46817b82016-03-29 12:21:58 +01001659 if (uses_it != uses_end) {
Vladimir Markofb337ea2015-11-25 15:25:10 +00001660 // This instruction has uses in two or more blocks. Find the common dominator.
1661 CommonDominator finder(target_block);
Vladimir Marko46817b82016-03-29 12:21:58 +01001662 for (; uses_it != uses_end; ++uses_it) {
1663 finder.Update(uses_it->GetUser()->GetBlock());
Vladimir Markofb337ea2015-11-25 15:25:10 +00001664 }
1665 target_block = finder.Get();
1666 DCHECK(target_block != nullptr);
1667 }
1668 // Move to the first dominator not in a loop.
1669 while (target_block->IsInLoop()) {
1670 target_block = target_block->GetDominator();
1671 DCHECK(target_block != nullptr);
1672 }
1673
1674 // Find insertion position.
1675 HInstruction* insert_pos = nullptr;
Vladimir Marko46817b82016-03-29 12:21:58 +01001676 for (const HUseListNode<HInstruction*>& use : GetUses()) {
1677 if (use.GetUser()->GetBlock() == target_block &&
1678 (insert_pos == nullptr || use.GetUser()->StrictlyDominates(insert_pos))) {
1679 insert_pos = use.GetUser();
Vladimir Markofb337ea2015-11-25 15:25:10 +00001680 }
1681 }
1682 if (insert_pos == nullptr) {
1683 // No user in `target_block`, insert before the control flow instruction.
1684 insert_pos = target_block->GetLastInstruction();
1685 DCHECK(insert_pos->IsControlFlow());
1686 // Avoid splitting HCondition from HIf to prevent unnecessary materialization.
1687 if (insert_pos->IsIf()) {
1688 HInstruction* if_input = insert_pos->AsIf()->InputAt(0);
1689 if (if_input == insert_pos->GetPrevious()) {
1690 insert_pos = if_input;
1691 }
1692 }
1693 }
1694 MoveBefore(insert_pos);
1695}
1696
David Brazdilfc6a86a2015-06-26 10:33:45 +00001697HBasicBlock* HBasicBlock::SplitBefore(HInstruction* cursor) {
David Brazdil9bc43612015-11-05 21:25:24 +00001698 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented.";
David Brazdilfc6a86a2015-06-26 10:33:45 +00001699 DCHECK_EQ(cursor->GetBlock(), this);
1700
Vladimir Markoca6fff82017-10-03 14:49:14 +01001701 HBasicBlock* new_block =
1702 new (GetGraph()->GetAllocator()) HBasicBlock(GetGraph(), cursor->GetDexPc());
David Brazdilfc6a86a2015-06-26 10:33:45 +00001703 new_block->instructions_.first_instruction_ = cursor;
1704 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1705 instructions_.last_instruction_ = cursor->previous_;
1706 if (cursor->previous_ == nullptr) {
1707 instructions_.first_instruction_ = nullptr;
1708 } else {
1709 cursor->previous_->next_ = nullptr;
1710 cursor->previous_ = nullptr;
1711 }
1712
1713 new_block->instructions_.SetBlockOfInstructions(new_block);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001714 AddInstruction(new (GetGraph()->GetAllocator()) HGoto(new_block->GetDexPc()));
David Brazdilfc6a86a2015-06-26 10:33:45 +00001715
Vladimir Marko60584552015-09-03 13:35:12 +00001716 for (HBasicBlock* successor : GetSuccessors()) {
Vladimir Marko60584552015-09-03 13:35:12 +00001717 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001718 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001719 new_block->successors_.swap(successors_);
1720 DCHECK(successors_.empty());
David Brazdilfc6a86a2015-06-26 10:33:45 +00001721 AddSuccessor(new_block);
1722
David Brazdil56e1acc2015-06-30 15:41:36 +01001723 GetGraph()->AddBlock(new_block);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001724 return new_block;
1725}
1726
David Brazdild7558da2015-09-22 13:04:14 +01001727HBasicBlock* HBasicBlock::CreateImmediateDominator() {
David Brazdil9bc43612015-11-05 21:25:24 +00001728 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented.";
David Brazdild7558da2015-09-22 13:04:14 +01001729 DCHECK(!IsCatchBlock()) << "Support for updating try/catch information not implemented.";
1730
Vladimir Markoca6fff82017-10-03 14:49:14 +01001731 HBasicBlock* new_block = new (GetGraph()->GetAllocator()) HBasicBlock(GetGraph(), GetDexPc());
David Brazdild7558da2015-09-22 13:04:14 +01001732
1733 for (HBasicBlock* predecessor : GetPredecessors()) {
David Brazdild7558da2015-09-22 13:04:14 +01001734 predecessor->successors_[predecessor->GetSuccessorIndexOf(this)] = new_block;
1735 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001736 new_block->predecessors_.swap(predecessors_);
1737 DCHECK(predecessors_.empty());
David Brazdild7558da2015-09-22 13:04:14 +01001738 AddPredecessor(new_block);
1739
1740 GetGraph()->AddBlock(new_block);
1741 return new_block;
1742}
1743
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001744HBasicBlock* HBasicBlock::SplitBeforeForInlining(HInstruction* cursor) {
1745 DCHECK_EQ(cursor->GetBlock(), this);
1746
Vladimir Markoca6fff82017-10-03 14:49:14 +01001747 HBasicBlock* new_block =
1748 new (GetGraph()->GetAllocator()) HBasicBlock(GetGraph(), cursor->GetDexPc());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001749 new_block->instructions_.first_instruction_ = cursor;
1750 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1751 instructions_.last_instruction_ = cursor->previous_;
1752 if (cursor->previous_ == nullptr) {
1753 instructions_.first_instruction_ = nullptr;
1754 } else {
1755 cursor->previous_->next_ = nullptr;
1756 cursor->previous_ = nullptr;
1757 }
1758
1759 new_block->instructions_.SetBlockOfInstructions(new_block);
1760
1761 for (HBasicBlock* successor : GetSuccessors()) {
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001762 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
1763 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001764 new_block->successors_.swap(successors_);
1765 DCHECK(successors_.empty());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001766
1767 for (HBasicBlock* dominated : GetDominatedBlocks()) {
1768 dominated->dominator_ = new_block;
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001769 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001770 new_block->dominated_blocks_.swap(dominated_blocks_);
1771 DCHECK(dominated_blocks_.empty());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001772 return new_block;
1773}
1774
1775HBasicBlock* HBasicBlock::SplitAfterForInlining(HInstruction* cursor) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001776 DCHECK(!cursor->IsControlFlow());
1777 DCHECK_NE(instructions_.last_instruction_, cursor);
1778 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001779
Vladimir Markoca6fff82017-10-03 14:49:14 +01001780 HBasicBlock* new_block = new (GetGraph()->GetAllocator()) HBasicBlock(GetGraph(), GetDexPc());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001781 new_block->instructions_.first_instruction_ = cursor->GetNext();
1782 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1783 cursor->next_->previous_ = nullptr;
1784 cursor->next_ = nullptr;
1785 instructions_.last_instruction_ = cursor;
1786
1787 new_block->instructions_.SetBlockOfInstructions(new_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001788 for (HBasicBlock* successor : GetSuccessors()) {
Vladimir Marko60584552015-09-03 13:35:12 +00001789 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001790 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001791 new_block->successors_.swap(successors_);
1792 DCHECK(successors_.empty());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001793
Vladimir Marko60584552015-09-03 13:35:12 +00001794 for (HBasicBlock* dominated : GetDominatedBlocks()) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001795 dominated->dominator_ = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001796 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001797 new_block->dominated_blocks_.swap(dominated_blocks_);
1798 DCHECK(dominated_blocks_.empty());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001799 return new_block;
1800}
1801
David Brazdilec16f792015-08-19 15:04:01 +01001802const HTryBoundary* HBasicBlock::ComputeTryEntryOfSuccessors() const {
David Brazdilffee3d32015-07-06 11:48:53 +01001803 if (EndsWithTryBoundary()) {
1804 HTryBoundary* try_boundary = GetLastInstruction()->AsTryBoundary();
1805 if (try_boundary->IsEntry()) {
David Brazdilec16f792015-08-19 15:04:01 +01001806 DCHECK(!IsTryBlock());
David Brazdilffee3d32015-07-06 11:48:53 +01001807 return try_boundary;
1808 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001809 DCHECK(IsTryBlock());
1810 DCHECK(try_catch_information_->GetTryEntry().HasSameExceptionHandlersAs(*try_boundary));
David Brazdilffee3d32015-07-06 11:48:53 +01001811 return nullptr;
1812 }
David Brazdilec16f792015-08-19 15:04:01 +01001813 } else if (IsTryBlock()) {
1814 return &try_catch_information_->GetTryEntry();
David Brazdilffee3d32015-07-06 11:48:53 +01001815 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001816 return nullptr;
David Brazdilffee3d32015-07-06 11:48:53 +01001817 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00001818}
1819
David Brazdild7558da2015-09-22 13:04:14 +01001820bool HBasicBlock::HasThrowingInstructions() const {
1821 for (HInstructionIterator it(GetInstructions()); !it.Done(); it.Advance()) {
1822 if (it.Current()->CanThrow()) {
1823 return true;
1824 }
1825 }
1826 return false;
1827}
1828
David Brazdilfc6a86a2015-06-26 10:33:45 +00001829static bool HasOnlyOneInstruction(const HBasicBlock& block) {
1830 return block.GetPhis().IsEmpty()
1831 && !block.GetInstructions().IsEmpty()
1832 && block.GetFirstInstruction() == block.GetLastInstruction();
1833}
1834
David Brazdil46e2a392015-03-16 17:31:52 +00001835bool HBasicBlock::IsSingleGoto() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001836 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsGoto();
1837}
1838
Mads Ager16e52892017-07-14 13:11:37 +02001839bool HBasicBlock::IsSingleReturn() const {
1840 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsReturn();
1841}
1842
Mingyao Yang46721ef2017-10-05 14:45:17 -07001843bool HBasicBlock::IsSingleReturnOrReturnVoidAllowingPhis() const {
1844 return (GetFirstInstruction() == GetLastInstruction()) &&
1845 (GetLastInstruction()->IsReturn() || GetLastInstruction()->IsReturnVoid());
1846}
1847
David Brazdilfc6a86a2015-06-26 10:33:45 +00001848bool HBasicBlock::IsSingleTryBoundary() const {
1849 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsTryBoundary();
David Brazdil46e2a392015-03-16 17:31:52 +00001850}
1851
David Brazdil8d5b8b22015-03-24 10:51:52 +00001852bool HBasicBlock::EndsWithControlFlowInstruction() const {
1853 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
1854}
1855
David Brazdilb2bd1c52015-03-25 11:17:37 +00001856bool HBasicBlock::EndsWithIf() const {
1857 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
1858}
1859
David Brazdilffee3d32015-07-06 11:48:53 +01001860bool HBasicBlock::EndsWithTryBoundary() const {
1861 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsTryBoundary();
1862}
1863
David Brazdilb2bd1c52015-03-25 11:17:37 +00001864bool HBasicBlock::HasSinglePhi() const {
1865 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
1866}
1867
David Brazdild26a4112015-11-10 11:07:31 +00001868ArrayRef<HBasicBlock* const> HBasicBlock::GetNormalSuccessors() const {
1869 if (EndsWithTryBoundary()) {
1870 // The normal-flow successor of HTryBoundary is always stored at index zero.
1871 DCHECK_EQ(successors_[0], GetLastInstruction()->AsTryBoundary()->GetNormalFlowSuccessor());
1872 return ArrayRef<HBasicBlock* const>(successors_).SubArray(0u, 1u);
1873 } else {
1874 // All successors of blocks not ending with TryBoundary are normal.
1875 return ArrayRef<HBasicBlock* const>(successors_);
1876 }
1877}
1878
1879ArrayRef<HBasicBlock* const> HBasicBlock::GetExceptionalSuccessors() const {
1880 if (EndsWithTryBoundary()) {
1881 return GetLastInstruction()->AsTryBoundary()->GetExceptionHandlers();
1882 } else {
1883 // Blocks not ending with TryBoundary do not have exceptional successors.
1884 return ArrayRef<HBasicBlock* const>();
1885 }
1886}
1887
David Brazdilffee3d32015-07-06 11:48:53 +01001888bool HTryBoundary::HasSameExceptionHandlersAs(const HTryBoundary& other) const {
David Brazdild26a4112015-11-10 11:07:31 +00001889 ArrayRef<HBasicBlock* const> handlers1 = GetExceptionHandlers();
1890 ArrayRef<HBasicBlock* const> handlers2 = other.GetExceptionHandlers();
1891
1892 size_t length = handlers1.size();
1893 if (length != handlers2.size()) {
David Brazdilffee3d32015-07-06 11:48:53 +01001894 return false;
1895 }
1896
David Brazdilb618ade2015-07-29 10:31:29 +01001897 // Exception handlers need to be stored in the same order.
David Brazdild26a4112015-11-10 11:07:31 +00001898 for (size_t i = 0; i < length; ++i) {
1899 if (handlers1[i] != handlers2[i]) {
David Brazdilffee3d32015-07-06 11:48:53 +01001900 return false;
1901 }
1902 }
1903 return true;
1904}
1905
David Brazdil2d7352b2015-04-20 14:52:42 +01001906size_t HInstructionList::CountSize() const {
1907 size_t size = 0;
1908 HInstruction* current = first_instruction_;
1909 for (; current != nullptr; current = current->GetNext()) {
1910 size++;
1911 }
1912 return size;
1913}
1914
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001915void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
1916 for (HInstruction* current = first_instruction_;
1917 current != nullptr;
1918 current = current->GetNext()) {
1919 current->SetBlock(block);
1920 }
1921}
1922
1923void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
1924 DCHECK(Contains(cursor));
1925 if (!instruction_list.IsEmpty()) {
1926 if (cursor == last_instruction_) {
1927 last_instruction_ = instruction_list.last_instruction_;
1928 } else {
1929 cursor->next_->previous_ = instruction_list.last_instruction_;
1930 }
1931 instruction_list.last_instruction_->next_ = cursor->next_;
1932 cursor->next_ = instruction_list.first_instruction_;
1933 instruction_list.first_instruction_->previous_ = cursor;
1934 }
1935}
1936
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001937void HInstructionList::AddBefore(HInstruction* cursor, const HInstructionList& instruction_list) {
1938 DCHECK(Contains(cursor));
1939 if (!instruction_list.IsEmpty()) {
1940 if (cursor == first_instruction_) {
1941 first_instruction_ = instruction_list.first_instruction_;
1942 } else {
1943 cursor->previous_->next_ = instruction_list.first_instruction_;
1944 }
1945 instruction_list.last_instruction_->next_ = cursor;
1946 instruction_list.first_instruction_->previous_ = cursor->previous_;
1947 cursor->previous_ = instruction_list.last_instruction_;
1948 }
1949}
1950
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001951void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +00001952 if (IsEmpty()) {
1953 first_instruction_ = instruction_list.first_instruction_;
1954 last_instruction_ = instruction_list.last_instruction_;
1955 } else {
1956 AddAfter(last_instruction_, instruction_list);
1957 }
1958}
1959
David Brazdil04ff4e82015-12-10 13:54:52 +00001960// Should be called on instructions in a dead block in post order. This method
1961// assumes `insn` has been removed from all users with the exception of catch
1962// phis because of missing exceptional edges in the graph. It removes the
1963// instruction from catch phi uses, together with inputs of other catch phis in
1964// the catch block at the same index, as these must be dead too.
1965static void RemoveUsesOfDeadInstruction(HInstruction* insn) {
1966 DCHECK(!insn->HasEnvironmentUses());
1967 while (insn->HasNonEnvironmentUses()) {
Vladimir Marko46817b82016-03-29 12:21:58 +01001968 const HUseListNode<HInstruction*>& use = insn->GetUses().front();
1969 size_t use_index = use.GetIndex();
1970 HBasicBlock* user_block = use.GetUser()->GetBlock();
1971 DCHECK(use.GetUser()->IsPhi() && user_block->IsCatchBlock());
David Brazdil04ff4e82015-12-10 13:54:52 +00001972 for (HInstructionIterator phi_it(user_block->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1973 phi_it.Current()->AsPhi()->RemoveInputAt(use_index);
1974 }
1975 }
1976}
1977
David Brazdil2d7352b2015-04-20 14:52:42 +01001978void HBasicBlock::DisconnectAndDelete() {
1979 // Dominators must be removed after all the blocks they dominate. This way
1980 // a loop header is removed last, a requirement for correct loop information
1981 // iteration.
Vladimir Marko60584552015-09-03 13:35:12 +00001982 DCHECK(dominated_blocks_.empty());
David Brazdil46e2a392015-03-16 17:31:52 +00001983
David Brazdil9eeebf62016-03-24 11:18:15 +00001984 // The following steps gradually remove the block from all its dependants in
1985 // post order (b/27683071).
1986
1987 // (1) Store a basic block that we'll use in step (5) to find loops to be updated.
1988 // We need to do this before step (4) which destroys the predecessor list.
1989 HBasicBlock* loop_update_start = this;
1990 if (IsLoopHeader()) {
1991 HLoopInformation* loop_info = GetLoopInformation();
1992 // All other blocks in this loop should have been removed because the header
1993 // was their dominator.
1994 // Note that we do not remove `this` from `loop_info` as it is unreachable.
1995 DCHECK(!loop_info->IsIrreducible());
1996 DCHECK_EQ(loop_info->GetBlocks().NumSetBits(), 1u);
1997 DCHECK_EQ(static_cast<uint32_t>(loop_info->GetBlocks().GetHighestBitSet()), GetBlockId());
1998 loop_update_start = loop_info->GetPreHeader();
David Brazdil2d7352b2015-04-20 14:52:42 +01001999 }
2000
David Brazdil9eeebf62016-03-24 11:18:15 +00002001 // (2) Disconnect the block from its successors and update their phis.
2002 for (HBasicBlock* successor : successors_) {
2003 // Delete this block from the list of predecessors.
2004 size_t this_index = successor->GetPredecessorIndexOf(this);
2005 successor->predecessors_.erase(successor->predecessors_.begin() + this_index);
2006
2007 // Check that `successor` has other predecessors, otherwise `this` is the
2008 // dominator of `successor` which violates the order DCHECKed at the top.
2009 DCHECK(!successor->predecessors_.empty());
2010
2011 // Remove this block's entries in the successor's phis. Skip exceptional
2012 // successors because catch phi inputs do not correspond to predecessor
2013 // blocks but throwing instructions. The inputs of the catch phis will be
2014 // updated in step (3).
2015 if (!successor->IsCatchBlock()) {
2016 if (successor->predecessors_.size() == 1u) {
2017 // The successor has just one predecessor left. Replace phis with the only
2018 // remaining input.
2019 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
2020 HPhi* phi = phi_it.Current()->AsPhi();
2021 phi->ReplaceWith(phi->InputAt(1 - this_index));
2022 successor->RemovePhi(phi);
2023 }
2024 } else {
2025 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
2026 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
2027 }
2028 }
2029 }
2030 }
2031 successors_.clear();
2032
2033 // (3) Remove instructions and phis. Instructions should have no remaining uses
2034 // except in catch phis. If an instruction is used by a catch phi at `index`,
2035 // remove `index`-th input of all phis in the catch block since they are
2036 // guaranteed dead. Note that we may miss dead inputs this way but the
2037 // graph will always remain consistent.
2038 for (HBackwardInstructionIterator it(GetInstructions()); !it.Done(); it.Advance()) {
2039 HInstruction* insn = it.Current();
2040 RemoveUsesOfDeadInstruction(insn);
2041 RemoveInstruction(insn);
2042 }
2043 for (HInstructionIterator it(GetPhis()); !it.Done(); it.Advance()) {
2044 HPhi* insn = it.Current()->AsPhi();
2045 RemoveUsesOfDeadInstruction(insn);
2046 RemovePhi(insn);
2047 }
2048
2049 // (4) Disconnect the block from its predecessors and update their
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002050 // control-flow instructions.
Vladimir Marko60584552015-09-03 13:35:12 +00002051 for (HBasicBlock* predecessor : predecessors_) {
David Brazdil9eeebf62016-03-24 11:18:15 +00002052 // We should not see any back edges as they would have been removed by step (3).
2053 DCHECK(!IsInLoop() || !GetLoopInformation()->IsBackEdge(*predecessor));
2054
David Brazdil2d7352b2015-04-20 14:52:42 +01002055 HInstruction* last_instruction = predecessor->GetLastInstruction();
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002056 if (last_instruction->IsTryBoundary() && !IsCatchBlock()) {
2057 // This block is the only normal-flow successor of the TryBoundary which
2058 // makes `predecessor` dead. Since DCE removes blocks in post order,
2059 // exception handlers of this TryBoundary were already visited and any
2060 // remaining handlers therefore must be live. We remove `predecessor` from
2061 // their list of predecessors.
2062 DCHECK_EQ(last_instruction->AsTryBoundary()->GetNormalFlowSuccessor(), this);
2063 while (predecessor->GetSuccessors().size() > 1) {
2064 HBasicBlock* handler = predecessor->GetSuccessors()[1];
2065 DCHECK(handler->IsCatchBlock());
2066 predecessor->RemoveSuccessor(handler);
2067 handler->RemovePredecessor(predecessor);
2068 }
2069 }
2070
David Brazdil2d7352b2015-04-20 14:52:42 +01002071 predecessor->RemoveSuccessor(this);
Mark Mendellfe57faa2015-09-18 09:26:15 -04002072 uint32_t num_pred_successors = predecessor->GetSuccessors().size();
2073 if (num_pred_successors == 1u) {
2074 // If we have one successor after removing one, then we must have
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002075 // had an HIf, HPackedSwitch or HTryBoundary, as they have more than one
2076 // successor. Replace those with a HGoto.
2077 DCHECK(last_instruction->IsIf() ||
2078 last_instruction->IsPackedSwitch() ||
2079 (last_instruction->IsTryBoundary() && IsCatchBlock()));
Mark Mendellfe57faa2015-09-18 09:26:15 -04002080 predecessor->RemoveInstruction(last_instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002081 predecessor->AddInstruction(new (graph_->GetAllocator()) HGoto(last_instruction->GetDexPc()));
Mark Mendellfe57faa2015-09-18 09:26:15 -04002082 } else if (num_pred_successors == 0u) {
David Brazdil2d7352b2015-04-20 14:52:42 +01002083 // The predecessor has no remaining successors and therefore must be dead.
2084 // We deliberately leave it without a control-flow instruction so that the
David Brazdilbadd8262016-02-02 16:28:56 +00002085 // GraphChecker fails unless it is not removed during the pass too.
Mark Mendellfe57faa2015-09-18 09:26:15 -04002086 predecessor->RemoveInstruction(last_instruction);
2087 } else {
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002088 // There are multiple successors left. The removed block might be a successor
2089 // of a PackedSwitch which will be completely removed (perhaps replaced with
2090 // a Goto), or we are deleting a catch block from a TryBoundary. In either
2091 // case, leave `last_instruction` as is for now.
2092 DCHECK(last_instruction->IsPackedSwitch() ||
2093 (last_instruction->IsTryBoundary() && IsCatchBlock()));
David Brazdil2d7352b2015-04-20 14:52:42 +01002094 }
David Brazdil46e2a392015-03-16 17:31:52 +00002095 }
Vladimir Marko60584552015-09-03 13:35:12 +00002096 predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01002097
David Brazdil9eeebf62016-03-24 11:18:15 +00002098 // (5) Remove the block from all loops it is included in. Skip the inner-most
2099 // loop if this is the loop header (see definition of `loop_update_start`)
2100 // because the loop header's predecessor list has been destroyed in step (4).
2101 for (HLoopInformationOutwardIterator it(*loop_update_start); !it.Done(); it.Advance()) {
2102 HLoopInformation* loop_info = it.Current();
2103 loop_info->Remove(this);
2104 if (loop_info->IsBackEdge(*this)) {
2105 // If this was the last back edge of the loop, we deliberately leave the
2106 // loop in an inconsistent state and will fail GraphChecker unless the
2107 // entire loop is removed during the pass.
2108 loop_info->RemoveBackEdge(this);
David Brazdil2d7352b2015-04-20 14:52:42 +01002109 }
2110 }
David Brazdil2d7352b2015-04-20 14:52:42 +01002111
David Brazdil9eeebf62016-03-24 11:18:15 +00002112 // (6) Disconnect from the dominator.
David Brazdil2d7352b2015-04-20 14:52:42 +01002113 dominator_->RemoveDominatedBlock(this);
2114 SetDominator(nullptr);
2115
David Brazdil9eeebf62016-03-24 11:18:15 +00002116 // (7) Delete from the graph, update reverse post order.
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002117 graph_->DeleteDeadEmptyBlock(this);
David Brazdil2d7352b2015-04-20 14:52:42 +01002118 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002119}
2120
Aart Bik6b69e0a2017-01-11 10:20:43 -08002121void HBasicBlock::MergeInstructionsWith(HBasicBlock* other) {
2122 DCHECK(EndsWithControlFlowInstruction());
2123 RemoveInstruction(GetLastInstruction());
2124 instructions_.Add(other->GetInstructions());
2125 other->instructions_.SetBlockOfInstructions(this);
2126 other->instructions_.Clear();
2127}
2128
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002129void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01002130 DCHECK_EQ(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00002131 DCHECK(ContainsElement(dominated_blocks_, other));
2132 DCHECK_EQ(GetSingleSuccessor(), other);
2133 DCHECK_EQ(other->GetSinglePredecessor(), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002134 DCHECK(other->GetPhis().IsEmpty());
2135
David Brazdil2d7352b2015-04-20 14:52:42 +01002136 // Move instructions from `other` to `this`.
Aart Bik6b69e0a2017-01-11 10:20:43 -08002137 MergeInstructionsWith(other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002138
David Brazdil2d7352b2015-04-20 14:52:42 +01002139 // Remove `other` from the loops it is included in.
2140 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
2141 HLoopInformation* loop_info = it.Current();
2142 loop_info->Remove(other);
2143 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002144 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01002145 }
2146 }
2147
2148 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00002149 successors_.clear();
Vladimir Marko661b69b2016-11-09 14:11:37 +00002150 for (HBasicBlock* successor : other->GetSuccessors()) {
2151 successor->predecessors_[successor->GetPredecessorIndexOf(other)] = this;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002152 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00002153 successors_.swap(other->successors_);
2154 DCHECK(other->successors_.empty());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002155
David Brazdil2d7352b2015-04-20 14:52:42 +01002156 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00002157 RemoveDominatedBlock(other);
2158 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
David Brazdil2d7352b2015-04-20 14:52:42 +01002159 dominated->SetDominator(this);
2160 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00002161 dominated_blocks_.insert(
2162 dominated_blocks_.end(), other->dominated_blocks_.begin(), other->dominated_blocks_.end());
Vladimir Marko60584552015-09-03 13:35:12 +00002163 other->dominated_blocks_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01002164 other->dominator_ = nullptr;
2165
2166 // Clear the list of predecessors of `other` in preparation of deleting it.
Vladimir Marko60584552015-09-03 13:35:12 +00002167 other->predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01002168
2169 // Delete `other` from the graph. The function updates reverse post order.
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002170 graph_->DeleteDeadEmptyBlock(other);
David Brazdil2d7352b2015-04-20 14:52:42 +01002171 other->SetGraph(nullptr);
2172}
2173
2174void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
2175 DCHECK_NE(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00002176 DCHECK(GetDominatedBlocks().empty());
2177 DCHECK(GetSuccessors().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01002178 DCHECK(!EndsWithControlFlowInstruction());
Vladimir Marko60584552015-09-03 13:35:12 +00002179 DCHECK(other->GetSinglePredecessor()->IsEntryBlock());
David Brazdil2d7352b2015-04-20 14:52:42 +01002180 DCHECK(other->GetPhis().IsEmpty());
2181 DCHECK(!other->IsInLoop());
2182
2183 // Move instructions from `other` to `this`.
2184 instructions_.Add(other->GetInstructions());
2185 other->instructions_.SetBlockOfInstructions(this);
2186
2187 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00002188 successors_.clear();
Vladimir Marko661b69b2016-11-09 14:11:37 +00002189 for (HBasicBlock* successor : other->GetSuccessors()) {
2190 successor->predecessors_[successor->GetPredecessorIndexOf(other)] = this;
David Brazdil2d7352b2015-04-20 14:52:42 +01002191 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00002192 successors_.swap(other->successors_);
2193 DCHECK(other->successors_.empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01002194
2195 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00002196 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002197 dominated->SetDominator(this);
2198 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00002199 dominated_blocks_.insert(
2200 dominated_blocks_.end(), other->dominated_blocks_.begin(), other->dominated_blocks_.end());
Vladimir Marko60584552015-09-03 13:35:12 +00002201 other->dominated_blocks_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002202 other->dominator_ = nullptr;
2203 other->graph_ = nullptr;
2204}
2205
2206void HBasicBlock::ReplaceWith(HBasicBlock* other) {
Vladimir Marko60584552015-09-03 13:35:12 +00002207 while (!GetPredecessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002208 HBasicBlock* predecessor = GetPredecessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002209 predecessor->ReplaceSuccessor(this, other);
2210 }
Vladimir Marko60584552015-09-03 13:35:12 +00002211 while (!GetSuccessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002212 HBasicBlock* successor = GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002213 successor->ReplacePredecessor(this, other);
2214 }
Vladimir Marko60584552015-09-03 13:35:12 +00002215 for (HBasicBlock* dominated : GetDominatedBlocks()) {
2216 other->AddDominatedBlock(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002217 }
2218 GetDominator()->ReplaceDominatedBlock(this, other);
2219 other->SetDominator(GetDominator());
2220 dominator_ = nullptr;
2221 graph_ = nullptr;
2222}
2223
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002224void HGraph::DeleteDeadEmptyBlock(HBasicBlock* block) {
David Brazdil2d7352b2015-04-20 14:52:42 +01002225 DCHECK_EQ(block->GetGraph(), this);
Vladimir Marko60584552015-09-03 13:35:12 +00002226 DCHECK(block->GetSuccessors().empty());
2227 DCHECK(block->GetPredecessors().empty());
2228 DCHECK(block->GetDominatedBlocks().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01002229 DCHECK(block->GetDominator() == nullptr);
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002230 DCHECK(block->GetInstructions().IsEmpty());
2231 DCHECK(block->GetPhis().IsEmpty());
David Brazdil2d7352b2015-04-20 14:52:42 +01002232
David Brazdilc7af85d2015-05-26 12:05:55 +01002233 if (block->IsExitBlock()) {
Serguei Katkov7ba99662016-03-02 16:25:36 +06002234 SetExitBlock(nullptr);
David Brazdilc7af85d2015-05-26 12:05:55 +01002235 }
2236
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002237 RemoveElement(reverse_post_order_, block);
2238 blocks_[block->GetBlockId()] = nullptr;
David Brazdil86ea7ee2016-02-16 09:26:07 +00002239 block->SetGraph(nullptr);
David Brazdil2d7352b2015-04-20 14:52:42 +01002240}
2241
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +00002242void HGraph::UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
2243 HBasicBlock* reference,
2244 bool replace_if_back_edge) {
2245 if (block->IsLoopHeader()) {
2246 // Clear the information of which blocks are contained in that loop. Since the
2247 // information is stored as a bit vector based on block ids, we have to update
2248 // it, as those block ids were specific to the callee graph and we are now adding
2249 // these blocks to the caller graph.
2250 block->GetLoopInformation()->ClearAllBlocks();
2251 }
2252
2253 // If not already in a loop, update the loop information.
2254 if (!block->IsInLoop()) {
2255 block->SetLoopInformation(reference->GetLoopInformation());
2256 }
2257
2258 // If the block is in a loop, update all its outward loops.
2259 HLoopInformation* loop_info = block->GetLoopInformation();
2260 if (loop_info != nullptr) {
2261 for (HLoopInformationOutwardIterator loop_it(*block);
2262 !loop_it.Done();
2263 loop_it.Advance()) {
2264 loop_it.Current()->Add(block);
2265 }
2266 if (replace_if_back_edge && loop_info->IsBackEdge(*reference)) {
2267 loop_info->ReplaceBackEdge(reference, block);
2268 }
2269 }
2270
2271 // Copy TryCatchInformation if `reference` is a try block, not if it is a catch block.
2272 TryCatchInformation* try_catch_info = reference->IsTryBlock()
2273 ? reference->GetTryCatchInformation()
2274 : nullptr;
2275 block->SetTryCatchInformation(try_catch_info);
2276}
2277
Calin Juravle2e768302015-07-28 14:41:11 +00002278HInstruction* HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
David Brazdilc7af85d2015-05-26 12:05:55 +01002279 DCHECK(HasExitBlock()) << "Unimplemented scenario";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002280 // Update the environments in this graph to have the invoke's environment
2281 // as parent.
2282 {
Vladimir Marko2c45bc92016-10-25 16:54:12 +01002283 // Skip the entry block, we do not need to update the entry's suspend check.
2284 for (HBasicBlock* block : GetReversePostOrderSkipEntryBlock()) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002285 for (HInstructionIterator instr_it(block->GetInstructions());
2286 !instr_it.Done();
2287 instr_it.Advance()) {
2288 HInstruction* current = instr_it.Current();
2289 if (current->NeedsEnvironment()) {
David Brazdildee58d62016-04-07 09:54:26 +00002290 DCHECK(current->HasEnvironment());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002291 current->GetEnvironment()->SetAndCopyParentChain(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002292 outer_graph->GetAllocator(), invoke->GetEnvironment());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002293 }
2294 }
2295 }
2296 }
2297 outer_graph->UpdateMaximumNumberOfOutVRegs(GetMaximumNumberOfOutVRegs());
Mingyao Yang69d75ff2017-02-07 13:06:06 -08002298
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002299 if (HasBoundsChecks()) {
2300 outer_graph->SetHasBoundsChecks(true);
2301 }
Mingyao Yang69d75ff2017-02-07 13:06:06 -08002302 if (HasLoops()) {
2303 outer_graph->SetHasLoops(true);
2304 }
2305 if (HasIrreducibleLoops()) {
2306 outer_graph->SetHasIrreducibleLoops(true);
2307 }
2308 if (HasTryCatch()) {
2309 outer_graph->SetHasTryCatch(true);
2310 }
Aart Bikb13c65b2017-03-21 20:14:07 -07002311 if (HasSIMD()) {
2312 outer_graph->SetHasSIMD(true);
2313 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002314
Calin Juravle2e768302015-07-28 14:41:11 +00002315 HInstruction* return_value = nullptr;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002316 if (GetBlocks().size() == 3) {
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002317 // Inliner already made sure we don't inline methods that always throw.
2318 DCHECK(!GetBlocks()[1]->GetLastInstruction()->IsThrow());
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00002319 // Simple case of an entry block, a body block, and an exit block.
2320 // Put the body block's instruction into `invoke`'s block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002321 HBasicBlock* body = GetBlocks()[1];
2322 DCHECK(GetBlocks()[0]->IsEntryBlock());
2323 DCHECK(GetBlocks()[2]->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002324 DCHECK(!body->IsExitBlock());
Nicolas Geoffray788f2f02016-01-22 12:41:38 +00002325 DCHECK(!body->IsInLoop());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002326 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002327
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00002328 // Note that we add instructions before the invoke only to simplify polymorphic inlining.
2329 invoke->GetBlock()->instructions_.AddBefore(invoke, body->GetInstructions());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002330 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002331
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002332 // Replace the invoke with the return value of the inlined graph.
2333 if (last->IsReturn()) {
Calin Juravle2e768302015-07-28 14:41:11 +00002334 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002335 } else {
2336 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002337 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002338
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002339 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002340 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002341 // Need to inline multiple blocks. We split `invoke`'s block
2342 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00002343 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002344 // with the second half.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002345 ArenaAllocator* allocator = outer_graph->GetAllocator();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002346 HBasicBlock* at = invoke->GetBlock();
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00002347 // Note that we split before the invoke only to simplify polymorphic inlining.
2348 HBasicBlock* to = at->SplitBeforeForInlining(invoke);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002349
Vladimir Markoec7802a2015-10-01 20:57:57 +01002350 HBasicBlock* first = entry_block_->GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002351 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01002352 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002353 exit_block_->ReplaceWith(to);
2354
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002355 // Update the meta information surrounding blocks:
2356 // (1) the graph they are now in,
2357 // (2) the reverse post order of that graph,
Nicolas Geoffray788f2f02016-01-22 12:41:38 +00002358 // (3) their potential loop information, inner and outer,
David Brazdil95177982015-10-30 12:56:58 -05002359 // (4) try block membership.
David Brazdil59a850e2015-11-10 13:04:30 +00002360 // Note that we do not need to update catch phi inputs because they
2361 // correspond to the register file of the outer method which the inlinee
2362 // cannot modify.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002363
2364 // We don't add the entry block, the exit block, and the first block, which
2365 // has been merged with `at`.
2366 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
2367
2368 // We add the `to` block.
2369 static constexpr int kNumberOfNewBlocksInCaller = 1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002370 size_t blocks_added = (reverse_post_order_.size() - kNumberOfSkippedBlocksInCallee)
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002371 + kNumberOfNewBlocksInCaller;
2372
2373 // Find the location of `at` in the outer graph's reverse post order. The new
2374 // blocks will be added after it.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002375 size_t index_of_at = IndexOfElement(outer_graph->reverse_post_order_, at);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002376 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
2377
David Brazdil95177982015-10-30 12:56:58 -05002378 // Do a reverse post order of the blocks in the callee and do (1), (2), (3)
2379 // and (4) to the blocks that apply.
Vladimir Marko2c45bc92016-10-25 16:54:12 +01002380 for (HBasicBlock* current : GetReversePostOrder()) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002381 if (current != exit_block_ && current != entry_block_ && current != first) {
David Brazdil95177982015-10-30 12:56:58 -05002382 DCHECK(current->GetTryCatchInformation() == nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002383 DCHECK(current->GetGraph() == this);
2384 current->SetGraph(outer_graph);
2385 outer_graph->AddBlock(current);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002386 outer_graph->reverse_post_order_[++index_of_at] = current;
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +00002387 UpdateLoopAndTryInformationOfNewBlock(current, at, /* replace_if_back_edge */ false);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002388 }
2389 }
2390
David Brazdil95177982015-10-30 12:56:58 -05002391 // Do (1), (2), (3) and (4) to `to`.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002392 to->SetGraph(outer_graph);
2393 outer_graph->AddBlock(to);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002394 outer_graph->reverse_post_order_[++index_of_at] = to;
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +00002395 // Only `to` can become a back edge, as the inlined blocks
2396 // are predecessors of `to`.
2397 UpdateLoopAndTryInformationOfNewBlock(to, at, /* replace_if_back_edge */ true);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00002398
David Brazdil3f523062016-02-29 16:53:33 +00002399 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002400 // to not `HReturn` but `HGoto` instead. Special case throwing blocks
2401 // to now get the outer graph exit block as successor. Note that the inliner
2402 // currently doesn't support inlining methods with try/catch.
2403 HPhi* return_value_phi = nullptr;
2404 bool rerun_dominance = false;
2405 bool rerun_loop_analysis = false;
2406 for (size_t pred = 0; pred < to->GetPredecessors().size(); ++pred) {
2407 HBasicBlock* predecessor = to->GetPredecessors()[pred];
David Brazdil3f523062016-02-29 16:53:33 +00002408 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002409 if (last->IsThrow()) {
2410 DCHECK(!at->IsTryBlock());
2411 predecessor->ReplaceSuccessor(to, outer_graph->GetExitBlock());
2412 --pred;
2413 // We need to re-run dominance information, as the exit block now has
2414 // a new dominator.
2415 rerun_dominance = true;
2416 if (predecessor->GetLoopInformation() != nullptr) {
2417 // The exit block and blocks post dominated by the exit block do not belong
2418 // to any loop. Because we do not compute the post dominators, we need to re-run
2419 // loop analysis to get the loop information correct.
2420 rerun_loop_analysis = true;
2421 }
2422 } else {
2423 if (last->IsReturnVoid()) {
2424 DCHECK(return_value == nullptr);
2425 DCHECK(return_value_phi == nullptr);
2426 } else {
David Brazdil3f523062016-02-29 16:53:33 +00002427 DCHECK(last->IsReturn());
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002428 if (return_value_phi != nullptr) {
2429 return_value_phi->AddInput(last->InputAt(0));
2430 } else if (return_value == nullptr) {
2431 return_value = last->InputAt(0);
2432 } else {
2433 // There will be multiple returns.
2434 return_value_phi = new (allocator) HPhi(
2435 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()), to->GetDexPc());
2436 to->AddPhi(return_value_phi);
2437 return_value_phi->AddInput(return_value);
2438 return_value_phi->AddInput(last->InputAt(0));
2439 return_value = return_value_phi;
2440 }
David Brazdil3f523062016-02-29 16:53:33 +00002441 }
2442 predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc()));
2443 predecessor->RemoveInstruction(last);
2444 }
2445 }
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002446 if (rerun_loop_analysis) {
Nicolas Geoffray1eede6a2017-03-02 16:14:53 +00002447 DCHECK(!outer_graph->HasIrreducibleLoops())
2448 << "Recomputing loop information in graphs with irreducible loops "
2449 << "is unsupported, as it could lead to loop header changes";
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002450 outer_graph->ClearLoopInformation();
2451 outer_graph->ClearDominanceInformation();
2452 outer_graph->BuildDominatorTree();
2453 } else if (rerun_dominance) {
2454 outer_graph->ClearDominanceInformation();
2455 outer_graph->ComputeDominanceInformation();
2456 }
David Brazdil3f523062016-02-29 16:53:33 +00002457 }
David Brazdil05144f42015-04-16 15:18:00 +01002458
2459 // Walk over the entry block and:
2460 // - Move constants from the entry block to the outer_graph's entry block,
2461 // - Replace HParameterValue instructions with their real value.
2462 // - Remove suspend checks, that hold an environment.
2463 // We must do this after the other blocks have been inlined, otherwise ids of
2464 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01002465 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01002466 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
2467 HInstruction* current = it.Current();
Calin Juravle214bbcd2015-10-20 14:54:07 +01002468 HInstruction* replacement = nullptr;
David Brazdil05144f42015-04-16 15:18:00 +01002469 if (current->IsNullConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002470 replacement = outer_graph->GetNullConstant(current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01002471 } else if (current->IsIntConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002472 replacement = outer_graph->GetIntConstant(
2473 current->AsIntConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01002474 } else if (current->IsLongConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002475 replacement = outer_graph->GetLongConstant(
2476 current->AsLongConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002477 } else if (current->IsFloatConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002478 replacement = outer_graph->GetFloatConstant(
2479 current->AsFloatConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002480 } else if (current->IsDoubleConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002481 replacement = outer_graph->GetDoubleConstant(
2482 current->AsDoubleConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01002483 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002484 if (kIsDebugBuild
2485 && invoke->IsInvokeStaticOrDirect()
2486 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
2487 // Ensure we do not use the last input of `invoke`, as it
2488 // contains a clinit check which is not an actual argument.
2489 size_t last_input_index = invoke->InputCount() - 1;
2490 DCHECK(parameter_index != last_input_index);
2491 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01002492 replacement = invoke->InputAt(parameter_index++);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002493 } else if (current->IsCurrentMethod()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002494 replacement = outer_graph->GetCurrentMethod();
David Brazdil05144f42015-04-16 15:18:00 +01002495 } else {
2496 DCHECK(current->IsGoto() || current->IsSuspendCheck());
2497 entry_block_->RemoveInstruction(current);
2498 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01002499 if (replacement != nullptr) {
2500 current->ReplaceWith(replacement);
2501 // If the current is the return value then we need to update the latter.
2502 if (current == return_value) {
2503 DCHECK_EQ(entry_block_, return_value->GetBlock());
2504 return_value = replacement;
2505 }
2506 }
2507 }
2508
Calin Juravle2e768302015-07-28 14:41:11 +00002509 return return_value;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002510}
2511
Mingyao Yang3584bce2015-05-19 16:01:59 -07002512/*
2513 * Loop will be transformed to:
2514 * old_pre_header
2515 * |
2516 * if_block
2517 * / \
Aart Bik3fc7f352015-11-20 22:03:03 -08002518 * true_block false_block
Mingyao Yang3584bce2015-05-19 16:01:59 -07002519 * \ /
2520 * new_pre_header
2521 * |
2522 * header
2523 */
2524void HGraph::TransformLoopHeaderForBCE(HBasicBlock* header) {
2525 DCHECK(header->IsLoopHeader());
Aart Bik3fc7f352015-11-20 22:03:03 -08002526 HBasicBlock* old_pre_header = header->GetDominator();
Mingyao Yang3584bce2015-05-19 16:01:59 -07002527
Aart Bik3fc7f352015-11-20 22:03:03 -08002528 // Need extra block to avoid critical edge.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002529 HBasicBlock* if_block = new (allocator_) HBasicBlock(this, header->GetDexPc());
2530 HBasicBlock* true_block = new (allocator_) HBasicBlock(this, header->GetDexPc());
2531 HBasicBlock* false_block = new (allocator_) HBasicBlock(this, header->GetDexPc());
2532 HBasicBlock* new_pre_header = new (allocator_) HBasicBlock(this, header->GetDexPc());
Mingyao Yang3584bce2015-05-19 16:01:59 -07002533 AddBlock(if_block);
Aart Bik3fc7f352015-11-20 22:03:03 -08002534 AddBlock(true_block);
2535 AddBlock(false_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002536 AddBlock(new_pre_header);
2537
Aart Bik3fc7f352015-11-20 22:03:03 -08002538 header->ReplacePredecessor(old_pre_header, new_pre_header);
2539 old_pre_header->successors_.clear();
2540 old_pre_header->dominated_blocks_.clear();
Mingyao Yang3584bce2015-05-19 16:01:59 -07002541
Aart Bik3fc7f352015-11-20 22:03:03 -08002542 old_pre_header->AddSuccessor(if_block);
2543 if_block->AddSuccessor(true_block); // True successor
2544 if_block->AddSuccessor(false_block); // False successor
2545 true_block->AddSuccessor(new_pre_header);
2546 false_block->AddSuccessor(new_pre_header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002547
Aart Bik3fc7f352015-11-20 22:03:03 -08002548 old_pre_header->dominated_blocks_.push_back(if_block);
2549 if_block->SetDominator(old_pre_header);
2550 if_block->dominated_blocks_.push_back(true_block);
2551 true_block->SetDominator(if_block);
2552 if_block->dominated_blocks_.push_back(false_block);
2553 false_block->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00002554 if_block->dominated_blocks_.push_back(new_pre_header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002555 new_pre_header->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00002556 new_pre_header->dominated_blocks_.push_back(header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002557 header->SetDominator(new_pre_header);
2558
Aart Bik3fc7f352015-11-20 22:03:03 -08002559 // Fix reverse post order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002560 size_t index_of_header = IndexOfElement(reverse_post_order_, header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002561 MakeRoomFor(&reverse_post_order_, 4, index_of_header - 1);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002562 reverse_post_order_[index_of_header++] = if_block;
Aart Bik3fc7f352015-11-20 22:03:03 -08002563 reverse_post_order_[index_of_header++] = true_block;
2564 reverse_post_order_[index_of_header++] = false_block;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002565 reverse_post_order_[index_of_header++] = new_pre_header;
Mingyao Yang3584bce2015-05-19 16:01:59 -07002566
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +00002567 // The pre_header can never be a back edge of a loop.
2568 DCHECK((old_pre_header->GetLoopInformation() == nullptr) ||
2569 !old_pre_header->GetLoopInformation()->IsBackEdge(*old_pre_header));
2570 UpdateLoopAndTryInformationOfNewBlock(
2571 if_block, old_pre_header, /* replace_if_back_edge */ false);
2572 UpdateLoopAndTryInformationOfNewBlock(
2573 true_block, old_pre_header, /* replace_if_back_edge */ false);
2574 UpdateLoopAndTryInformationOfNewBlock(
2575 false_block, old_pre_header, /* replace_if_back_edge */ false);
2576 UpdateLoopAndTryInformationOfNewBlock(
2577 new_pre_header, old_pre_header, /* replace_if_back_edge */ false);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002578}
2579
Aart Bikf8f5a162017-02-06 15:35:29 -08002580HBasicBlock* HGraph::TransformLoopForVectorization(HBasicBlock* header,
2581 HBasicBlock* body,
2582 HBasicBlock* exit) {
2583 DCHECK(header->IsLoopHeader());
2584 HLoopInformation* loop = header->GetLoopInformation();
2585
2586 // Add new loop blocks.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002587 HBasicBlock* new_pre_header = new (allocator_) HBasicBlock(this, header->GetDexPc());
2588 HBasicBlock* new_header = new (allocator_) HBasicBlock(this, header->GetDexPc());
2589 HBasicBlock* new_body = new (allocator_) HBasicBlock(this, header->GetDexPc());
Aart Bikf8f5a162017-02-06 15:35:29 -08002590 AddBlock(new_pre_header);
2591 AddBlock(new_header);
2592 AddBlock(new_body);
2593
2594 // Set up control flow.
2595 header->ReplaceSuccessor(exit, new_pre_header);
2596 new_pre_header->AddSuccessor(new_header);
2597 new_header->AddSuccessor(exit);
2598 new_header->AddSuccessor(new_body);
2599 new_body->AddSuccessor(new_header);
2600
2601 // Set up dominators.
2602 header->ReplaceDominatedBlock(exit, new_pre_header);
2603 new_pre_header->SetDominator(header);
2604 new_pre_header->dominated_blocks_.push_back(new_header);
2605 new_header->SetDominator(new_pre_header);
2606 new_header->dominated_blocks_.push_back(new_body);
2607 new_body->SetDominator(new_header);
2608 new_header->dominated_blocks_.push_back(exit);
2609 exit->SetDominator(new_header);
2610
2611 // Fix reverse post order.
2612 size_t index_of_header = IndexOfElement(reverse_post_order_, header);
2613 MakeRoomFor(&reverse_post_order_, 2, index_of_header);
2614 reverse_post_order_[++index_of_header] = new_pre_header;
2615 reverse_post_order_[++index_of_header] = new_header;
2616 size_t index_of_body = IndexOfElement(reverse_post_order_, body);
2617 MakeRoomFor(&reverse_post_order_, 1, index_of_body - 1);
2618 reverse_post_order_[index_of_body] = new_body;
2619
Aart Bikb07d1bc2017-04-05 10:03:15 -07002620 // Add gotos and suspend check (client must add conditional in header).
Vladimir Markoca6fff82017-10-03 14:49:14 +01002621 new_pre_header->AddInstruction(new (allocator_) HGoto());
2622 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(header->GetDexPc());
Aart Bikf8f5a162017-02-06 15:35:29 -08002623 new_header->AddInstruction(suspend_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002624 new_body->AddInstruction(new (allocator_) HGoto());
Aart Bikb07d1bc2017-04-05 10:03:15 -07002625 suspend_check->CopyEnvironmentFromWithLoopPhiAdjustment(
2626 loop->GetSuspendCheck()->GetEnvironment(), header);
Aart Bikf8f5a162017-02-06 15:35:29 -08002627
2628 // Update loop information.
2629 new_header->AddBackEdge(new_body);
2630 new_header->GetLoopInformation()->SetSuspendCheck(suspend_check);
2631 new_header->GetLoopInformation()->Populate();
2632 new_pre_header->SetLoopInformation(loop->GetPreHeader()->GetLoopInformation()); // outward
2633 HLoopInformationOutwardIterator it(*new_header);
2634 for (it.Advance(); !it.Done(); it.Advance()) {
2635 it.Current()->Add(new_pre_header);
2636 it.Current()->Add(new_header);
2637 it.Current()->Add(new_body);
2638 }
2639 return new_pre_header;
2640}
2641
David Brazdilf5552582015-12-27 13:36:12 +00002642static void CheckAgainstUpperBound(ReferenceTypeInfo rti, ReferenceTypeInfo upper_bound_rti)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002643 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdilf5552582015-12-27 13:36:12 +00002644 if (rti.IsValid()) {
2645 DCHECK(upper_bound_rti.IsSupertypeOf(rti))
2646 << " upper_bound_rti: " << upper_bound_rti
2647 << " rti: " << rti;
Nicolas Geoffray18401b72016-03-11 13:35:51 +00002648 DCHECK(!upper_bound_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes() || rti.IsExact())
2649 << " upper_bound_rti: " << upper_bound_rti
2650 << " rti: " << rti;
David Brazdilf5552582015-12-27 13:36:12 +00002651 }
2652}
2653
Calin Juravle2e768302015-07-28 14:41:11 +00002654void HInstruction::SetReferenceTypeInfo(ReferenceTypeInfo rti) {
2655 if (kIsDebugBuild) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002656 DCHECK_EQ(GetType(), DataType::Type::kReference);
Calin Juravle2e768302015-07-28 14:41:11 +00002657 ScopedObjectAccess soa(Thread::Current());
2658 DCHECK(rti.IsValid()) << "Invalid RTI for " << DebugName();
2659 if (IsBoundType()) {
2660 // Having the test here spares us from making the method virtual just for
2661 // the sake of a DCHECK.
David Brazdilf5552582015-12-27 13:36:12 +00002662 CheckAgainstUpperBound(rti, AsBoundType()->GetUpperBound());
Calin Juravle2e768302015-07-28 14:41:11 +00002663 }
2664 }
Vladimir Markoa1de9182016-02-25 11:37:38 +00002665 reference_type_handle_ = rti.GetTypeHandle();
2666 SetPackedFlag<kFlagReferenceTypeIsExact>(rti.IsExact());
Calin Juravle2e768302015-07-28 14:41:11 +00002667}
2668
David Brazdilf5552582015-12-27 13:36:12 +00002669void HBoundType::SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null) {
2670 if (kIsDebugBuild) {
2671 ScopedObjectAccess soa(Thread::Current());
2672 DCHECK(upper_bound.IsValid());
2673 DCHECK(!upper_bound_.IsValid()) << "Upper bound should only be set once.";
2674 CheckAgainstUpperBound(GetReferenceTypeInfo(), upper_bound);
2675 }
2676 upper_bound_ = upper_bound;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002677 SetPackedFlag<kFlagUpperCanBeNull>(can_be_null);
David Brazdilf5552582015-12-27 13:36:12 +00002678}
2679
Vladimir Markoa1de9182016-02-25 11:37:38 +00002680ReferenceTypeInfo ReferenceTypeInfo::Create(TypeHandle type_handle, bool is_exact) {
Calin Juravle2e768302015-07-28 14:41:11 +00002681 if (kIsDebugBuild) {
2682 ScopedObjectAccess soa(Thread::Current());
2683 DCHECK(IsValidHandle(type_handle));
Nicolas Geoffray18401b72016-03-11 13:35:51 +00002684 if (!is_exact) {
2685 DCHECK(!type_handle->CannotBeAssignedFromOtherTypes())
2686 << "Callers of ReferenceTypeInfo::Create should ensure is_exact is properly computed";
2687 }
Calin Juravle2e768302015-07-28 14:41:11 +00002688 }
Vladimir Markoa1de9182016-02-25 11:37:38 +00002689 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravle2e768302015-07-28 14:41:11 +00002690}
2691
Calin Juravleacf735c2015-02-12 15:25:22 +00002692std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
2693 ScopedObjectAccess soa(Thread::Current());
2694 os << "["
Calin Juravle2e768302015-07-28 14:41:11 +00002695 << " is_valid=" << rhs.IsValid()
David Sehr709b0702016-10-13 09:12:37 -07002696 << " type=" << (!rhs.IsValid() ? "?" : mirror::Class::PrettyClass(rhs.GetTypeHandle().Get()))
Calin Juravleacf735c2015-02-12 15:25:22 +00002697 << " is_exact=" << rhs.IsExact()
2698 << " ]";
2699 return os;
2700}
2701
Mark Mendellc4701932015-04-10 13:18:51 -04002702bool HInstruction::HasAnyEnvironmentUseBefore(HInstruction* other) {
2703 // For now, assume that instructions in different blocks may use the
2704 // environment.
2705 // TODO: Use the control flow to decide if this is true.
2706 if (GetBlock() != other->GetBlock()) {
2707 return true;
2708 }
2709
2710 // We know that we are in the same block. Walk from 'this' to 'other',
2711 // checking to see if there is any instruction with an environment.
2712 HInstruction* current = this;
2713 for (; current != other && current != nullptr; current = current->GetNext()) {
2714 // This is a conservative check, as the instruction result may not be in
2715 // the referenced environment.
2716 if (current->HasEnvironment()) {
2717 return true;
2718 }
2719 }
2720
2721 // We should have been called with 'this' before 'other' in the block.
2722 // Just confirm this.
2723 DCHECK(current != nullptr);
2724 return false;
2725}
2726
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002727void HInvoke::SetIntrinsic(Intrinsics intrinsic,
Aart Bik5d75afe2015-12-14 11:57:01 -08002728 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
2729 IntrinsicSideEffects side_effects,
2730 IntrinsicExceptions exceptions) {
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002731 intrinsic_ = intrinsic;
2732 IntrinsicOptimizations opt(this);
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002733
Aart Bik5d75afe2015-12-14 11:57:01 -08002734 // Adjust method's side effects from intrinsic table.
2735 switch (side_effects) {
2736 case kNoSideEffects: SetSideEffects(SideEffects::None()); break;
2737 case kReadSideEffects: SetSideEffects(SideEffects::AllReads()); break;
2738 case kWriteSideEffects: SetSideEffects(SideEffects::AllWrites()); break;
2739 case kAllSideEffects: SetSideEffects(SideEffects::AllExceptGCDependency()); break;
2740 }
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002741
2742 if (needs_env_or_cache == kNoEnvironmentOrCache) {
2743 opt.SetDoesNotNeedDexCache();
2744 opt.SetDoesNotNeedEnvironment();
2745 } else {
2746 // If we need an environment, that means there will be a call, which can trigger GC.
2747 SetSideEffects(GetSideEffects().Union(SideEffects::CanTriggerGC()));
2748 }
Aart Bik5d75afe2015-12-14 11:57:01 -08002749 // Adjust method's exception status from intrinsic table.
Aart Bik09e8d5f2016-01-22 16:49:55 -08002750 SetCanThrow(exceptions == kCanThrow);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002751}
2752
David Brazdil6de19382016-01-08 17:37:10 +00002753bool HNewInstance::IsStringAlloc() const {
2754 ScopedObjectAccess soa(Thread::Current());
2755 return GetReferenceTypeInfo().IsStringClass();
2756}
2757
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002758bool HInvoke::NeedsEnvironment() const {
2759 if (!IsIntrinsic()) {
2760 return true;
2761 }
2762 IntrinsicOptimizations opt(*this);
2763 return !opt.GetDoesNotNeedEnvironment();
2764}
2765
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002766const DexFile& HInvokeStaticOrDirect::GetDexFileForPcRelativeDexCache() const {
2767 ArtMethod* caller = GetEnvironment()->GetMethod();
2768 ScopedObjectAccess soa(Thread::Current());
2769 // `caller` is null for a top-level graph representing a method whose declaring
2770 // class was not resolved.
2771 return caller == nullptr ? GetBlock()->GetGraph()->GetDexFile() : *caller->GetDexFile();
2772}
2773
Vladimir Markodc151b22015-10-15 18:02:30 +01002774bool HInvokeStaticOrDirect::NeedsDexCacheOfDeclaringClass() const {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01002775 if (GetMethodLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002776 return false;
2777 }
2778 if (!IsIntrinsic()) {
2779 return true;
2780 }
2781 IntrinsicOptimizations opt(*this);
2782 return !opt.GetDoesNotNeedDexCache();
2783}
2784
Vladimir Markof64242a2015-12-01 14:58:23 +00002785std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs) {
2786 switch (rhs) {
2787 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
Vladimir Marko65979462017-05-19 17:25:12 +01002788 return os << "StringInit";
Vladimir Markof64242a2015-12-01 14:58:23 +00002789 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Marko65979462017-05-19 17:25:12 +01002790 return os << "Recursive";
2791 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
2792 return os << "BootImageLinkTimePcRelative";
Vladimir Markof64242a2015-12-01 14:58:23 +00002793 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Vladimir Marko19d7d502017-05-24 13:04:14 +01002794 return os << "DirectAddress";
Vladimir Marko0eb882b2017-05-15 13:39:18 +01002795 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry:
2796 return os << "BssEntry";
Vladimir Markoe7197bf2017-06-02 17:00:23 +01002797 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall:
2798 return os << "RuntimeCall";
Vladimir Markof64242a2015-12-01 14:58:23 +00002799 default:
2800 LOG(FATAL) << "Unknown MethodLoadKind: " << static_cast<int>(rhs);
2801 UNREACHABLE();
2802 }
2803}
2804
Vladimir Markofbb184a2015-11-13 14:47:00 +00002805std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs) {
2806 switch (rhs) {
2807 case HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit:
2808 return os << "explicit";
2809 case HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit:
2810 return os << "implicit";
2811 case HInvokeStaticOrDirect::ClinitCheckRequirement::kNone:
2812 return os << "none";
2813 default:
Vladimir Markof64242a2015-12-01 14:58:23 +00002814 LOG(FATAL) << "Unknown ClinitCheckRequirement: " << static_cast<int>(rhs);
2815 UNREACHABLE();
Vladimir Markofbb184a2015-11-13 14:47:00 +00002816 }
2817}
2818
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002819bool HLoadClass::InstructionDataEquals(const HInstruction* other) const {
2820 const HLoadClass* other_load_class = other->AsLoadClass();
2821 // TODO: To allow GVN for HLoadClass from different dex files, we should compare the type
2822 // names rather than type indexes. However, we shall also have to re-think the hash code.
2823 if (type_index_ != other_load_class->type_index_ ||
2824 GetPackedFields() != other_load_class->GetPackedFields()) {
2825 return false;
2826 }
Nicolas Geoffray9b1583e2016-12-13 13:43:31 +00002827 switch (GetLoadKind()) {
2828 case LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01002829 case LoadKind::kBootImageClassTable:
Nicolas Geoffray1ea9efc2017-01-16 22:57:39 +00002830 case LoadKind::kJitTableAddress: {
2831 ScopedObjectAccess soa(Thread::Current());
2832 return GetClass().Get() == other_load_class->GetClass().Get();
2833 }
Nicolas Geoffray9b1583e2016-12-13 13:43:31 +00002834 default:
Vladimir Marko48886c22017-01-06 11:45:47 +00002835 DCHECK(HasTypeReference(GetLoadKind()));
Nicolas Geoffray9b1583e2016-12-13 13:43:31 +00002836 return IsSameDexFile(GetDexFile(), other_load_class->GetDexFile());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002837 }
2838}
2839
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002840std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs) {
2841 switch (rhs) {
2842 case HLoadClass::LoadKind::kReferrersClass:
2843 return os << "ReferrersClass";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002844 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
2845 return os << "BootImageLinkTimePcRelative";
2846 case HLoadClass::LoadKind::kBootImageAddress:
2847 return os << "BootImageAddress";
Vladimir Marko94ec2db2017-09-06 17:21:03 +01002848 case HLoadClass::LoadKind::kBootImageClassTable:
2849 return os << "BootImageClassTable";
Vladimir Marko6bec91c2017-01-09 15:03:12 +00002850 case HLoadClass::LoadKind::kBssEntry:
2851 return os << "BssEntry";
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00002852 case HLoadClass::LoadKind::kJitTableAddress:
2853 return os << "JitTableAddress";
Vladimir Marko847e6ce2017-06-02 13:55:07 +01002854 case HLoadClass::LoadKind::kRuntimeCall:
2855 return os << "RuntimeCall";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002856 default:
2857 LOG(FATAL) << "Unknown HLoadClass::LoadKind: " << static_cast<int>(rhs);
2858 UNREACHABLE();
2859 }
2860}
2861
Vladimir Marko372f10e2016-05-17 16:30:10 +01002862bool HLoadString::InstructionDataEquals(const HInstruction* other) const {
2863 const HLoadString* other_load_string = other->AsLoadString();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002864 // TODO: To allow GVN for HLoadString from different dex files, we should compare the strings
2865 // rather than their indexes. However, we shall also have to re-think the hash code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002866 if (string_index_ != other_load_string->string_index_ ||
2867 GetPackedFields() != other_load_string->GetPackedFields()) {
2868 return false;
2869 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00002870 switch (GetLoadKind()) {
2871 case LoadKind::kBootImageAddress:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002872 case LoadKind::kBootImageInternTable:
Nicolas Geoffray1ea9efc2017-01-16 22:57:39 +00002873 case LoadKind::kJitTableAddress: {
2874 ScopedObjectAccess soa(Thread::Current());
2875 return GetString().Get() == other_load_string->GetString().Get();
2876 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00002877 default:
2878 return IsSameDexFile(GetDexFile(), other_load_string->GetDexFile());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002879 }
2880}
2881
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002882std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs) {
2883 switch (rhs) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002884 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
2885 return os << "BootImageLinkTimePcRelative";
2886 case HLoadString::LoadKind::kBootImageAddress:
2887 return os << "BootImageAddress";
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002888 case HLoadString::LoadKind::kBootImageInternTable:
2889 return os << "BootImageInternTable";
Vladimir Markoaad75c62016-10-03 08:46:48 +00002890 case HLoadString::LoadKind::kBssEntry:
2891 return os << "BssEntry";
Mingyao Yangbe44dcf2016-11-30 14:17:32 -08002892 case HLoadString::LoadKind::kJitTableAddress:
2893 return os << "JitTableAddress";
Vladimir Marko847e6ce2017-06-02 13:55:07 +01002894 case HLoadString::LoadKind::kRuntimeCall:
2895 return os << "RuntimeCall";
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002896 default:
2897 LOG(FATAL) << "Unknown HLoadString::LoadKind: " << static_cast<int>(rhs);
2898 UNREACHABLE();
2899 }
2900}
2901
Mark Mendellc4701932015-04-10 13:18:51 -04002902void HInstruction::RemoveEnvironmentUsers() {
Vladimir Marko46817b82016-03-29 12:21:58 +01002903 for (const HUseListNode<HEnvironment*>& use : GetEnvUses()) {
2904 HEnvironment* user = use.GetUser();
2905 user->SetRawEnvAt(use.GetIndex(), nullptr);
Mark Mendellc4701932015-04-10 13:18:51 -04002906 }
Vladimir Marko46817b82016-03-29 12:21:58 +01002907 env_uses_.clear();
Mark Mendellc4701932015-04-10 13:18:51 -04002908}
2909
Artem Serovcced8ba2017-07-19 18:18:09 +01002910HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr) {
2911 HInstruction* clone = instr->Clone(instr->GetBlock()->GetGraph()->GetAllocator());
2912 HBasicBlock* block = instr->GetBlock();
2913
2914 if (instr->IsPhi()) {
2915 HPhi* phi = instr->AsPhi();
2916 DCHECK(!phi->HasEnvironment());
2917 HPhi* phi_clone = clone->AsPhi();
2918 block->ReplaceAndRemovePhiWith(phi, phi_clone);
2919 } else {
2920 block->ReplaceAndRemoveInstructionWith(instr, clone);
2921 if (instr->HasEnvironment()) {
2922 clone->CopyEnvironmentFrom(instr->GetEnvironment());
2923 HLoopInformation* loop_info = block->GetLoopInformation();
2924 if (instr->IsSuspendCheck() && loop_info != nullptr) {
2925 loop_info->SetSuspendCheck(clone->AsSuspendCheck());
2926 }
2927 }
2928 }
2929 return clone;
2930}
2931
Roland Levillainc9b21f82016-03-23 16:36:59 +00002932// Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -05002933HInstruction* HGraph::InsertOppositeCondition(HInstruction* cond, HInstruction* cursor) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002934 ArenaAllocator* allocator = GetAllocator();
Mark Mendellf6529172015-11-17 11:16:56 -05002935
2936 if (cond->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002937 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType())) {
Mark Mendellf6529172015-11-17 11:16:56 -05002938 // Can't reverse floating point conditions. We have to use HBooleanNot in that case.
2939 HInstruction* lhs = cond->InputAt(0);
2940 HInstruction* rhs = cond->InputAt(1);
David Brazdil5c004852015-11-23 09:44:52 +00002941 HInstruction* replacement = nullptr;
Mark Mendellf6529172015-11-17 11:16:56 -05002942 switch (cond->AsCondition()->GetOppositeCondition()) { // get *opposite*
2943 case kCondEQ: replacement = new (allocator) HEqual(lhs, rhs); break;
2944 case kCondNE: replacement = new (allocator) HNotEqual(lhs, rhs); break;
2945 case kCondLT: replacement = new (allocator) HLessThan(lhs, rhs); break;
2946 case kCondLE: replacement = new (allocator) HLessThanOrEqual(lhs, rhs); break;
2947 case kCondGT: replacement = new (allocator) HGreaterThan(lhs, rhs); break;
2948 case kCondGE: replacement = new (allocator) HGreaterThanOrEqual(lhs, rhs); break;
2949 case kCondB: replacement = new (allocator) HBelow(lhs, rhs); break;
2950 case kCondBE: replacement = new (allocator) HBelowOrEqual(lhs, rhs); break;
2951 case kCondA: replacement = new (allocator) HAbove(lhs, rhs); break;
2952 case kCondAE: replacement = new (allocator) HAboveOrEqual(lhs, rhs); break;
David Brazdil5c004852015-11-23 09:44:52 +00002953 default:
2954 LOG(FATAL) << "Unexpected condition";
2955 UNREACHABLE();
Mark Mendellf6529172015-11-17 11:16:56 -05002956 }
2957 cursor->GetBlock()->InsertInstructionBefore(replacement, cursor);
2958 return replacement;
2959 } else if (cond->IsIntConstant()) {
2960 HIntConstant* int_const = cond->AsIntConstant();
Roland Levillain1a653882016-03-18 18:05:57 +00002961 if (int_const->IsFalse()) {
Mark Mendellf6529172015-11-17 11:16:56 -05002962 return GetIntConstant(1);
2963 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002964 DCHECK(int_const->IsTrue()) << int_const->GetValue();
Mark Mendellf6529172015-11-17 11:16:56 -05002965 return GetIntConstant(0);
2966 }
2967 } else {
2968 HInstruction* replacement = new (allocator) HBooleanNot(cond);
2969 cursor->GetBlock()->InsertInstructionBefore(replacement, cursor);
2970 return replacement;
2971 }
2972}
2973
Roland Levillainc9285912015-12-18 10:38:42 +00002974std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs) {
2975 os << "["
2976 << " source=" << rhs.GetSource()
2977 << " destination=" << rhs.GetDestination()
2978 << " type=" << rhs.GetType()
2979 << " instruction=";
2980 if (rhs.GetInstruction() != nullptr) {
2981 os << rhs.GetInstruction()->DebugName() << ' ' << rhs.GetInstruction()->GetId();
2982 } else {
2983 os << "null";
2984 }
2985 os << " ]";
2986 return os;
2987}
2988
Roland Levillain86503782016-02-11 19:07:30 +00002989std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs) {
2990 switch (rhs) {
2991 case TypeCheckKind::kUnresolvedCheck:
2992 return os << "unresolved_check";
2993 case TypeCheckKind::kExactCheck:
2994 return os << "exact_check";
2995 case TypeCheckKind::kClassHierarchyCheck:
2996 return os << "class_hierarchy_check";
2997 case TypeCheckKind::kAbstractClassCheck:
2998 return os << "abstract_class_check";
2999 case TypeCheckKind::kInterfaceCheck:
3000 return os << "interface_check";
3001 case TypeCheckKind::kArrayObjectCheck:
3002 return os << "array_object_check";
3003 case TypeCheckKind::kArrayCheck:
3004 return os << "array_check";
3005 default:
3006 LOG(FATAL) << "Unknown TypeCheckKind: " << static_cast<int>(rhs);
3007 UNREACHABLE();
3008 }
3009}
3010
Andreas Gampe26de38b2016-07-27 17:53:11 -07003011std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind) {
3012 switch (kind) {
3013 case MemBarrierKind::kAnyStore:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003014 return os << "AnyStore";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003015 case MemBarrierKind::kLoadAny:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003016 return os << "LoadAny";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003017 case MemBarrierKind::kStoreStore:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003018 return os << "StoreStore";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003019 case MemBarrierKind::kAnyAny:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003020 return os << "AnyAny";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003021 case MemBarrierKind::kNTStoreStore:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003022 return os << "NTStoreStore";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003023
3024 default:
3025 LOG(FATAL) << "Unknown MemBarrierKind: " << static_cast<int>(kind);
3026 UNREACHABLE();
3027 }
3028}
3029
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003030} // namespace art