blob: ff4e9aa510b8efd47075ce7c6efe4b73983202da [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
Roland Levillain6c82d402014-10-13 16:10:27 +01001114bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
1115 if (other_instruction == this) {
1116 // An instruction does not strictly dominate itself.
1117 return false;
1118 }
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
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001151void HInstruction::RemoveEnvironment() {
1152 RemoveEnvironmentUses(this);
1153 environment_ = nullptr;
1154}
1155
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001156void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001157 DCHECK(other != nullptr);
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001158 // Note: fixup_end remains valid across splice_after().
1159 auto fixup_end = other->uses_.empty() ? other->uses_.begin() : ++other->uses_.begin();
1160 other->uses_.splice_after(other->uses_.before_begin(), uses_);
1161 other->FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001162
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001163 // Note: env_fixup_end remains valid across splice_after().
1164 auto env_fixup_end =
1165 other->env_uses_.empty() ? other->env_uses_.begin() : ++other->env_uses_.begin();
1166 other->env_uses_.splice_after(other->env_uses_.before_begin(), env_uses_);
1167 other->FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001168
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001169 DCHECK(uses_.empty());
1170 DCHECK(env_uses_.empty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001171}
1172
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00001173void HInstruction::ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement) {
1174 const HUseList<HInstruction*>& uses = GetUses();
1175 for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) {
1176 HInstruction* user = it->GetUser();
1177 size_t index = it->GetIndex();
1178 // Increment `it` now because `*it` may disappear thanks to user->ReplaceInput().
1179 ++it;
1180 if (dominator->StrictlyDominates(user)) {
1181 user->ReplaceInput(replacement, index);
1182 }
1183 }
1184}
1185
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001186void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001187 HUserRecord<HInstruction*> input_use = InputRecordAt(index);
Vladimir Markoc6b56272016-04-20 18:45:25 +01001188 if (input_use.GetInstruction() == replacement) {
1189 // Nothing to do.
1190 return;
1191 }
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001192 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
Vladimir Marko3c19d3e2016-04-19 14:36:35 +01001193 // Note: fixup_end remains valid across splice_after().
1194 auto fixup_end =
1195 replacement->uses_.empty() ? replacement->uses_.begin() : ++replacement->uses_.begin();
1196 replacement->uses_.splice_after(replacement->uses_.before_begin(),
1197 input_use.GetInstruction()->uses_,
1198 before_use_node);
1199 replacement->FixUpUserRecordsAfterUseInsertion(fixup_end);
1200 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001201}
1202
Nicolas Geoffray39468442014-09-02 15:17:15 +01001203size_t HInstruction::EnvironmentSize() const {
1204 return HasEnvironment() ? environment_->Size() : 0;
1205}
1206
Mingyao Yanga9dbe832016-12-15 12:02:53 -08001207void HVariableInputSizeInstruction::AddInput(HInstruction* input) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001208 DCHECK(input->GetBlock() != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001209 inputs_.push_back(HUserRecord<HInstruction*>(input));
1210 input->AddUseAt(this, inputs_.size() - 1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001211}
1212
Mingyao Yanga9dbe832016-12-15 12:02:53 -08001213void HVariableInputSizeInstruction::InsertInputAt(size_t index, HInstruction* input) {
1214 inputs_.insert(inputs_.begin() + index, HUserRecord<HInstruction*>(input));
1215 input->AddUseAt(this, index);
1216 // Update indexes in use nodes of inputs that have been pushed further back by the insert().
1217 for (size_t i = index + 1u, e = inputs_.size(); i < e; ++i) {
1218 DCHECK_EQ(inputs_[i].GetUseNode()->GetIndex(), i - 1u);
1219 inputs_[i].GetUseNode()->SetIndex(i);
1220 }
1221}
1222
1223void HVariableInputSizeInstruction::RemoveInputAt(size_t index) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001224 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001225 inputs_.erase(inputs_.begin() + index);
Vladimir Marko372f10e2016-05-17 16:30:10 +01001226 // Update indexes in use nodes of inputs that have been pulled forward by the erase().
1227 for (size_t i = index, e = inputs_.size(); i < e; ++i) {
1228 DCHECK_EQ(inputs_[i].GetUseNode()->GetIndex(), i + 1u);
1229 inputs_[i].GetUseNode()->SetIndex(i);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001230 }
David Brazdil2d7352b2015-04-20 14:52:42 +01001231}
1232
Igor Murashkind01745e2017-04-05 16:40:31 -07001233void HVariableInputSizeInstruction::RemoveAllInputs() {
1234 RemoveAsUserOfAllInputs();
1235 DCHECK(!HasNonEnvironmentUses());
1236
1237 inputs_.clear();
1238 DCHECK_EQ(0u, InputCount());
1239}
1240
Igor Murashkin6ef45672017-08-08 13:59:55 -07001241size_t HConstructorFence::RemoveConstructorFences(HInstruction* instruction) {
Igor Murashkind01745e2017-04-05 16:40:31 -07001242 DCHECK(instruction->GetBlock() != nullptr);
1243 // Removing constructor fences only makes sense for instructions with an object return type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001244 DCHECK_EQ(DataType::Type::kReference, instruction->GetType());
Igor Murashkind01745e2017-04-05 16:40:31 -07001245
Igor Murashkin6ef45672017-08-08 13:59:55 -07001246 // Return how many instructions were removed for statistic purposes.
1247 size_t remove_count = 0;
1248
Igor Murashkind01745e2017-04-05 16:40:31 -07001249 // Efficient implementation that simultaneously (in one pass):
1250 // * Scans the uses list for all constructor fences.
1251 // * Deletes that constructor fence from the uses list of `instruction`.
1252 // * Deletes `instruction` from the constructor fence's inputs.
1253 // * Deletes the constructor fence if it now has 0 inputs.
1254
1255 const HUseList<HInstruction*>& uses = instruction->GetUses();
1256 // Warning: Although this is "const", we might mutate the list when calling RemoveInputAt.
1257 for (auto it = uses.begin(), end = uses.end(); it != end; ) {
1258 const HUseListNode<HInstruction*>& use_node = *it;
1259 HInstruction* const use_instruction = use_node.GetUser();
1260
1261 // Advance the iterator immediately once we fetch the use_node.
1262 // Warning: If the input is removed, the current iterator becomes invalid.
1263 ++it;
1264
1265 if (use_instruction->IsConstructorFence()) {
1266 HConstructorFence* ctor_fence = use_instruction->AsConstructorFence();
1267 size_t input_index = use_node.GetIndex();
1268
1269 // Process the candidate instruction for removal
1270 // from the graph.
1271
1272 // Constructor fence instructions are never
1273 // used by other instructions.
1274 //
1275 // If we wanted to make this more generic, it
1276 // could be a runtime if statement.
1277 DCHECK(!ctor_fence->HasUses());
1278
1279 // A constructor fence's return type is "kPrimVoid"
1280 // and therefore it can't have any environment uses.
1281 DCHECK(!ctor_fence->HasEnvironmentUses());
1282
1283 // Remove the inputs first, otherwise removing the instruction
1284 // will try to remove its uses while we are already removing uses
1285 // and this operation will fail.
1286 DCHECK_EQ(instruction, ctor_fence->InputAt(input_index));
1287
1288 // Removing the input will also remove the `use_node`.
1289 // (Do not look at `use_node` after this, it will be a dangling reference).
1290 ctor_fence->RemoveInputAt(input_index);
1291
1292 // Once all inputs are removed, the fence is considered dead and
1293 // is removed.
1294 if (ctor_fence->InputCount() == 0u) {
1295 ctor_fence->GetBlock()->RemoveInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001296 ++remove_count;
Igor Murashkind01745e2017-04-05 16:40:31 -07001297 }
1298 }
1299 }
1300
1301 if (kIsDebugBuild) {
1302 // Post-condition checks:
1303 // * None of the uses of `instruction` are a constructor fence.
1304 // * The `instruction` itself did not get removed from a block.
1305 for (const HUseListNode<HInstruction*>& use_node : instruction->GetUses()) {
1306 CHECK(!use_node.GetUser()->IsConstructorFence());
1307 }
1308 CHECK(instruction->GetBlock() != nullptr);
1309 }
Igor Murashkin6ef45672017-08-08 13:59:55 -07001310
1311 return remove_count;
Igor Murashkind01745e2017-04-05 16:40:31 -07001312}
1313
Igor Murashkindd018df2017-08-09 10:38:31 -07001314void HConstructorFence::Merge(HConstructorFence* other) {
1315 // Do not delete yourself from the graph.
1316 DCHECK(this != other);
1317 // Don't try to merge with an instruction not associated with a block.
1318 DCHECK(other->GetBlock() != nullptr);
1319 // A constructor fence's return type is "kPrimVoid"
1320 // and therefore it cannot have any environment uses.
1321 DCHECK(!other->HasEnvironmentUses());
1322
1323 auto has_input = [](HInstruction* haystack, HInstruction* needle) {
1324 // Check if `haystack` has `needle` as any of its inputs.
1325 for (size_t input_count = 0; input_count < haystack->InputCount(); ++input_count) {
1326 if (haystack->InputAt(input_count) == needle) {
1327 return true;
1328 }
1329 }
1330 return false;
1331 };
1332
1333 // Add any inputs from `other` into `this` if it wasn't already an input.
1334 for (size_t input_count = 0; input_count < other->InputCount(); ++input_count) {
1335 HInstruction* other_input = other->InputAt(input_count);
1336 if (!has_input(this, other_input)) {
1337 AddInput(other_input);
1338 }
1339 }
1340
1341 other->GetBlock()->RemoveInstruction(other);
1342}
1343
1344HInstruction* HConstructorFence::GetAssociatedAllocation(bool ignore_inputs) {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001345 HInstruction* new_instance_inst = GetPrevious();
1346 // Check if the immediately preceding instruction is a new-instance/new-array.
1347 // Otherwise this fence is for protecting final fields.
1348 if (new_instance_inst != nullptr &&
1349 (new_instance_inst->IsNewInstance() || new_instance_inst->IsNewArray())) {
Igor Murashkindd018df2017-08-09 10:38:31 -07001350 if (ignore_inputs) {
1351 // If inputs are ignored, simply check if the predecessor is
1352 // *any* HNewInstance/HNewArray.
1353 //
1354 // Inputs are normally only ignored for prepare_for_register_allocation,
1355 // at which point *any* prior HNewInstance/Array can be considered
1356 // associated.
1357 return new_instance_inst;
1358 } else {
1359 // Normal case: There must be exactly 1 input and the previous instruction
1360 // must be that input.
1361 if (InputCount() == 1u && InputAt(0) == new_instance_inst) {
1362 return new_instance_inst;
1363 }
1364 }
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001365 }
Igor Murashkindd018df2017-08-09 10:38:31 -07001366 return nullptr;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001367}
1368
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001369#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001370void H##name::Accept(HGraphVisitor* visitor) { \
1371 visitor->Visit##name(this); \
1372}
1373
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001374FOR_EACH_CONCRETE_INSTRUCTION(DEFINE_ACCEPT)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001375
1376#undef DEFINE_ACCEPT
1377
1378void HGraphVisitor::VisitInsertionOrder() {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001379 const ArenaVector<HBasicBlock*>& blocks = graph_->GetBlocks();
1380 for (HBasicBlock* block : blocks) {
David Brazdil46e2a392015-03-16 17:31:52 +00001381 if (block != nullptr) {
1382 VisitBasicBlock(block);
1383 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001384 }
1385}
1386
Roland Levillain633021e2014-10-01 14:12:25 +01001387void HGraphVisitor::VisitReversePostOrder() {
Vladimir Marko2c45bc92016-10-25 16:54:12 +01001388 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
1389 VisitBasicBlock(block);
Roland Levillain633021e2014-10-01 14:12:25 +01001390 }
1391}
1392
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001393void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001394 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001395 it.Current()->Accept(this);
1396 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001397 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001398 it.Current()->Accept(this);
1399 }
1400}
1401
Mark Mendelle82549b2015-05-06 10:55:34 -04001402HConstant* HTypeConversion::TryStaticEvaluation() const {
1403 HGraph* graph = GetBlock()->GetGraph();
1404 if (GetInput()->IsIntConstant()) {
1405 int32_t value = GetInput()->AsIntConstant()->GetValue();
1406 switch (GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001407 case DataType::Type::kInt64:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001408 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001409 case DataType::Type::kFloat32:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001410 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001411 case DataType::Type::kFloat64:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001412 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001413 default:
1414 return nullptr;
1415 }
1416 } else if (GetInput()->IsLongConstant()) {
1417 int64_t value = GetInput()->AsLongConstant()->GetValue();
1418 switch (GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001419 case DataType::Type::kInt32:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001420 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001421 case DataType::Type::kFloat32:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001422 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001423 case DataType::Type::kFloat64:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001424 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001425 default:
1426 return nullptr;
1427 }
1428 } else if (GetInput()->IsFloatConstant()) {
1429 float value = GetInput()->AsFloatConstant()->GetValue();
1430 switch (GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001431 case DataType::Type::kInt32:
Mark Mendelle82549b2015-05-06 10:55:34 -04001432 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001433 return graph->GetIntConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001434 if (value >= kPrimIntMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001435 return graph->GetIntConstant(kPrimIntMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001436 if (value <= kPrimIntMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001437 return graph->GetIntConstant(kPrimIntMin, GetDexPc());
1438 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001439 case DataType::Type::kInt64:
Mark Mendelle82549b2015-05-06 10:55:34 -04001440 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001441 return graph->GetLongConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001442 if (value >= kPrimLongMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001443 return graph->GetLongConstant(kPrimLongMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001444 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001445 return graph->GetLongConstant(kPrimLongMin, GetDexPc());
1446 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001447 case DataType::Type::kFloat64:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001448 return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001449 default:
1450 return nullptr;
1451 }
1452 } else if (GetInput()->IsDoubleConstant()) {
1453 double value = GetInput()->AsDoubleConstant()->GetValue();
1454 switch (GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001455 case DataType::Type::kInt32:
Mark Mendelle82549b2015-05-06 10:55:34 -04001456 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001457 return graph->GetIntConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001458 if (value >= kPrimIntMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001459 return graph->GetIntConstant(kPrimIntMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001460 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001461 return graph->GetIntConstant(kPrimIntMin, GetDexPc());
1462 return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001463 case DataType::Type::kInt64:
Mark Mendelle82549b2015-05-06 10:55:34 -04001464 if (std::isnan(value))
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001465 return graph->GetLongConstant(0, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001466 if (value >= kPrimLongMax)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001467 return graph->GetLongConstant(kPrimLongMax, GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001468 if (value <= kPrimLongMin)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001469 return graph->GetLongConstant(kPrimLongMin, GetDexPc());
1470 return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001471 case DataType::Type::kFloat32:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001472 return graph->GetFloatConstant(static_cast<float>(value), GetDexPc());
Mark Mendelle82549b2015-05-06 10:55:34 -04001473 default:
1474 return nullptr;
1475 }
1476 }
1477 return nullptr;
1478}
1479
Roland Levillain9240d6a2014-10-20 16:47:04 +01001480HConstant* HUnaryOperation::TryStaticEvaluation() const {
1481 if (GetInput()->IsIntConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001482 return Evaluate(GetInput()->AsIntConstant());
Roland Levillain9240d6a2014-10-20 16:47:04 +01001483 } else if (GetInput()->IsLongConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001484 return Evaluate(GetInput()->AsLongConstant());
Roland Levillain31dd3d62016-02-16 12:21:02 +00001485 } else if (kEnableFloatingPointStaticEvaluation) {
1486 if (GetInput()->IsFloatConstant()) {
1487 return Evaluate(GetInput()->AsFloatConstant());
1488 } else if (GetInput()->IsDoubleConstant()) {
1489 return Evaluate(GetInput()->AsDoubleConstant());
1490 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01001491 }
1492 return nullptr;
1493}
1494
1495HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillaine53bd812016-02-24 14:54:18 +00001496 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
1497 return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsIntConstant());
Roland Levillain9867bc72015-08-05 10:21:34 +01001498 } else if (GetLeft()->IsLongConstant()) {
1499 if (GetRight()->IsIntConstant()) {
Roland Levillaine53bd812016-02-24 14:54:18 +00001500 // The binop(long, int) case is only valid for shifts and rotations.
1501 DCHECK(IsShl() || IsShr() || IsUShr() || IsRor()) << DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01001502 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsIntConstant());
1503 } else if (GetRight()->IsLongConstant()) {
1504 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsLongConstant());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +00001505 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00001506 } else if (GetLeft()->IsNullConstant() && GetRight()->IsNullConstant()) {
Roland Levillaine53bd812016-02-24 14:54:18 +00001507 // The binop(null, null) case is only valid for equal and not-equal conditions.
1508 DCHECK(IsEqual() || IsNotEqual()) << DebugName();
Vladimir Marko9e23df52015-11-10 17:14:35 +00001509 return Evaluate(GetLeft()->AsNullConstant(), GetRight()->AsNullConstant());
Roland Levillain31dd3d62016-02-16 12:21:02 +00001510 } else if (kEnableFloatingPointStaticEvaluation) {
1511 if (GetLeft()->IsFloatConstant() && GetRight()->IsFloatConstant()) {
1512 return Evaluate(GetLeft()->AsFloatConstant(), GetRight()->AsFloatConstant());
1513 } else if (GetLeft()->IsDoubleConstant() && GetRight()->IsDoubleConstant()) {
1514 return Evaluate(GetLeft()->AsDoubleConstant(), GetRight()->AsDoubleConstant());
1515 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001516 }
1517 return nullptr;
1518}
Dave Allison20dfc792014-06-16 20:44:29 -07001519
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001520HConstant* HBinaryOperation::GetConstantRight() const {
1521 if (GetRight()->IsConstant()) {
1522 return GetRight()->AsConstant();
1523 } else if (IsCommutative() && GetLeft()->IsConstant()) {
1524 return GetLeft()->AsConstant();
1525 } else {
1526 return nullptr;
1527 }
1528}
1529
1530// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001531// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001532HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
1533 HInstruction* most_constant_right = GetConstantRight();
1534 if (most_constant_right == nullptr) {
1535 return nullptr;
1536 } else if (most_constant_right == GetLeft()) {
1537 return GetRight();
1538 } else {
1539 return GetLeft();
1540 }
1541}
1542
Roland Levillain31dd3d62016-02-16 12:21:02 +00001543std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs) {
1544 switch (rhs) {
1545 case ComparisonBias::kNoBias:
1546 return os << "no_bias";
1547 case ComparisonBias::kGtBias:
1548 return os << "gt_bias";
1549 case ComparisonBias::kLtBias:
1550 return os << "lt_bias";
1551 default:
1552 LOG(FATAL) << "Unknown ComparisonBias: " << static_cast<int>(rhs);
1553 UNREACHABLE();
1554 }
1555}
1556
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001557bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
1558 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001559}
1560
Vladimir Marko372f10e2016-05-17 16:30:10 +01001561bool HInstruction::Equals(const HInstruction* other) const {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001562 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001563 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001564 if (!InstructionDataEquals(other)) return false;
1565 if (GetType() != other->GetType()) return false;
Vladimir Markoe9004912016-06-16 16:50:52 +01001566 HConstInputsRef inputs = GetInputs();
1567 HConstInputsRef other_inputs = other->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +01001568 if (inputs.size() != other_inputs.size()) return false;
1569 for (size_t i = 0; i != inputs.size(); ++i) {
1570 if (inputs[i] != other_inputs[i]) return false;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001571 }
Vladimir Marko372f10e2016-05-17 16:30:10 +01001572
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001573 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001574 return true;
1575}
1576
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001577std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
1578#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
1579 switch (rhs) {
1580 FOR_EACH_INSTRUCTION(DECLARE_CASE)
1581 default:
1582 os << "Unknown instruction kind " << static_cast<int>(rhs);
1583 break;
1584 }
1585#undef DECLARE_CASE
1586 return os;
1587}
1588
Alexandre Rames22aa54b2016-10-18 09:32:29 +01001589void HInstruction::MoveBefore(HInstruction* cursor, bool do_checks) {
1590 if (do_checks) {
1591 DCHECK(!IsPhi());
1592 DCHECK(!IsControlFlow());
1593 DCHECK(CanBeMoved() ||
1594 // HShouldDeoptimizeFlag can only be moved by CHAGuardOptimization.
1595 IsShouldDeoptimizeFlag());
1596 DCHECK(!cursor->IsPhi());
1597 }
David Brazdild6c205e2016-06-07 14:20:52 +01001598
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001599 next_->previous_ = previous_;
1600 if (previous_ != nullptr) {
1601 previous_->next_ = next_;
1602 }
1603 if (block_->instructions_.first_instruction_ == this) {
1604 block_->instructions_.first_instruction_ = next_;
1605 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001606 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001607
1608 previous_ = cursor->previous_;
1609 if (previous_ != nullptr) {
1610 previous_->next_ = this;
1611 }
1612 next_ = cursor;
1613 cursor->previous_ = this;
1614 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001615
1616 if (block_->instructions_.first_instruction_ == cursor) {
1617 block_->instructions_.first_instruction_ = this;
1618 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001619}
1620
Vladimir Markofb337ea2015-11-25 15:25:10 +00001621void HInstruction::MoveBeforeFirstUserAndOutOfLoops() {
1622 DCHECK(!CanThrow());
1623 DCHECK(!HasSideEffects());
1624 DCHECK(!HasEnvironmentUses());
1625 DCHECK(HasNonEnvironmentUses());
1626 DCHECK(!IsPhi()); // Makes no sense for Phi.
1627 DCHECK_EQ(InputCount(), 0u);
1628
1629 // Find the target block.
Vladimir Marko46817b82016-03-29 12:21:58 +01001630 auto uses_it = GetUses().begin();
1631 auto uses_end = GetUses().end();
1632 HBasicBlock* target_block = uses_it->GetUser()->GetBlock();
1633 ++uses_it;
1634 while (uses_it != uses_end && uses_it->GetUser()->GetBlock() == target_block) {
1635 ++uses_it;
Vladimir Markofb337ea2015-11-25 15:25:10 +00001636 }
Vladimir Marko46817b82016-03-29 12:21:58 +01001637 if (uses_it != uses_end) {
Vladimir Markofb337ea2015-11-25 15:25:10 +00001638 // This instruction has uses in two or more blocks. Find the common dominator.
1639 CommonDominator finder(target_block);
Vladimir Marko46817b82016-03-29 12:21:58 +01001640 for (; uses_it != uses_end; ++uses_it) {
1641 finder.Update(uses_it->GetUser()->GetBlock());
Vladimir Markofb337ea2015-11-25 15:25:10 +00001642 }
1643 target_block = finder.Get();
1644 DCHECK(target_block != nullptr);
1645 }
1646 // Move to the first dominator not in a loop.
1647 while (target_block->IsInLoop()) {
1648 target_block = target_block->GetDominator();
1649 DCHECK(target_block != nullptr);
1650 }
1651
1652 // Find insertion position.
1653 HInstruction* insert_pos = nullptr;
Vladimir Marko46817b82016-03-29 12:21:58 +01001654 for (const HUseListNode<HInstruction*>& use : GetUses()) {
1655 if (use.GetUser()->GetBlock() == target_block &&
1656 (insert_pos == nullptr || use.GetUser()->StrictlyDominates(insert_pos))) {
1657 insert_pos = use.GetUser();
Vladimir Markofb337ea2015-11-25 15:25:10 +00001658 }
1659 }
1660 if (insert_pos == nullptr) {
1661 // No user in `target_block`, insert before the control flow instruction.
1662 insert_pos = target_block->GetLastInstruction();
1663 DCHECK(insert_pos->IsControlFlow());
1664 // Avoid splitting HCondition from HIf to prevent unnecessary materialization.
1665 if (insert_pos->IsIf()) {
1666 HInstruction* if_input = insert_pos->AsIf()->InputAt(0);
1667 if (if_input == insert_pos->GetPrevious()) {
1668 insert_pos = if_input;
1669 }
1670 }
1671 }
1672 MoveBefore(insert_pos);
1673}
1674
David Brazdilfc6a86a2015-06-26 10:33:45 +00001675HBasicBlock* HBasicBlock::SplitBefore(HInstruction* cursor) {
David Brazdil9bc43612015-11-05 21:25:24 +00001676 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented.";
David Brazdilfc6a86a2015-06-26 10:33:45 +00001677 DCHECK_EQ(cursor->GetBlock(), this);
1678
Vladimir Markoca6fff82017-10-03 14:49:14 +01001679 HBasicBlock* new_block =
1680 new (GetGraph()->GetAllocator()) HBasicBlock(GetGraph(), cursor->GetDexPc());
David Brazdilfc6a86a2015-06-26 10:33:45 +00001681 new_block->instructions_.first_instruction_ = cursor;
1682 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1683 instructions_.last_instruction_ = cursor->previous_;
1684 if (cursor->previous_ == nullptr) {
1685 instructions_.first_instruction_ = nullptr;
1686 } else {
1687 cursor->previous_->next_ = nullptr;
1688 cursor->previous_ = nullptr;
1689 }
1690
1691 new_block->instructions_.SetBlockOfInstructions(new_block);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001692 AddInstruction(new (GetGraph()->GetAllocator()) HGoto(new_block->GetDexPc()));
David Brazdilfc6a86a2015-06-26 10:33:45 +00001693
Vladimir Marko60584552015-09-03 13:35:12 +00001694 for (HBasicBlock* successor : GetSuccessors()) {
Vladimir Marko60584552015-09-03 13:35:12 +00001695 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001696 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001697 new_block->successors_.swap(successors_);
1698 DCHECK(successors_.empty());
David Brazdilfc6a86a2015-06-26 10:33:45 +00001699 AddSuccessor(new_block);
1700
David Brazdil56e1acc2015-06-30 15:41:36 +01001701 GetGraph()->AddBlock(new_block);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001702 return new_block;
1703}
1704
David Brazdild7558da2015-09-22 13:04:14 +01001705HBasicBlock* HBasicBlock::CreateImmediateDominator() {
David Brazdil9bc43612015-11-05 21:25:24 +00001706 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented.";
David Brazdild7558da2015-09-22 13:04:14 +01001707 DCHECK(!IsCatchBlock()) << "Support for updating try/catch information not implemented.";
1708
Vladimir Markoca6fff82017-10-03 14:49:14 +01001709 HBasicBlock* new_block = new (GetGraph()->GetAllocator()) HBasicBlock(GetGraph(), GetDexPc());
David Brazdild7558da2015-09-22 13:04:14 +01001710
1711 for (HBasicBlock* predecessor : GetPredecessors()) {
David Brazdild7558da2015-09-22 13:04:14 +01001712 predecessor->successors_[predecessor->GetSuccessorIndexOf(this)] = new_block;
1713 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001714 new_block->predecessors_.swap(predecessors_);
1715 DCHECK(predecessors_.empty());
David Brazdild7558da2015-09-22 13:04:14 +01001716 AddPredecessor(new_block);
1717
1718 GetGraph()->AddBlock(new_block);
1719 return new_block;
1720}
1721
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001722HBasicBlock* HBasicBlock::SplitBeforeForInlining(HInstruction* cursor) {
1723 DCHECK_EQ(cursor->GetBlock(), this);
1724
Vladimir Markoca6fff82017-10-03 14:49:14 +01001725 HBasicBlock* new_block =
1726 new (GetGraph()->GetAllocator()) HBasicBlock(GetGraph(), cursor->GetDexPc());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001727 new_block->instructions_.first_instruction_ = cursor;
1728 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1729 instructions_.last_instruction_ = cursor->previous_;
1730 if (cursor->previous_ == nullptr) {
1731 instructions_.first_instruction_ = nullptr;
1732 } else {
1733 cursor->previous_->next_ = nullptr;
1734 cursor->previous_ = nullptr;
1735 }
1736
1737 new_block->instructions_.SetBlockOfInstructions(new_block);
1738
1739 for (HBasicBlock* successor : GetSuccessors()) {
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001740 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
1741 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001742 new_block->successors_.swap(successors_);
1743 DCHECK(successors_.empty());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001744
1745 for (HBasicBlock* dominated : GetDominatedBlocks()) {
1746 dominated->dominator_ = new_block;
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001747 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001748 new_block->dominated_blocks_.swap(dominated_blocks_);
1749 DCHECK(dominated_blocks_.empty());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001750 return new_block;
1751}
1752
1753HBasicBlock* HBasicBlock::SplitAfterForInlining(HInstruction* cursor) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001754 DCHECK(!cursor->IsControlFlow());
1755 DCHECK_NE(instructions_.last_instruction_, cursor);
1756 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001757
Vladimir Markoca6fff82017-10-03 14:49:14 +01001758 HBasicBlock* new_block = new (GetGraph()->GetAllocator()) HBasicBlock(GetGraph(), GetDexPc());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001759 new_block->instructions_.first_instruction_ = cursor->GetNext();
1760 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1761 cursor->next_->previous_ = nullptr;
1762 cursor->next_ = nullptr;
1763 instructions_.last_instruction_ = cursor;
1764
1765 new_block->instructions_.SetBlockOfInstructions(new_block);
Vladimir Marko60584552015-09-03 13:35:12 +00001766 for (HBasicBlock* successor : GetSuccessors()) {
Vladimir Marko60584552015-09-03 13:35:12 +00001767 successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001768 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001769 new_block->successors_.swap(successors_);
1770 DCHECK(successors_.empty());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001771
Vladimir Marko60584552015-09-03 13:35:12 +00001772 for (HBasicBlock* dominated : GetDominatedBlocks()) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001773 dominated->dominator_ = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001774 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00001775 new_block->dominated_blocks_.swap(dominated_blocks_);
1776 DCHECK(dominated_blocks_.empty());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001777 return new_block;
1778}
1779
David Brazdilec16f792015-08-19 15:04:01 +01001780const HTryBoundary* HBasicBlock::ComputeTryEntryOfSuccessors() const {
David Brazdilffee3d32015-07-06 11:48:53 +01001781 if (EndsWithTryBoundary()) {
1782 HTryBoundary* try_boundary = GetLastInstruction()->AsTryBoundary();
1783 if (try_boundary->IsEntry()) {
David Brazdilec16f792015-08-19 15:04:01 +01001784 DCHECK(!IsTryBlock());
David Brazdilffee3d32015-07-06 11:48:53 +01001785 return try_boundary;
1786 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001787 DCHECK(IsTryBlock());
1788 DCHECK(try_catch_information_->GetTryEntry().HasSameExceptionHandlersAs(*try_boundary));
David Brazdilffee3d32015-07-06 11:48:53 +01001789 return nullptr;
1790 }
David Brazdilec16f792015-08-19 15:04:01 +01001791 } else if (IsTryBlock()) {
1792 return &try_catch_information_->GetTryEntry();
David Brazdilffee3d32015-07-06 11:48:53 +01001793 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001794 return nullptr;
David Brazdilffee3d32015-07-06 11:48:53 +01001795 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00001796}
1797
David Brazdild7558da2015-09-22 13:04:14 +01001798bool HBasicBlock::HasThrowingInstructions() const {
1799 for (HInstructionIterator it(GetInstructions()); !it.Done(); it.Advance()) {
1800 if (it.Current()->CanThrow()) {
1801 return true;
1802 }
1803 }
1804 return false;
1805}
1806
David Brazdilfc6a86a2015-06-26 10:33:45 +00001807static bool HasOnlyOneInstruction(const HBasicBlock& block) {
1808 return block.GetPhis().IsEmpty()
1809 && !block.GetInstructions().IsEmpty()
1810 && block.GetFirstInstruction() == block.GetLastInstruction();
1811}
1812
David Brazdil46e2a392015-03-16 17:31:52 +00001813bool HBasicBlock::IsSingleGoto() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001814 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsGoto();
1815}
1816
Mads Ager16e52892017-07-14 13:11:37 +02001817bool HBasicBlock::IsSingleReturn() const {
1818 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsReturn();
1819}
1820
Mingyao Yang46721ef2017-10-05 14:45:17 -07001821bool HBasicBlock::IsSingleReturnOrReturnVoidAllowingPhis() const {
1822 return (GetFirstInstruction() == GetLastInstruction()) &&
1823 (GetLastInstruction()->IsReturn() || GetLastInstruction()->IsReturnVoid());
1824}
1825
David Brazdilfc6a86a2015-06-26 10:33:45 +00001826bool HBasicBlock::IsSingleTryBoundary() const {
1827 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsTryBoundary();
David Brazdil46e2a392015-03-16 17:31:52 +00001828}
1829
David Brazdil8d5b8b22015-03-24 10:51:52 +00001830bool HBasicBlock::EndsWithControlFlowInstruction() const {
1831 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
1832}
1833
David Brazdilb2bd1c52015-03-25 11:17:37 +00001834bool HBasicBlock::EndsWithIf() const {
1835 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
1836}
1837
David Brazdilffee3d32015-07-06 11:48:53 +01001838bool HBasicBlock::EndsWithTryBoundary() const {
1839 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsTryBoundary();
1840}
1841
David Brazdilb2bd1c52015-03-25 11:17:37 +00001842bool HBasicBlock::HasSinglePhi() const {
1843 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
1844}
1845
David Brazdild26a4112015-11-10 11:07:31 +00001846ArrayRef<HBasicBlock* const> HBasicBlock::GetNormalSuccessors() const {
1847 if (EndsWithTryBoundary()) {
1848 // The normal-flow successor of HTryBoundary is always stored at index zero.
1849 DCHECK_EQ(successors_[0], GetLastInstruction()->AsTryBoundary()->GetNormalFlowSuccessor());
1850 return ArrayRef<HBasicBlock* const>(successors_).SubArray(0u, 1u);
1851 } else {
1852 // All successors of blocks not ending with TryBoundary are normal.
1853 return ArrayRef<HBasicBlock* const>(successors_);
1854 }
1855}
1856
1857ArrayRef<HBasicBlock* const> HBasicBlock::GetExceptionalSuccessors() const {
1858 if (EndsWithTryBoundary()) {
1859 return GetLastInstruction()->AsTryBoundary()->GetExceptionHandlers();
1860 } else {
1861 // Blocks not ending with TryBoundary do not have exceptional successors.
1862 return ArrayRef<HBasicBlock* const>();
1863 }
1864}
1865
David Brazdilffee3d32015-07-06 11:48:53 +01001866bool HTryBoundary::HasSameExceptionHandlersAs(const HTryBoundary& other) const {
David Brazdild26a4112015-11-10 11:07:31 +00001867 ArrayRef<HBasicBlock* const> handlers1 = GetExceptionHandlers();
1868 ArrayRef<HBasicBlock* const> handlers2 = other.GetExceptionHandlers();
1869
1870 size_t length = handlers1.size();
1871 if (length != handlers2.size()) {
David Brazdilffee3d32015-07-06 11:48:53 +01001872 return false;
1873 }
1874
David Brazdilb618ade2015-07-29 10:31:29 +01001875 // Exception handlers need to be stored in the same order.
David Brazdild26a4112015-11-10 11:07:31 +00001876 for (size_t i = 0; i < length; ++i) {
1877 if (handlers1[i] != handlers2[i]) {
David Brazdilffee3d32015-07-06 11:48:53 +01001878 return false;
1879 }
1880 }
1881 return true;
1882}
1883
David Brazdil2d7352b2015-04-20 14:52:42 +01001884size_t HInstructionList::CountSize() const {
1885 size_t size = 0;
1886 HInstruction* current = first_instruction_;
1887 for (; current != nullptr; current = current->GetNext()) {
1888 size++;
1889 }
1890 return size;
1891}
1892
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001893void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
1894 for (HInstruction* current = first_instruction_;
1895 current != nullptr;
1896 current = current->GetNext()) {
1897 current->SetBlock(block);
1898 }
1899}
1900
1901void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
1902 DCHECK(Contains(cursor));
1903 if (!instruction_list.IsEmpty()) {
1904 if (cursor == last_instruction_) {
1905 last_instruction_ = instruction_list.last_instruction_;
1906 } else {
1907 cursor->next_->previous_ = instruction_list.last_instruction_;
1908 }
1909 instruction_list.last_instruction_->next_ = cursor->next_;
1910 cursor->next_ = instruction_list.first_instruction_;
1911 instruction_list.first_instruction_->previous_ = cursor;
1912 }
1913}
1914
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001915void HInstructionList::AddBefore(HInstruction* cursor, const HInstructionList& instruction_list) {
1916 DCHECK(Contains(cursor));
1917 if (!instruction_list.IsEmpty()) {
1918 if (cursor == first_instruction_) {
1919 first_instruction_ = instruction_list.first_instruction_;
1920 } else {
1921 cursor->previous_->next_ = instruction_list.first_instruction_;
1922 }
1923 instruction_list.last_instruction_->next_ = cursor;
1924 instruction_list.first_instruction_->previous_ = cursor->previous_;
1925 cursor->previous_ = instruction_list.last_instruction_;
1926 }
1927}
1928
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001929void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +00001930 if (IsEmpty()) {
1931 first_instruction_ = instruction_list.first_instruction_;
1932 last_instruction_ = instruction_list.last_instruction_;
1933 } else {
1934 AddAfter(last_instruction_, instruction_list);
1935 }
1936}
1937
David Brazdil04ff4e82015-12-10 13:54:52 +00001938// Should be called on instructions in a dead block in post order. This method
1939// assumes `insn` has been removed from all users with the exception of catch
1940// phis because of missing exceptional edges in the graph. It removes the
1941// instruction from catch phi uses, together with inputs of other catch phis in
1942// the catch block at the same index, as these must be dead too.
1943static void RemoveUsesOfDeadInstruction(HInstruction* insn) {
1944 DCHECK(!insn->HasEnvironmentUses());
1945 while (insn->HasNonEnvironmentUses()) {
Vladimir Marko46817b82016-03-29 12:21:58 +01001946 const HUseListNode<HInstruction*>& use = insn->GetUses().front();
1947 size_t use_index = use.GetIndex();
1948 HBasicBlock* user_block = use.GetUser()->GetBlock();
1949 DCHECK(use.GetUser()->IsPhi() && user_block->IsCatchBlock());
David Brazdil04ff4e82015-12-10 13:54:52 +00001950 for (HInstructionIterator phi_it(user_block->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1951 phi_it.Current()->AsPhi()->RemoveInputAt(use_index);
1952 }
1953 }
1954}
1955
David Brazdil2d7352b2015-04-20 14:52:42 +01001956void HBasicBlock::DisconnectAndDelete() {
1957 // Dominators must be removed after all the blocks they dominate. This way
1958 // a loop header is removed last, a requirement for correct loop information
1959 // iteration.
Vladimir Marko60584552015-09-03 13:35:12 +00001960 DCHECK(dominated_blocks_.empty());
David Brazdil46e2a392015-03-16 17:31:52 +00001961
David Brazdil9eeebf62016-03-24 11:18:15 +00001962 // The following steps gradually remove the block from all its dependants in
1963 // post order (b/27683071).
1964
1965 // (1) Store a basic block that we'll use in step (5) to find loops to be updated.
1966 // We need to do this before step (4) which destroys the predecessor list.
1967 HBasicBlock* loop_update_start = this;
1968 if (IsLoopHeader()) {
1969 HLoopInformation* loop_info = GetLoopInformation();
1970 // All other blocks in this loop should have been removed because the header
1971 // was their dominator.
1972 // Note that we do not remove `this` from `loop_info` as it is unreachable.
1973 DCHECK(!loop_info->IsIrreducible());
1974 DCHECK_EQ(loop_info->GetBlocks().NumSetBits(), 1u);
1975 DCHECK_EQ(static_cast<uint32_t>(loop_info->GetBlocks().GetHighestBitSet()), GetBlockId());
1976 loop_update_start = loop_info->GetPreHeader();
David Brazdil2d7352b2015-04-20 14:52:42 +01001977 }
1978
David Brazdil9eeebf62016-03-24 11:18:15 +00001979 // (2) Disconnect the block from its successors and update their phis.
1980 for (HBasicBlock* successor : successors_) {
1981 // Delete this block from the list of predecessors.
1982 size_t this_index = successor->GetPredecessorIndexOf(this);
1983 successor->predecessors_.erase(successor->predecessors_.begin() + this_index);
1984
1985 // Check that `successor` has other predecessors, otherwise `this` is the
1986 // dominator of `successor` which violates the order DCHECKed at the top.
1987 DCHECK(!successor->predecessors_.empty());
1988
1989 // Remove this block's entries in the successor's phis. Skip exceptional
1990 // successors because catch phi inputs do not correspond to predecessor
1991 // blocks but throwing instructions. The inputs of the catch phis will be
1992 // updated in step (3).
1993 if (!successor->IsCatchBlock()) {
1994 if (successor->predecessors_.size() == 1u) {
1995 // The successor has just one predecessor left. Replace phis with the only
1996 // remaining input.
1997 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1998 HPhi* phi = phi_it.Current()->AsPhi();
1999 phi->ReplaceWith(phi->InputAt(1 - this_index));
2000 successor->RemovePhi(phi);
2001 }
2002 } else {
2003 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
2004 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
2005 }
2006 }
2007 }
2008 }
2009 successors_.clear();
2010
2011 // (3) Remove instructions and phis. Instructions should have no remaining uses
2012 // except in catch phis. If an instruction is used by a catch phi at `index`,
2013 // remove `index`-th input of all phis in the catch block since they are
2014 // guaranteed dead. Note that we may miss dead inputs this way but the
2015 // graph will always remain consistent.
2016 for (HBackwardInstructionIterator it(GetInstructions()); !it.Done(); it.Advance()) {
2017 HInstruction* insn = it.Current();
2018 RemoveUsesOfDeadInstruction(insn);
2019 RemoveInstruction(insn);
2020 }
2021 for (HInstructionIterator it(GetPhis()); !it.Done(); it.Advance()) {
2022 HPhi* insn = it.Current()->AsPhi();
2023 RemoveUsesOfDeadInstruction(insn);
2024 RemovePhi(insn);
2025 }
2026
2027 // (4) Disconnect the block from its predecessors and update their
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002028 // control-flow instructions.
Vladimir Marko60584552015-09-03 13:35:12 +00002029 for (HBasicBlock* predecessor : predecessors_) {
David Brazdil9eeebf62016-03-24 11:18:15 +00002030 // We should not see any back edges as they would have been removed by step (3).
2031 DCHECK(!IsInLoop() || !GetLoopInformation()->IsBackEdge(*predecessor));
2032
David Brazdil2d7352b2015-04-20 14:52:42 +01002033 HInstruction* last_instruction = predecessor->GetLastInstruction();
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002034 if (last_instruction->IsTryBoundary() && !IsCatchBlock()) {
2035 // This block is the only normal-flow successor of the TryBoundary which
2036 // makes `predecessor` dead. Since DCE removes blocks in post order,
2037 // exception handlers of this TryBoundary were already visited and any
2038 // remaining handlers therefore must be live. We remove `predecessor` from
2039 // their list of predecessors.
2040 DCHECK_EQ(last_instruction->AsTryBoundary()->GetNormalFlowSuccessor(), this);
2041 while (predecessor->GetSuccessors().size() > 1) {
2042 HBasicBlock* handler = predecessor->GetSuccessors()[1];
2043 DCHECK(handler->IsCatchBlock());
2044 predecessor->RemoveSuccessor(handler);
2045 handler->RemovePredecessor(predecessor);
2046 }
2047 }
2048
David Brazdil2d7352b2015-04-20 14:52:42 +01002049 predecessor->RemoveSuccessor(this);
Mark Mendellfe57faa2015-09-18 09:26:15 -04002050 uint32_t num_pred_successors = predecessor->GetSuccessors().size();
2051 if (num_pred_successors == 1u) {
2052 // If we have one successor after removing one, then we must have
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002053 // had an HIf, HPackedSwitch or HTryBoundary, as they have more than one
2054 // successor. Replace those with a HGoto.
2055 DCHECK(last_instruction->IsIf() ||
2056 last_instruction->IsPackedSwitch() ||
2057 (last_instruction->IsTryBoundary() && IsCatchBlock()));
Mark Mendellfe57faa2015-09-18 09:26:15 -04002058 predecessor->RemoveInstruction(last_instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002059 predecessor->AddInstruction(new (graph_->GetAllocator()) HGoto(last_instruction->GetDexPc()));
Mark Mendellfe57faa2015-09-18 09:26:15 -04002060 } else if (num_pred_successors == 0u) {
David Brazdil2d7352b2015-04-20 14:52:42 +01002061 // The predecessor has no remaining successors and therefore must be dead.
2062 // We deliberately leave it without a control-flow instruction so that the
David Brazdilbadd8262016-02-02 16:28:56 +00002063 // GraphChecker fails unless it is not removed during the pass too.
Mark Mendellfe57faa2015-09-18 09:26:15 -04002064 predecessor->RemoveInstruction(last_instruction);
2065 } else {
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002066 // There are multiple successors left. The removed block might be a successor
2067 // of a PackedSwitch which will be completely removed (perhaps replaced with
2068 // a Goto), or we are deleting a catch block from a TryBoundary. In either
2069 // case, leave `last_instruction` as is for now.
2070 DCHECK(last_instruction->IsPackedSwitch() ||
2071 (last_instruction->IsTryBoundary() && IsCatchBlock()));
David Brazdil2d7352b2015-04-20 14:52:42 +01002072 }
David Brazdil46e2a392015-03-16 17:31:52 +00002073 }
Vladimir Marko60584552015-09-03 13:35:12 +00002074 predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01002075
David Brazdil9eeebf62016-03-24 11:18:15 +00002076 // (5) Remove the block from all loops it is included in. Skip the inner-most
2077 // loop if this is the loop header (see definition of `loop_update_start`)
2078 // because the loop header's predecessor list has been destroyed in step (4).
2079 for (HLoopInformationOutwardIterator it(*loop_update_start); !it.Done(); it.Advance()) {
2080 HLoopInformation* loop_info = it.Current();
2081 loop_info->Remove(this);
2082 if (loop_info->IsBackEdge(*this)) {
2083 // If this was the last back edge of the loop, we deliberately leave the
2084 // loop in an inconsistent state and will fail GraphChecker unless the
2085 // entire loop is removed during the pass.
2086 loop_info->RemoveBackEdge(this);
David Brazdil2d7352b2015-04-20 14:52:42 +01002087 }
2088 }
David Brazdil2d7352b2015-04-20 14:52:42 +01002089
David Brazdil9eeebf62016-03-24 11:18:15 +00002090 // (6) Disconnect from the dominator.
David Brazdil2d7352b2015-04-20 14:52:42 +01002091 dominator_->RemoveDominatedBlock(this);
2092 SetDominator(nullptr);
2093
David Brazdil9eeebf62016-03-24 11:18:15 +00002094 // (7) Delete from the graph, update reverse post order.
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002095 graph_->DeleteDeadEmptyBlock(this);
David Brazdil2d7352b2015-04-20 14:52:42 +01002096 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002097}
2098
Aart Bik6b69e0a2017-01-11 10:20:43 -08002099void HBasicBlock::MergeInstructionsWith(HBasicBlock* other) {
2100 DCHECK(EndsWithControlFlowInstruction());
2101 RemoveInstruction(GetLastInstruction());
2102 instructions_.Add(other->GetInstructions());
2103 other->instructions_.SetBlockOfInstructions(this);
2104 other->instructions_.Clear();
2105}
2106
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002107void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01002108 DCHECK_EQ(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00002109 DCHECK(ContainsElement(dominated_blocks_, other));
2110 DCHECK_EQ(GetSingleSuccessor(), other);
2111 DCHECK_EQ(other->GetSinglePredecessor(), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002112 DCHECK(other->GetPhis().IsEmpty());
2113
David Brazdil2d7352b2015-04-20 14:52:42 +01002114 // Move instructions from `other` to `this`.
Aart Bik6b69e0a2017-01-11 10:20:43 -08002115 MergeInstructionsWith(other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002116
David Brazdil2d7352b2015-04-20 14:52:42 +01002117 // Remove `other` from the loops it is included in.
2118 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
2119 HLoopInformation* loop_info = it.Current();
2120 loop_info->Remove(other);
2121 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002122 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01002123 }
2124 }
2125
2126 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00002127 successors_.clear();
Vladimir Marko661b69b2016-11-09 14:11:37 +00002128 for (HBasicBlock* successor : other->GetSuccessors()) {
2129 successor->predecessors_[successor->GetPredecessorIndexOf(other)] = this;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002130 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00002131 successors_.swap(other->successors_);
2132 DCHECK(other->successors_.empty());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002133
David Brazdil2d7352b2015-04-20 14:52:42 +01002134 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00002135 RemoveDominatedBlock(other);
2136 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
David Brazdil2d7352b2015-04-20 14:52:42 +01002137 dominated->SetDominator(this);
2138 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00002139 dominated_blocks_.insert(
2140 dominated_blocks_.end(), other->dominated_blocks_.begin(), other->dominated_blocks_.end());
Vladimir Marko60584552015-09-03 13:35:12 +00002141 other->dominated_blocks_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01002142 other->dominator_ = nullptr;
2143
2144 // Clear the list of predecessors of `other` in preparation of deleting it.
Vladimir Marko60584552015-09-03 13:35:12 +00002145 other->predecessors_.clear();
David Brazdil2d7352b2015-04-20 14:52:42 +01002146
2147 // Delete `other` from the graph. The function updates reverse post order.
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002148 graph_->DeleteDeadEmptyBlock(other);
David Brazdil2d7352b2015-04-20 14:52:42 +01002149 other->SetGraph(nullptr);
2150}
2151
2152void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
2153 DCHECK_NE(GetGraph(), other->GetGraph());
Vladimir Marko60584552015-09-03 13:35:12 +00002154 DCHECK(GetDominatedBlocks().empty());
2155 DCHECK(GetSuccessors().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01002156 DCHECK(!EndsWithControlFlowInstruction());
Vladimir Marko60584552015-09-03 13:35:12 +00002157 DCHECK(other->GetSinglePredecessor()->IsEntryBlock());
David Brazdil2d7352b2015-04-20 14:52:42 +01002158 DCHECK(other->GetPhis().IsEmpty());
2159 DCHECK(!other->IsInLoop());
2160
2161 // Move instructions from `other` to `this`.
2162 instructions_.Add(other->GetInstructions());
2163 other->instructions_.SetBlockOfInstructions(this);
2164
2165 // Update links to the successors of `other`.
Vladimir Marko60584552015-09-03 13:35:12 +00002166 successors_.clear();
Vladimir Marko661b69b2016-11-09 14:11:37 +00002167 for (HBasicBlock* successor : other->GetSuccessors()) {
2168 successor->predecessors_[successor->GetPredecessorIndexOf(other)] = this;
David Brazdil2d7352b2015-04-20 14:52:42 +01002169 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00002170 successors_.swap(other->successors_);
2171 DCHECK(other->successors_.empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01002172
2173 // Update the dominator tree.
Vladimir Marko60584552015-09-03 13:35:12 +00002174 for (HBasicBlock* dominated : other->GetDominatedBlocks()) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002175 dominated->SetDominator(this);
2176 }
Vladimir Marko661b69b2016-11-09 14:11:37 +00002177 dominated_blocks_.insert(
2178 dominated_blocks_.end(), other->dominated_blocks_.begin(), other->dominated_blocks_.end());
Vladimir Marko60584552015-09-03 13:35:12 +00002179 other->dominated_blocks_.clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002180 other->dominator_ = nullptr;
2181 other->graph_ = nullptr;
2182}
2183
2184void HBasicBlock::ReplaceWith(HBasicBlock* other) {
Vladimir Marko60584552015-09-03 13:35:12 +00002185 while (!GetPredecessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002186 HBasicBlock* predecessor = GetPredecessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002187 predecessor->ReplaceSuccessor(this, other);
2188 }
Vladimir Marko60584552015-09-03 13:35:12 +00002189 while (!GetSuccessors().empty()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002190 HBasicBlock* successor = GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002191 successor->ReplacePredecessor(this, other);
2192 }
Vladimir Marko60584552015-09-03 13:35:12 +00002193 for (HBasicBlock* dominated : GetDominatedBlocks()) {
2194 other->AddDominatedBlock(dominated);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002195 }
2196 GetDominator()->ReplaceDominatedBlock(this, other);
2197 other->SetDominator(GetDominator());
2198 dominator_ = nullptr;
2199 graph_ = nullptr;
2200}
2201
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002202void HGraph::DeleteDeadEmptyBlock(HBasicBlock* block) {
David Brazdil2d7352b2015-04-20 14:52:42 +01002203 DCHECK_EQ(block->GetGraph(), this);
Vladimir Marko60584552015-09-03 13:35:12 +00002204 DCHECK(block->GetSuccessors().empty());
2205 DCHECK(block->GetPredecessors().empty());
2206 DCHECK(block->GetDominatedBlocks().empty());
David Brazdil2d7352b2015-04-20 14:52:42 +01002207 DCHECK(block->GetDominator() == nullptr);
David Brazdil8a7c0fe2015-11-02 20:24:55 +00002208 DCHECK(block->GetInstructions().IsEmpty());
2209 DCHECK(block->GetPhis().IsEmpty());
David Brazdil2d7352b2015-04-20 14:52:42 +01002210
David Brazdilc7af85d2015-05-26 12:05:55 +01002211 if (block->IsExitBlock()) {
Serguei Katkov7ba99662016-03-02 16:25:36 +06002212 SetExitBlock(nullptr);
David Brazdilc7af85d2015-05-26 12:05:55 +01002213 }
2214
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002215 RemoveElement(reverse_post_order_, block);
2216 blocks_[block->GetBlockId()] = nullptr;
David Brazdil86ea7ee2016-02-16 09:26:07 +00002217 block->SetGraph(nullptr);
David Brazdil2d7352b2015-04-20 14:52:42 +01002218}
2219
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +00002220void HGraph::UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
2221 HBasicBlock* reference,
2222 bool replace_if_back_edge) {
2223 if (block->IsLoopHeader()) {
2224 // Clear the information of which blocks are contained in that loop. Since the
2225 // information is stored as a bit vector based on block ids, we have to update
2226 // it, as those block ids were specific to the callee graph and we are now adding
2227 // these blocks to the caller graph.
2228 block->GetLoopInformation()->ClearAllBlocks();
2229 }
2230
2231 // If not already in a loop, update the loop information.
2232 if (!block->IsInLoop()) {
2233 block->SetLoopInformation(reference->GetLoopInformation());
2234 }
2235
2236 // If the block is in a loop, update all its outward loops.
2237 HLoopInformation* loop_info = block->GetLoopInformation();
2238 if (loop_info != nullptr) {
2239 for (HLoopInformationOutwardIterator loop_it(*block);
2240 !loop_it.Done();
2241 loop_it.Advance()) {
2242 loop_it.Current()->Add(block);
2243 }
2244 if (replace_if_back_edge && loop_info->IsBackEdge(*reference)) {
2245 loop_info->ReplaceBackEdge(reference, block);
2246 }
2247 }
2248
2249 // Copy TryCatchInformation if `reference` is a try block, not if it is a catch block.
2250 TryCatchInformation* try_catch_info = reference->IsTryBlock()
2251 ? reference->GetTryCatchInformation()
2252 : nullptr;
2253 block->SetTryCatchInformation(try_catch_info);
2254}
2255
Calin Juravle2e768302015-07-28 14:41:11 +00002256HInstruction* HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
David Brazdilc7af85d2015-05-26 12:05:55 +01002257 DCHECK(HasExitBlock()) << "Unimplemented scenario";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002258 // Update the environments in this graph to have the invoke's environment
2259 // as parent.
2260 {
Vladimir Marko2c45bc92016-10-25 16:54:12 +01002261 // Skip the entry block, we do not need to update the entry's suspend check.
2262 for (HBasicBlock* block : GetReversePostOrderSkipEntryBlock()) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002263 for (HInstructionIterator instr_it(block->GetInstructions());
2264 !instr_it.Done();
2265 instr_it.Advance()) {
2266 HInstruction* current = instr_it.Current();
2267 if (current->NeedsEnvironment()) {
David Brazdildee58d62016-04-07 09:54:26 +00002268 DCHECK(current->HasEnvironment());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002269 current->GetEnvironment()->SetAndCopyParentChain(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002270 outer_graph->GetAllocator(), invoke->GetEnvironment());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002271 }
2272 }
2273 }
2274 }
2275 outer_graph->UpdateMaximumNumberOfOutVRegs(GetMaximumNumberOfOutVRegs());
Mingyao Yang69d75ff2017-02-07 13:06:06 -08002276
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002277 if (HasBoundsChecks()) {
2278 outer_graph->SetHasBoundsChecks(true);
2279 }
Mingyao Yang69d75ff2017-02-07 13:06:06 -08002280 if (HasLoops()) {
2281 outer_graph->SetHasLoops(true);
2282 }
2283 if (HasIrreducibleLoops()) {
2284 outer_graph->SetHasIrreducibleLoops(true);
2285 }
2286 if (HasTryCatch()) {
2287 outer_graph->SetHasTryCatch(true);
2288 }
Aart Bikb13c65b2017-03-21 20:14:07 -07002289 if (HasSIMD()) {
2290 outer_graph->SetHasSIMD(true);
2291 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002292
Calin Juravle2e768302015-07-28 14:41:11 +00002293 HInstruction* return_value = nullptr;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002294 if (GetBlocks().size() == 3) {
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002295 // Inliner already made sure we don't inline methods that always throw.
2296 DCHECK(!GetBlocks()[1]->GetLastInstruction()->IsThrow());
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00002297 // Simple case of an entry block, a body block, and an exit block.
2298 // Put the body block's instruction into `invoke`'s block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002299 HBasicBlock* body = GetBlocks()[1];
2300 DCHECK(GetBlocks()[0]->IsEntryBlock());
2301 DCHECK(GetBlocks()[2]->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002302 DCHECK(!body->IsExitBlock());
Nicolas Geoffray788f2f02016-01-22 12:41:38 +00002303 DCHECK(!body->IsInLoop());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002304 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002305
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00002306 // Note that we add instructions before the invoke only to simplify polymorphic inlining.
2307 invoke->GetBlock()->instructions_.AddBefore(invoke, body->GetInstructions());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002308 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002309
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002310 // Replace the invoke with the return value of the inlined graph.
2311 if (last->IsReturn()) {
Calin Juravle2e768302015-07-28 14:41:11 +00002312 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002313 } else {
2314 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002315 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002316
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002317 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002318 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002319 // Need to inline multiple blocks. We split `invoke`'s block
2320 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00002321 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002322 // with the second half.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002323 ArenaAllocator* allocator = outer_graph->GetAllocator();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002324 HBasicBlock* at = invoke->GetBlock();
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00002325 // Note that we split before the invoke only to simplify polymorphic inlining.
2326 HBasicBlock* to = at->SplitBeforeForInlining(invoke);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002327
Vladimir Markoec7802a2015-10-01 20:57:57 +01002328 HBasicBlock* first = entry_block_->GetSuccessors()[0];
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002329 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01002330 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002331 exit_block_->ReplaceWith(to);
2332
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002333 // Update the meta information surrounding blocks:
2334 // (1) the graph they are now in,
2335 // (2) the reverse post order of that graph,
Nicolas Geoffray788f2f02016-01-22 12:41:38 +00002336 // (3) their potential loop information, inner and outer,
David Brazdil95177982015-10-30 12:56:58 -05002337 // (4) try block membership.
David Brazdil59a850e2015-11-10 13:04:30 +00002338 // Note that we do not need to update catch phi inputs because they
2339 // correspond to the register file of the outer method which the inlinee
2340 // cannot modify.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002341
2342 // We don't add the entry block, the exit block, and the first block, which
2343 // has been merged with `at`.
2344 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
2345
2346 // We add the `to` block.
2347 static constexpr int kNumberOfNewBlocksInCaller = 1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002348 size_t blocks_added = (reverse_post_order_.size() - kNumberOfSkippedBlocksInCallee)
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002349 + kNumberOfNewBlocksInCaller;
2350
2351 // Find the location of `at` in the outer graph's reverse post order. The new
2352 // blocks will be added after it.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002353 size_t index_of_at = IndexOfElement(outer_graph->reverse_post_order_, at);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002354 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
2355
David Brazdil95177982015-10-30 12:56:58 -05002356 // Do a reverse post order of the blocks in the callee and do (1), (2), (3)
2357 // and (4) to the blocks that apply.
Vladimir Marko2c45bc92016-10-25 16:54:12 +01002358 for (HBasicBlock* current : GetReversePostOrder()) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002359 if (current != exit_block_ && current != entry_block_ && current != first) {
David Brazdil95177982015-10-30 12:56:58 -05002360 DCHECK(current->GetTryCatchInformation() == nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002361 DCHECK(current->GetGraph() == this);
2362 current->SetGraph(outer_graph);
2363 outer_graph->AddBlock(current);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002364 outer_graph->reverse_post_order_[++index_of_at] = current;
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +00002365 UpdateLoopAndTryInformationOfNewBlock(current, at, /* replace_if_back_edge */ false);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002366 }
2367 }
2368
David Brazdil95177982015-10-30 12:56:58 -05002369 // Do (1), (2), (3) and (4) to `to`.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002370 to->SetGraph(outer_graph);
2371 outer_graph->AddBlock(to);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002372 outer_graph->reverse_post_order_[++index_of_at] = to;
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +00002373 // Only `to` can become a back edge, as the inlined blocks
2374 // are predecessors of `to`.
2375 UpdateLoopAndTryInformationOfNewBlock(to, at, /* replace_if_back_edge */ true);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00002376
David Brazdil3f523062016-02-29 16:53:33 +00002377 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002378 // to not `HReturn` but `HGoto` instead. Special case throwing blocks
2379 // to now get the outer graph exit block as successor. Note that the inliner
2380 // currently doesn't support inlining methods with try/catch.
2381 HPhi* return_value_phi = nullptr;
2382 bool rerun_dominance = false;
2383 bool rerun_loop_analysis = false;
2384 for (size_t pred = 0; pred < to->GetPredecessors().size(); ++pred) {
2385 HBasicBlock* predecessor = to->GetPredecessors()[pred];
David Brazdil3f523062016-02-29 16:53:33 +00002386 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002387 if (last->IsThrow()) {
2388 DCHECK(!at->IsTryBlock());
2389 predecessor->ReplaceSuccessor(to, outer_graph->GetExitBlock());
2390 --pred;
2391 // We need to re-run dominance information, as the exit block now has
2392 // a new dominator.
2393 rerun_dominance = true;
2394 if (predecessor->GetLoopInformation() != nullptr) {
2395 // The exit block and blocks post dominated by the exit block do not belong
2396 // to any loop. Because we do not compute the post dominators, we need to re-run
2397 // loop analysis to get the loop information correct.
2398 rerun_loop_analysis = true;
2399 }
2400 } else {
2401 if (last->IsReturnVoid()) {
2402 DCHECK(return_value == nullptr);
2403 DCHECK(return_value_phi == nullptr);
2404 } else {
David Brazdil3f523062016-02-29 16:53:33 +00002405 DCHECK(last->IsReturn());
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002406 if (return_value_phi != nullptr) {
2407 return_value_phi->AddInput(last->InputAt(0));
2408 } else if (return_value == nullptr) {
2409 return_value = last->InputAt(0);
2410 } else {
2411 // There will be multiple returns.
2412 return_value_phi = new (allocator) HPhi(
2413 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()), to->GetDexPc());
2414 to->AddPhi(return_value_phi);
2415 return_value_phi->AddInput(return_value);
2416 return_value_phi->AddInput(last->InputAt(0));
2417 return_value = return_value_phi;
2418 }
David Brazdil3f523062016-02-29 16:53:33 +00002419 }
2420 predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc()));
2421 predecessor->RemoveInstruction(last);
2422 }
2423 }
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002424 if (rerun_loop_analysis) {
Nicolas Geoffray1eede6a2017-03-02 16:14:53 +00002425 DCHECK(!outer_graph->HasIrreducibleLoops())
2426 << "Recomputing loop information in graphs with irreducible loops "
2427 << "is unsupported, as it could lead to loop header changes";
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +00002428 outer_graph->ClearLoopInformation();
2429 outer_graph->ClearDominanceInformation();
2430 outer_graph->BuildDominatorTree();
2431 } else if (rerun_dominance) {
2432 outer_graph->ClearDominanceInformation();
2433 outer_graph->ComputeDominanceInformation();
2434 }
David Brazdil3f523062016-02-29 16:53:33 +00002435 }
David Brazdil05144f42015-04-16 15:18:00 +01002436
2437 // Walk over the entry block and:
2438 // - Move constants from the entry block to the outer_graph's entry block,
2439 // - Replace HParameterValue instructions with their real value.
2440 // - Remove suspend checks, that hold an environment.
2441 // We must do this after the other blocks have been inlined, otherwise ids of
2442 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01002443 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01002444 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
2445 HInstruction* current = it.Current();
Calin Juravle214bbcd2015-10-20 14:54:07 +01002446 HInstruction* replacement = nullptr;
David Brazdil05144f42015-04-16 15:18:00 +01002447 if (current->IsNullConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002448 replacement = outer_graph->GetNullConstant(current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01002449 } else if (current->IsIntConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002450 replacement = outer_graph->GetIntConstant(
2451 current->AsIntConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01002452 } else if (current->IsLongConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002453 replacement = outer_graph->GetLongConstant(
2454 current->AsLongConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002455 } else if (current->IsFloatConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002456 replacement = outer_graph->GetFloatConstant(
2457 current->AsFloatConstant()->GetValue(), current->GetDexPc());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002458 } else if (current->IsDoubleConstant()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002459 replacement = outer_graph->GetDoubleConstant(
2460 current->AsDoubleConstant()->GetValue(), current->GetDexPc());
David Brazdil05144f42015-04-16 15:18:00 +01002461 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002462 if (kIsDebugBuild
2463 && invoke->IsInvokeStaticOrDirect()
2464 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
2465 // Ensure we do not use the last input of `invoke`, as it
2466 // contains a clinit check which is not an actual argument.
2467 size_t last_input_index = invoke->InputCount() - 1;
2468 DCHECK(parameter_index != last_input_index);
2469 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01002470 replacement = invoke->InputAt(parameter_index++);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002471 } else if (current->IsCurrentMethod()) {
Calin Juravle214bbcd2015-10-20 14:54:07 +01002472 replacement = outer_graph->GetCurrentMethod();
David Brazdil05144f42015-04-16 15:18:00 +01002473 } else {
2474 DCHECK(current->IsGoto() || current->IsSuspendCheck());
2475 entry_block_->RemoveInstruction(current);
2476 }
Calin Juravle214bbcd2015-10-20 14:54:07 +01002477 if (replacement != nullptr) {
2478 current->ReplaceWith(replacement);
2479 // If the current is the return value then we need to update the latter.
2480 if (current == return_value) {
2481 DCHECK_EQ(entry_block_, return_value->GetBlock());
2482 return_value = replacement;
2483 }
2484 }
2485 }
2486
Calin Juravle2e768302015-07-28 14:41:11 +00002487 return return_value;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002488}
2489
Mingyao Yang3584bce2015-05-19 16:01:59 -07002490/*
2491 * Loop will be transformed to:
2492 * old_pre_header
2493 * |
2494 * if_block
2495 * / \
Aart Bik3fc7f352015-11-20 22:03:03 -08002496 * true_block false_block
Mingyao Yang3584bce2015-05-19 16:01:59 -07002497 * \ /
2498 * new_pre_header
2499 * |
2500 * header
2501 */
2502void HGraph::TransformLoopHeaderForBCE(HBasicBlock* header) {
2503 DCHECK(header->IsLoopHeader());
Aart Bik3fc7f352015-11-20 22:03:03 -08002504 HBasicBlock* old_pre_header = header->GetDominator();
Mingyao Yang3584bce2015-05-19 16:01:59 -07002505
Aart Bik3fc7f352015-11-20 22:03:03 -08002506 // Need extra block to avoid critical edge.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002507 HBasicBlock* if_block = new (allocator_) HBasicBlock(this, header->GetDexPc());
2508 HBasicBlock* true_block = new (allocator_) HBasicBlock(this, header->GetDexPc());
2509 HBasicBlock* false_block = new (allocator_) HBasicBlock(this, header->GetDexPc());
2510 HBasicBlock* new_pre_header = new (allocator_) HBasicBlock(this, header->GetDexPc());
Mingyao Yang3584bce2015-05-19 16:01:59 -07002511 AddBlock(if_block);
Aart Bik3fc7f352015-11-20 22:03:03 -08002512 AddBlock(true_block);
2513 AddBlock(false_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002514 AddBlock(new_pre_header);
2515
Aart Bik3fc7f352015-11-20 22:03:03 -08002516 header->ReplacePredecessor(old_pre_header, new_pre_header);
2517 old_pre_header->successors_.clear();
2518 old_pre_header->dominated_blocks_.clear();
Mingyao Yang3584bce2015-05-19 16:01:59 -07002519
Aart Bik3fc7f352015-11-20 22:03:03 -08002520 old_pre_header->AddSuccessor(if_block);
2521 if_block->AddSuccessor(true_block); // True successor
2522 if_block->AddSuccessor(false_block); // False successor
2523 true_block->AddSuccessor(new_pre_header);
2524 false_block->AddSuccessor(new_pre_header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002525
Aart Bik3fc7f352015-11-20 22:03:03 -08002526 old_pre_header->dominated_blocks_.push_back(if_block);
2527 if_block->SetDominator(old_pre_header);
2528 if_block->dominated_blocks_.push_back(true_block);
2529 true_block->SetDominator(if_block);
2530 if_block->dominated_blocks_.push_back(false_block);
2531 false_block->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00002532 if_block->dominated_blocks_.push_back(new_pre_header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002533 new_pre_header->SetDominator(if_block);
Vladimir Marko60584552015-09-03 13:35:12 +00002534 new_pre_header->dominated_blocks_.push_back(header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002535 header->SetDominator(new_pre_header);
2536
Aart Bik3fc7f352015-11-20 22:03:03 -08002537 // Fix reverse post order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002538 size_t index_of_header = IndexOfElement(reverse_post_order_, header);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002539 MakeRoomFor(&reverse_post_order_, 4, index_of_header - 1);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002540 reverse_post_order_[index_of_header++] = if_block;
Aart Bik3fc7f352015-11-20 22:03:03 -08002541 reverse_post_order_[index_of_header++] = true_block;
2542 reverse_post_order_[index_of_header++] = false_block;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002543 reverse_post_order_[index_of_header++] = new_pre_header;
Mingyao Yang3584bce2015-05-19 16:01:59 -07002544
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +00002545 // The pre_header can never be a back edge of a loop.
2546 DCHECK((old_pre_header->GetLoopInformation() == nullptr) ||
2547 !old_pre_header->GetLoopInformation()->IsBackEdge(*old_pre_header));
2548 UpdateLoopAndTryInformationOfNewBlock(
2549 if_block, old_pre_header, /* replace_if_back_edge */ false);
2550 UpdateLoopAndTryInformationOfNewBlock(
2551 true_block, old_pre_header, /* replace_if_back_edge */ false);
2552 UpdateLoopAndTryInformationOfNewBlock(
2553 false_block, old_pre_header, /* replace_if_back_edge */ false);
2554 UpdateLoopAndTryInformationOfNewBlock(
2555 new_pre_header, old_pre_header, /* replace_if_back_edge */ false);
Mingyao Yang3584bce2015-05-19 16:01:59 -07002556}
2557
Aart Bikf8f5a162017-02-06 15:35:29 -08002558HBasicBlock* HGraph::TransformLoopForVectorization(HBasicBlock* header,
2559 HBasicBlock* body,
2560 HBasicBlock* exit) {
2561 DCHECK(header->IsLoopHeader());
2562 HLoopInformation* loop = header->GetLoopInformation();
2563
2564 // Add new loop blocks.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002565 HBasicBlock* new_pre_header = new (allocator_) HBasicBlock(this, header->GetDexPc());
2566 HBasicBlock* new_header = new (allocator_) HBasicBlock(this, header->GetDexPc());
2567 HBasicBlock* new_body = new (allocator_) HBasicBlock(this, header->GetDexPc());
Aart Bikf8f5a162017-02-06 15:35:29 -08002568 AddBlock(new_pre_header);
2569 AddBlock(new_header);
2570 AddBlock(new_body);
2571
2572 // Set up control flow.
2573 header->ReplaceSuccessor(exit, new_pre_header);
2574 new_pre_header->AddSuccessor(new_header);
2575 new_header->AddSuccessor(exit);
2576 new_header->AddSuccessor(new_body);
2577 new_body->AddSuccessor(new_header);
2578
2579 // Set up dominators.
2580 header->ReplaceDominatedBlock(exit, new_pre_header);
2581 new_pre_header->SetDominator(header);
2582 new_pre_header->dominated_blocks_.push_back(new_header);
2583 new_header->SetDominator(new_pre_header);
2584 new_header->dominated_blocks_.push_back(new_body);
2585 new_body->SetDominator(new_header);
2586 new_header->dominated_blocks_.push_back(exit);
2587 exit->SetDominator(new_header);
2588
2589 // Fix reverse post order.
2590 size_t index_of_header = IndexOfElement(reverse_post_order_, header);
2591 MakeRoomFor(&reverse_post_order_, 2, index_of_header);
2592 reverse_post_order_[++index_of_header] = new_pre_header;
2593 reverse_post_order_[++index_of_header] = new_header;
2594 size_t index_of_body = IndexOfElement(reverse_post_order_, body);
2595 MakeRoomFor(&reverse_post_order_, 1, index_of_body - 1);
2596 reverse_post_order_[index_of_body] = new_body;
2597
Aart Bikb07d1bc2017-04-05 10:03:15 -07002598 // Add gotos and suspend check (client must add conditional in header).
Vladimir Markoca6fff82017-10-03 14:49:14 +01002599 new_pre_header->AddInstruction(new (allocator_) HGoto());
2600 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(header->GetDexPc());
Aart Bikf8f5a162017-02-06 15:35:29 -08002601 new_header->AddInstruction(suspend_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002602 new_body->AddInstruction(new (allocator_) HGoto());
Aart Bikb07d1bc2017-04-05 10:03:15 -07002603 suspend_check->CopyEnvironmentFromWithLoopPhiAdjustment(
2604 loop->GetSuspendCheck()->GetEnvironment(), header);
Aart Bikf8f5a162017-02-06 15:35:29 -08002605
2606 // Update loop information.
2607 new_header->AddBackEdge(new_body);
2608 new_header->GetLoopInformation()->SetSuspendCheck(suspend_check);
2609 new_header->GetLoopInformation()->Populate();
2610 new_pre_header->SetLoopInformation(loop->GetPreHeader()->GetLoopInformation()); // outward
2611 HLoopInformationOutwardIterator it(*new_header);
2612 for (it.Advance(); !it.Done(); it.Advance()) {
2613 it.Current()->Add(new_pre_header);
2614 it.Current()->Add(new_header);
2615 it.Current()->Add(new_body);
2616 }
2617 return new_pre_header;
2618}
2619
David Brazdilf5552582015-12-27 13:36:12 +00002620static void CheckAgainstUpperBound(ReferenceTypeInfo rti, ReferenceTypeInfo upper_bound_rti)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002621 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdilf5552582015-12-27 13:36:12 +00002622 if (rti.IsValid()) {
2623 DCHECK(upper_bound_rti.IsSupertypeOf(rti))
2624 << " upper_bound_rti: " << upper_bound_rti
2625 << " rti: " << rti;
Nicolas Geoffray18401b72016-03-11 13:35:51 +00002626 DCHECK(!upper_bound_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes() || rti.IsExact())
2627 << " upper_bound_rti: " << upper_bound_rti
2628 << " rti: " << rti;
David Brazdilf5552582015-12-27 13:36:12 +00002629 }
2630}
2631
Calin Juravle2e768302015-07-28 14:41:11 +00002632void HInstruction::SetReferenceTypeInfo(ReferenceTypeInfo rti) {
2633 if (kIsDebugBuild) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002634 DCHECK_EQ(GetType(), DataType::Type::kReference);
Calin Juravle2e768302015-07-28 14:41:11 +00002635 ScopedObjectAccess soa(Thread::Current());
2636 DCHECK(rti.IsValid()) << "Invalid RTI for " << DebugName();
2637 if (IsBoundType()) {
2638 // Having the test here spares us from making the method virtual just for
2639 // the sake of a DCHECK.
David Brazdilf5552582015-12-27 13:36:12 +00002640 CheckAgainstUpperBound(rti, AsBoundType()->GetUpperBound());
Calin Juravle2e768302015-07-28 14:41:11 +00002641 }
2642 }
Vladimir Markoa1de9182016-02-25 11:37:38 +00002643 reference_type_handle_ = rti.GetTypeHandle();
2644 SetPackedFlag<kFlagReferenceTypeIsExact>(rti.IsExact());
Calin Juravle2e768302015-07-28 14:41:11 +00002645}
2646
David Brazdilf5552582015-12-27 13:36:12 +00002647void HBoundType::SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null) {
2648 if (kIsDebugBuild) {
2649 ScopedObjectAccess soa(Thread::Current());
2650 DCHECK(upper_bound.IsValid());
2651 DCHECK(!upper_bound_.IsValid()) << "Upper bound should only be set once.";
2652 CheckAgainstUpperBound(GetReferenceTypeInfo(), upper_bound);
2653 }
2654 upper_bound_ = upper_bound;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002655 SetPackedFlag<kFlagUpperCanBeNull>(can_be_null);
David Brazdilf5552582015-12-27 13:36:12 +00002656}
2657
Vladimir Markoa1de9182016-02-25 11:37:38 +00002658ReferenceTypeInfo ReferenceTypeInfo::Create(TypeHandle type_handle, bool is_exact) {
Calin Juravle2e768302015-07-28 14:41:11 +00002659 if (kIsDebugBuild) {
2660 ScopedObjectAccess soa(Thread::Current());
2661 DCHECK(IsValidHandle(type_handle));
Nicolas Geoffray18401b72016-03-11 13:35:51 +00002662 if (!is_exact) {
2663 DCHECK(!type_handle->CannotBeAssignedFromOtherTypes())
2664 << "Callers of ReferenceTypeInfo::Create should ensure is_exact is properly computed";
2665 }
Calin Juravle2e768302015-07-28 14:41:11 +00002666 }
Vladimir Markoa1de9182016-02-25 11:37:38 +00002667 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravle2e768302015-07-28 14:41:11 +00002668}
2669
Calin Juravleacf735c2015-02-12 15:25:22 +00002670std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
2671 ScopedObjectAccess soa(Thread::Current());
2672 os << "["
Calin Juravle2e768302015-07-28 14:41:11 +00002673 << " is_valid=" << rhs.IsValid()
David Sehr709b0702016-10-13 09:12:37 -07002674 << " type=" << (!rhs.IsValid() ? "?" : mirror::Class::PrettyClass(rhs.GetTypeHandle().Get()))
Calin Juravleacf735c2015-02-12 15:25:22 +00002675 << " is_exact=" << rhs.IsExact()
2676 << " ]";
2677 return os;
2678}
2679
Mark Mendellc4701932015-04-10 13:18:51 -04002680bool HInstruction::HasAnyEnvironmentUseBefore(HInstruction* other) {
2681 // For now, assume that instructions in different blocks may use the
2682 // environment.
2683 // TODO: Use the control flow to decide if this is true.
2684 if (GetBlock() != other->GetBlock()) {
2685 return true;
2686 }
2687
2688 // We know that we are in the same block. Walk from 'this' to 'other',
2689 // checking to see if there is any instruction with an environment.
2690 HInstruction* current = this;
2691 for (; current != other && current != nullptr; current = current->GetNext()) {
2692 // This is a conservative check, as the instruction result may not be in
2693 // the referenced environment.
2694 if (current->HasEnvironment()) {
2695 return true;
2696 }
2697 }
2698
2699 // We should have been called with 'this' before 'other' in the block.
2700 // Just confirm this.
2701 DCHECK(current != nullptr);
2702 return false;
2703}
2704
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002705void HInvoke::SetIntrinsic(Intrinsics intrinsic,
Aart Bik5d75afe2015-12-14 11:57:01 -08002706 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
2707 IntrinsicSideEffects side_effects,
2708 IntrinsicExceptions exceptions) {
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002709 intrinsic_ = intrinsic;
2710 IntrinsicOptimizations opt(this);
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002711
Aart Bik5d75afe2015-12-14 11:57:01 -08002712 // Adjust method's side effects from intrinsic table.
2713 switch (side_effects) {
2714 case kNoSideEffects: SetSideEffects(SideEffects::None()); break;
2715 case kReadSideEffects: SetSideEffects(SideEffects::AllReads()); break;
2716 case kWriteSideEffects: SetSideEffects(SideEffects::AllWrites()); break;
2717 case kAllSideEffects: SetSideEffects(SideEffects::AllExceptGCDependency()); break;
2718 }
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002719
2720 if (needs_env_or_cache == kNoEnvironmentOrCache) {
2721 opt.SetDoesNotNeedDexCache();
2722 opt.SetDoesNotNeedEnvironment();
2723 } else {
2724 // If we need an environment, that means there will be a call, which can trigger GC.
2725 SetSideEffects(GetSideEffects().Union(SideEffects::CanTriggerGC()));
2726 }
Aart Bik5d75afe2015-12-14 11:57:01 -08002727 // Adjust method's exception status from intrinsic table.
Aart Bik09e8d5f2016-01-22 16:49:55 -08002728 SetCanThrow(exceptions == kCanThrow);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002729}
2730
David Brazdil6de19382016-01-08 17:37:10 +00002731bool HNewInstance::IsStringAlloc() const {
2732 ScopedObjectAccess soa(Thread::Current());
2733 return GetReferenceTypeInfo().IsStringClass();
2734}
2735
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002736bool HInvoke::NeedsEnvironment() const {
2737 if (!IsIntrinsic()) {
2738 return true;
2739 }
2740 IntrinsicOptimizations opt(*this);
2741 return !opt.GetDoesNotNeedEnvironment();
2742}
2743
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002744const DexFile& HInvokeStaticOrDirect::GetDexFileForPcRelativeDexCache() const {
2745 ArtMethod* caller = GetEnvironment()->GetMethod();
2746 ScopedObjectAccess soa(Thread::Current());
2747 // `caller` is null for a top-level graph representing a method whose declaring
2748 // class was not resolved.
2749 return caller == nullptr ? GetBlock()->GetGraph()->GetDexFile() : *caller->GetDexFile();
2750}
2751
Vladimir Markodc151b22015-10-15 18:02:30 +01002752bool HInvokeStaticOrDirect::NeedsDexCacheOfDeclaringClass() const {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01002753 if (GetMethodLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002754 return false;
2755 }
2756 if (!IsIntrinsic()) {
2757 return true;
2758 }
2759 IntrinsicOptimizations opt(*this);
2760 return !opt.GetDoesNotNeedDexCache();
2761}
2762
Vladimir Markof64242a2015-12-01 14:58:23 +00002763std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs) {
2764 switch (rhs) {
2765 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
Vladimir Marko65979462017-05-19 17:25:12 +01002766 return os << "StringInit";
Vladimir Markof64242a2015-12-01 14:58:23 +00002767 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Marko65979462017-05-19 17:25:12 +01002768 return os << "Recursive";
2769 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
2770 return os << "BootImageLinkTimePcRelative";
Vladimir Markof64242a2015-12-01 14:58:23 +00002771 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Vladimir Marko19d7d502017-05-24 13:04:14 +01002772 return os << "DirectAddress";
Vladimir Marko0eb882b2017-05-15 13:39:18 +01002773 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry:
2774 return os << "BssEntry";
Vladimir Markoe7197bf2017-06-02 17:00:23 +01002775 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall:
2776 return os << "RuntimeCall";
Vladimir Markof64242a2015-12-01 14:58:23 +00002777 default:
2778 LOG(FATAL) << "Unknown MethodLoadKind: " << static_cast<int>(rhs);
2779 UNREACHABLE();
2780 }
2781}
2782
Vladimir Markofbb184a2015-11-13 14:47:00 +00002783std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs) {
2784 switch (rhs) {
2785 case HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit:
2786 return os << "explicit";
2787 case HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit:
2788 return os << "implicit";
2789 case HInvokeStaticOrDirect::ClinitCheckRequirement::kNone:
2790 return os << "none";
2791 default:
Vladimir Markof64242a2015-12-01 14:58:23 +00002792 LOG(FATAL) << "Unknown ClinitCheckRequirement: " << static_cast<int>(rhs);
2793 UNREACHABLE();
Vladimir Markofbb184a2015-11-13 14:47:00 +00002794 }
2795}
2796
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002797bool HLoadClass::InstructionDataEquals(const HInstruction* other) const {
2798 const HLoadClass* other_load_class = other->AsLoadClass();
2799 // TODO: To allow GVN for HLoadClass from different dex files, we should compare the type
2800 // names rather than type indexes. However, we shall also have to re-think the hash code.
2801 if (type_index_ != other_load_class->type_index_ ||
2802 GetPackedFields() != other_load_class->GetPackedFields()) {
2803 return false;
2804 }
Nicolas Geoffray9b1583e2016-12-13 13:43:31 +00002805 switch (GetLoadKind()) {
2806 case LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01002807 case LoadKind::kBootImageClassTable:
Nicolas Geoffray1ea9efc2017-01-16 22:57:39 +00002808 case LoadKind::kJitTableAddress: {
2809 ScopedObjectAccess soa(Thread::Current());
2810 return GetClass().Get() == other_load_class->GetClass().Get();
2811 }
Nicolas Geoffray9b1583e2016-12-13 13:43:31 +00002812 default:
Vladimir Marko48886c22017-01-06 11:45:47 +00002813 DCHECK(HasTypeReference(GetLoadKind()));
Nicolas Geoffray9b1583e2016-12-13 13:43:31 +00002814 return IsSameDexFile(GetDexFile(), other_load_class->GetDexFile());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002815 }
2816}
2817
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002818void HLoadClass::SetLoadKind(LoadKind load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002819 SetPackedField<LoadKindField>(load_kind);
2820
Vladimir Marko847e6ce2017-06-02 13:55:07 +01002821 if (load_kind != LoadKind::kRuntimeCall &&
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002822 load_kind != LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002823 RemoveAsUserOfInput(0u);
2824 SetRawInputAt(0u, nullptr);
2825 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002826
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002827 if (!NeedsEnvironment()) {
2828 RemoveEnvironment();
2829 SetSideEffects(SideEffects::None());
2830 }
2831}
2832
2833std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs) {
2834 switch (rhs) {
2835 case HLoadClass::LoadKind::kReferrersClass:
2836 return os << "ReferrersClass";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002837 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
2838 return os << "BootImageLinkTimePcRelative";
2839 case HLoadClass::LoadKind::kBootImageAddress:
2840 return os << "BootImageAddress";
Vladimir Marko94ec2db2017-09-06 17:21:03 +01002841 case HLoadClass::LoadKind::kBootImageClassTable:
2842 return os << "BootImageClassTable";
Vladimir Marko6bec91c2017-01-09 15:03:12 +00002843 case HLoadClass::LoadKind::kBssEntry:
2844 return os << "BssEntry";
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00002845 case HLoadClass::LoadKind::kJitTableAddress:
2846 return os << "JitTableAddress";
Vladimir Marko847e6ce2017-06-02 13:55:07 +01002847 case HLoadClass::LoadKind::kRuntimeCall:
2848 return os << "RuntimeCall";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002849 default:
2850 LOG(FATAL) << "Unknown HLoadClass::LoadKind: " << static_cast<int>(rhs);
2851 UNREACHABLE();
2852 }
2853}
2854
Vladimir Marko372f10e2016-05-17 16:30:10 +01002855bool HLoadString::InstructionDataEquals(const HInstruction* other) const {
2856 const HLoadString* other_load_string = other->AsLoadString();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002857 // TODO: To allow GVN for HLoadString from different dex files, we should compare the strings
2858 // rather than their indexes. However, we shall also have to re-think the hash code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002859 if (string_index_ != other_load_string->string_index_ ||
2860 GetPackedFields() != other_load_string->GetPackedFields()) {
2861 return false;
2862 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00002863 switch (GetLoadKind()) {
2864 case LoadKind::kBootImageAddress:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002865 case LoadKind::kBootImageInternTable:
Nicolas Geoffray1ea9efc2017-01-16 22:57:39 +00002866 case LoadKind::kJitTableAddress: {
2867 ScopedObjectAccess soa(Thread::Current());
2868 return GetString().Get() == other_load_string->GetString().Get();
2869 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00002870 default:
2871 return IsSameDexFile(GetDexFile(), other_load_string->GetDexFile());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002872 }
2873}
2874
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00002875void HLoadString::SetLoadKind(LoadKind load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002876 // Once sharpened, the load kind should not be changed again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01002877 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002878 SetPackedField<LoadKindField>(load_kind);
2879
Vladimir Marko847e6ce2017-06-02 13:55:07 +01002880 if (load_kind != LoadKind::kRuntimeCall) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002881 RemoveAsUserOfInput(0u);
2882 SetRawInputAt(0u, nullptr);
2883 }
2884 if (!NeedsEnvironment()) {
2885 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01002886 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002887 }
2888}
2889
2890std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs) {
2891 switch (rhs) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002892 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
2893 return os << "BootImageLinkTimePcRelative";
2894 case HLoadString::LoadKind::kBootImageAddress:
2895 return os << "BootImageAddress";
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002896 case HLoadString::LoadKind::kBootImageInternTable:
2897 return os << "BootImageInternTable";
Vladimir Markoaad75c62016-10-03 08:46:48 +00002898 case HLoadString::LoadKind::kBssEntry:
2899 return os << "BssEntry";
Mingyao Yangbe44dcf2016-11-30 14:17:32 -08002900 case HLoadString::LoadKind::kJitTableAddress:
2901 return os << "JitTableAddress";
Vladimir Marko847e6ce2017-06-02 13:55:07 +01002902 case HLoadString::LoadKind::kRuntimeCall:
2903 return os << "RuntimeCall";
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002904 default:
2905 LOG(FATAL) << "Unknown HLoadString::LoadKind: " << static_cast<int>(rhs);
2906 UNREACHABLE();
2907 }
2908}
2909
Mark Mendellc4701932015-04-10 13:18:51 -04002910void HInstruction::RemoveEnvironmentUsers() {
Vladimir Marko46817b82016-03-29 12:21:58 +01002911 for (const HUseListNode<HEnvironment*>& use : GetEnvUses()) {
2912 HEnvironment* user = use.GetUser();
2913 user->SetRawEnvAt(use.GetIndex(), nullptr);
Mark Mendellc4701932015-04-10 13:18:51 -04002914 }
Vladimir Marko46817b82016-03-29 12:21:58 +01002915 env_uses_.clear();
Mark Mendellc4701932015-04-10 13:18:51 -04002916}
2917
Artem Serovcced8ba2017-07-19 18:18:09 +01002918HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr) {
2919 HInstruction* clone = instr->Clone(instr->GetBlock()->GetGraph()->GetAllocator());
2920 HBasicBlock* block = instr->GetBlock();
2921
2922 if (instr->IsPhi()) {
2923 HPhi* phi = instr->AsPhi();
2924 DCHECK(!phi->HasEnvironment());
2925 HPhi* phi_clone = clone->AsPhi();
2926 block->ReplaceAndRemovePhiWith(phi, phi_clone);
2927 } else {
2928 block->ReplaceAndRemoveInstructionWith(instr, clone);
2929 if (instr->HasEnvironment()) {
2930 clone->CopyEnvironmentFrom(instr->GetEnvironment());
2931 HLoopInformation* loop_info = block->GetLoopInformation();
2932 if (instr->IsSuspendCheck() && loop_info != nullptr) {
2933 loop_info->SetSuspendCheck(clone->AsSuspendCheck());
2934 }
2935 }
2936 }
2937 return clone;
2938}
2939
Roland Levillainc9b21f82016-03-23 16:36:59 +00002940// Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -05002941HInstruction* HGraph::InsertOppositeCondition(HInstruction* cond, HInstruction* cursor) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002942 ArenaAllocator* allocator = GetAllocator();
Mark Mendellf6529172015-11-17 11:16:56 -05002943
2944 if (cond->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002945 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType())) {
Mark Mendellf6529172015-11-17 11:16:56 -05002946 // Can't reverse floating point conditions. We have to use HBooleanNot in that case.
2947 HInstruction* lhs = cond->InputAt(0);
2948 HInstruction* rhs = cond->InputAt(1);
David Brazdil5c004852015-11-23 09:44:52 +00002949 HInstruction* replacement = nullptr;
Mark Mendellf6529172015-11-17 11:16:56 -05002950 switch (cond->AsCondition()->GetOppositeCondition()) { // get *opposite*
2951 case kCondEQ: replacement = new (allocator) HEqual(lhs, rhs); break;
2952 case kCondNE: replacement = new (allocator) HNotEqual(lhs, rhs); break;
2953 case kCondLT: replacement = new (allocator) HLessThan(lhs, rhs); break;
2954 case kCondLE: replacement = new (allocator) HLessThanOrEqual(lhs, rhs); break;
2955 case kCondGT: replacement = new (allocator) HGreaterThan(lhs, rhs); break;
2956 case kCondGE: replacement = new (allocator) HGreaterThanOrEqual(lhs, rhs); break;
2957 case kCondB: replacement = new (allocator) HBelow(lhs, rhs); break;
2958 case kCondBE: replacement = new (allocator) HBelowOrEqual(lhs, rhs); break;
2959 case kCondA: replacement = new (allocator) HAbove(lhs, rhs); break;
2960 case kCondAE: replacement = new (allocator) HAboveOrEqual(lhs, rhs); break;
David Brazdil5c004852015-11-23 09:44:52 +00002961 default:
2962 LOG(FATAL) << "Unexpected condition";
2963 UNREACHABLE();
Mark Mendellf6529172015-11-17 11:16:56 -05002964 }
2965 cursor->GetBlock()->InsertInstructionBefore(replacement, cursor);
2966 return replacement;
2967 } else if (cond->IsIntConstant()) {
2968 HIntConstant* int_const = cond->AsIntConstant();
Roland Levillain1a653882016-03-18 18:05:57 +00002969 if (int_const->IsFalse()) {
Mark Mendellf6529172015-11-17 11:16:56 -05002970 return GetIntConstant(1);
2971 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002972 DCHECK(int_const->IsTrue()) << int_const->GetValue();
Mark Mendellf6529172015-11-17 11:16:56 -05002973 return GetIntConstant(0);
2974 }
2975 } else {
2976 HInstruction* replacement = new (allocator) HBooleanNot(cond);
2977 cursor->GetBlock()->InsertInstructionBefore(replacement, cursor);
2978 return replacement;
2979 }
2980}
2981
Roland Levillainc9285912015-12-18 10:38:42 +00002982std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs) {
2983 os << "["
2984 << " source=" << rhs.GetSource()
2985 << " destination=" << rhs.GetDestination()
2986 << " type=" << rhs.GetType()
2987 << " instruction=";
2988 if (rhs.GetInstruction() != nullptr) {
2989 os << rhs.GetInstruction()->DebugName() << ' ' << rhs.GetInstruction()->GetId();
2990 } else {
2991 os << "null";
2992 }
2993 os << " ]";
2994 return os;
2995}
2996
Roland Levillain86503782016-02-11 19:07:30 +00002997std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs) {
2998 switch (rhs) {
2999 case TypeCheckKind::kUnresolvedCheck:
3000 return os << "unresolved_check";
3001 case TypeCheckKind::kExactCheck:
3002 return os << "exact_check";
3003 case TypeCheckKind::kClassHierarchyCheck:
3004 return os << "class_hierarchy_check";
3005 case TypeCheckKind::kAbstractClassCheck:
3006 return os << "abstract_class_check";
3007 case TypeCheckKind::kInterfaceCheck:
3008 return os << "interface_check";
3009 case TypeCheckKind::kArrayObjectCheck:
3010 return os << "array_object_check";
3011 case TypeCheckKind::kArrayCheck:
3012 return os << "array_check";
3013 default:
3014 LOG(FATAL) << "Unknown TypeCheckKind: " << static_cast<int>(rhs);
3015 UNREACHABLE();
3016 }
3017}
3018
Andreas Gampe26de38b2016-07-27 17:53:11 -07003019std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind) {
3020 switch (kind) {
3021 case MemBarrierKind::kAnyStore:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003022 return os << "AnyStore";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003023 case MemBarrierKind::kLoadAny:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003024 return os << "LoadAny";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003025 case MemBarrierKind::kStoreStore:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003026 return os << "StoreStore";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003027 case MemBarrierKind::kAnyAny:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003028 return os << "AnyAny";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003029 case MemBarrierKind::kNTStoreStore:
Andreas Gampe75d2df22016-07-27 21:25:41 -07003030 return os << "NTStoreStore";
Andreas Gampe26de38b2016-07-27 17:53:11 -07003031
3032 default:
3033 LOG(FATAL) << "Unknown MemBarrierKind: " << static_cast<int>(kind);
3034 UNREACHABLE();
3035 }
3036}
3037
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003038} // namespace art