blob: c17251221ed64fbded076367c73d301892698f3a [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "nodes.h"
Calin Juravle77520bc2015-01-12 18:45:46 +000018
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010019#include "ssa_builder.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000020#include "utils/growable_array.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000021#include "scoped_thread_state_change.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000022
23namespace art {
24
25void HGraph::AddBlock(HBasicBlock* block) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000026 block->SetBlockId(blocks_.Size());
Nicolas Geoffray818f2102014-02-18 16:43:35 +000027 blocks_.Add(block);
28}
29
Nicolas Geoffray804d0932014-05-02 08:46:00 +010030void HGraph::FindBackEdges(ArenaBitVector* visited) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000031 ArenaBitVector visiting(arena_, blocks_.Size(), false);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032 VisitBlockForBackEdges(entry_block_, visited, &visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000033}
34
Roland Levillainfc600dc2014-12-02 17:16:31 +000035static void RemoveAsUser(HInstruction* instruction) {
36 for (size_t i = 0; i < instruction->InputCount(); i++) {
David Brazdil1abb4192015-02-17 18:33:36 +000037 instruction->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000038 }
39
40 HEnvironment* environment = instruction->GetEnvironment();
41 if (environment != nullptr) {
42 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
David Brazdil1abb4192015-02-17 18:33:36 +000043 if (environment->GetInstructionAt(i) != nullptr) {
44 environment->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000045 }
46 }
47 }
48}
49
50void HGraph::RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const {
51 for (size_t i = 0; i < blocks_.Size(); ++i) {
52 if (!visited.IsBitSet(i)) {
53 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010054 DCHECK(block->GetPhis().IsEmpty()) << "Phis are not inserted at this stage";
Roland Levillainfc600dc2014-12-02 17:16:31 +000055 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
56 RemoveAsUser(it.Current());
57 }
58 }
59 }
60}
61
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010062void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010063 for (size_t i = 0; i < blocks_.Size(); ++i) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000064 if (!visited.IsBitSet(i)) {
David Brazdil1abb4192015-02-17 18:33:36 +000065 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010066 // We only need to update the successor, which might be live.
David Brazdil1abb4192015-02-17 18:33:36 +000067 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
68 block->GetSuccessors().Get(j)->RemovePredecessor(block);
69 }
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010070 // Remove the block from the list of blocks, so that further analyses
71 // never see it.
72 blocks_.Put(i, nullptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073 }
74 }
75}
76
77void HGraph::VisitBlockForBackEdges(HBasicBlock* block,
78 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +010079 ArenaBitVector* visiting) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000080 int id = block->GetBlockId();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081 if (visited->IsBitSet(id)) return;
82
83 visited->SetBit(id);
84 visiting->SetBit(id);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010085 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
86 HBasicBlock* successor = block->GetSuccessors().Get(i);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000087 if (visiting->IsBitSet(successor->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000088 successor->AddBackEdge(block);
89 } else {
90 VisitBlockForBackEdges(successor, visited, visiting);
91 }
92 }
93 visiting->ClearBit(id);
94}
95
96void HGraph::BuildDominatorTree() {
97 ArenaBitVector visited(arena_, blocks_.Size(), false);
98
99 // (1) Find the back edges in the graph doing a DFS traversal.
100 FindBackEdges(&visited);
101
Roland Levillainfc600dc2014-12-02 17:16:31 +0000102 // (2) Remove instructions and phis from blocks not visited during
103 // the initial DFS as users from other instructions, so that
104 // users can be safely removed before uses later.
105 RemoveInstructionsAsUsersFromDeadBlocks(visited);
106
107 // (3) Remove blocks not visited during the initial DFS.
108 // Step (4) requires dead blocks to be removed from the
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000109 // predecessors list of live blocks.
110 RemoveDeadBlocks(visited);
111
Roland Levillainfc600dc2014-12-02 17:16:31 +0000112 // (4) Simplify the CFG now, so that we don't need to recompute
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100113 // dominators and the reverse post order.
114 SimplifyCFG();
115
Roland Levillainfc600dc2014-12-02 17:16:31 +0000116 // (5) Compute the immediate dominator of each block. We visit
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000117 // the successors of a block only when all its forward branches
118 // have been processed.
119 GrowableArray<size_t> visits(arena_, blocks_.Size());
120 visits.SetSize(blocks_.Size());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100121 reverse_post_order_.Add(entry_block_);
122 for (size_t i = 0; i < entry_block_->GetSuccessors().Size(); i++) {
123 VisitBlockForDominatorTree(entry_block_->GetSuccessors().Get(i), entry_block_, &visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000124 }
125}
126
127HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
128 ArenaBitVector visited(arena_, blocks_.Size(), false);
129 // Walk the dominator tree of the first block and mark the visited blocks.
130 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000131 visited.SetBit(first->GetBlockId());
132 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000133 }
134 // Walk the dominator tree of the second block until a marked block is found.
135 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000136 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000137 return second;
138 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000139 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000140 }
141 LOG(ERROR) << "Could not find common dominator";
142 return nullptr;
143}
144
145void HGraph::VisitBlockForDominatorTree(HBasicBlock* block,
146 HBasicBlock* predecessor,
147 GrowableArray<size_t>* visits) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000148 if (block->GetDominator() == nullptr) {
149 block->SetDominator(predecessor);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000150 } else {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000151 block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000152 }
153
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000154 visits->Increment(block->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000155 // Once all the forward edges have been visited, we know the immediate
156 // dominator of the block. We can then start visiting its successors.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000157 if (visits->Get(block->GetBlockId()) ==
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100158 block->GetPredecessors().Size() - block->NumberOfBackEdges()) {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100159 block->GetDominator()->AddDominatedBlock(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100160 reverse_post_order_.Add(block);
161 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
162 VisitBlockForDominatorTree(block->GetSuccessors().Get(i), block, visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000163 }
164 }
165}
166
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000167void HGraph::TransformToSsa() {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100168 DCHECK(!reverse_post_order_.IsEmpty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100169 SsaBuilder ssa_builder(this);
170 ssa_builder.BuildSsa();
171}
172
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100173void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) {
174 // Insert a new node between `block` and `successor` to split the
175 // critical edge.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100176 HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100177 AddBlock(new_block);
178 new_block->AddInstruction(new (arena_) HGoto());
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100179 block->ReplaceSuccessor(successor, new_block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100180 new_block->AddSuccessor(successor);
181 if (successor->IsLoopHeader()) {
182 // If we split at a back edge boundary, make the new block the back edge.
183 HLoopInformation* info = successor->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000184 if (info->IsBackEdge(*block)) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100185 info->RemoveBackEdge(block);
186 info->AddBackEdge(new_block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100187 }
188 }
189}
190
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100191void HGraph::SimplifyLoop(HBasicBlock* header) {
192 HLoopInformation* info = header->GetLoopInformation();
193
194 // If there are more than one back edge, make them branch to the same block that
195 // will become the only back edge. This simplifies finding natural loops in the
196 // graph.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100197 // Also, if the loop is a do/while (that is the back edge is an if), change the
198 // back edge to be a goto. This simplifies code generation of suspend cheks.
199 if (info->NumberOfBackEdges() > 1 || info->GetBackEdges().Get(0)->GetLastInstruction()->IsIf()) {
200 HBasicBlock* new_back_edge = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100201 AddBlock(new_back_edge);
202 new_back_edge->AddInstruction(new (arena_) HGoto());
203 for (size_t pred = 0, e = info->GetBackEdges().Size(); pred < e; ++pred) {
204 HBasicBlock* back_edge = info->GetBackEdges().Get(pred);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100205 back_edge->ReplaceSuccessor(header, new_back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100206 }
207 info->ClearBackEdges();
208 info->AddBackEdge(new_back_edge);
209 new_back_edge->AddSuccessor(header);
210 }
211
212 // Make sure the loop has only one pre header. This simplifies SSA building by having
213 // to just look at the pre header to know which locals are initialized at entry of the
214 // loop.
215 size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges();
216 if (number_of_incomings != 1) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100217 HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100218 AddBlock(pre_header);
219 pre_header->AddInstruction(new (arena_) HGoto());
220
221 ArenaBitVector back_edges(arena_, GetBlocks().Size(), false);
222 HBasicBlock* back_edge = info->GetBackEdges().Get(0);
223 for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) {
224 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
225 if (predecessor != back_edge) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100226 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100227 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100228 }
229 }
230 pre_header->AddSuccessor(header);
231 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100232
233 // Make sure the second predecessor of a loop header is the back edge.
234 if (header->GetPredecessors().Get(1) != info->GetBackEdges().Get(0)) {
235 header->SwapPredecessors();
236 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100237
238 // Place the suspend check at the beginning of the header, so that live registers
239 // will be known when allocating registers. Note that code generation can still
240 // generate the suspend check at the back edge, but needs to be careful with
241 // loop phi spill slots (which are not written to at back edge).
242 HInstruction* first_instruction = header->GetFirstInstruction();
243 if (!first_instruction->IsSuspendCheck()) {
244 HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc());
245 header->InsertInstructionBefore(check, first_instruction);
246 first_instruction = check;
247 }
248 info->SetSuspendCheck(first_instruction->AsSuspendCheck());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100249}
250
251void HGraph::SimplifyCFG() {
252 // Simplify the CFG for future analysis, and code generation:
253 // (1): Split critical edges.
254 // (2): Simplify loops by having only one back edge, and one preheader.
255 for (size_t i = 0; i < blocks_.Size(); ++i) {
256 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100257 if (block == nullptr) continue;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100258 if (block->GetSuccessors().Size() > 1) {
259 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
260 HBasicBlock* successor = block->GetSuccessors().Get(j);
261 if (successor->GetPredecessors().Size() > 1) {
262 SplitCriticalEdge(block, successor);
263 --j;
264 }
265 }
266 }
267 if (block->IsLoopHeader()) {
268 SimplifyLoop(block);
269 }
270 }
271}
272
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000273bool HGraph::AnalyzeNaturalLoops() const {
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100274 // Order does not matter.
275 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
276 HBasicBlock* block = it.Current();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100277 if (block->IsLoopHeader()) {
278 HLoopInformation* info = block->GetLoopInformation();
279 if (!info->Populate()) {
280 // Abort if the loop is non natural. We currently bailout in such cases.
281 return false;
282 }
283 }
284 }
285 return true;
286}
287
David Brazdil8d5b8b22015-03-24 10:51:52 +0000288void HGraph::InsertConstant(HConstant* constant) {
289 // New constants are inserted before the final control-flow instruction
290 // of the graph, or at its end if called from the graph builder.
291 if (entry_block_->EndsWithControlFlowInstruction()) {
292 entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction());
David Brazdil46e2a392015-03-16 17:31:52 +0000293 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000294 entry_block_->AddInstruction(constant);
David Brazdil46e2a392015-03-16 17:31:52 +0000295 }
296}
297
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000298HNullConstant* HGraph::GetNullConstant() {
299 if (cached_null_constant_ == nullptr) {
300 cached_null_constant_ = new (arena_) HNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000301 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000302 }
303 return cached_null_constant_;
304}
305
David Brazdil8d5b8b22015-03-24 10:51:52 +0000306HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value) {
307 switch (type) {
308 case Primitive::Type::kPrimBoolean:
309 DCHECK(IsUint<1>(value));
310 FALLTHROUGH_INTENDED;
311 case Primitive::Type::kPrimByte:
312 case Primitive::Type::kPrimChar:
313 case Primitive::Type::kPrimShort:
314 case Primitive::Type::kPrimInt:
315 DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value));
316 return GetIntConstant(static_cast<int32_t>(value));
317
318 case Primitive::Type::kPrimLong:
319 return GetLongConstant(value);
320
321 default:
322 LOG(FATAL) << "Unsupported constant type";
323 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000324 }
David Brazdil46e2a392015-03-16 17:31:52 +0000325}
326
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000327void HGraph::CacheFloatConstant(HFloatConstant* constant) {
328 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
329 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
330 cached_float_constants_.Overwrite(value, constant);
331}
332
333void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
334 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
335 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
336 cached_double_constants_.Overwrite(value, constant);
337}
338
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000339void HLoopInformation::Add(HBasicBlock* block) {
340 blocks_.SetBit(block->GetBlockId());
341}
342
David Brazdil46e2a392015-03-16 17:31:52 +0000343void HLoopInformation::Remove(HBasicBlock* block) {
344 blocks_.ClearBit(block->GetBlockId());
345}
346
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100347void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
348 if (blocks_.IsBitSet(block->GetBlockId())) {
349 return;
350 }
351
352 blocks_.SetBit(block->GetBlockId());
353 block->SetInLoop(this);
354 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
355 PopulateRecursive(block->GetPredecessors().Get(i));
356 }
357}
358
359bool HLoopInformation::Populate() {
360 DCHECK_EQ(GetBackEdges().Size(), 1u);
361 HBasicBlock* back_edge = GetBackEdges().Get(0);
362 DCHECK(back_edge->GetDominator() != nullptr);
363 if (!header_->Dominates(back_edge)) {
364 // This loop is not natural. Do not bother going further.
365 return false;
366 }
367
368 // Populate this loop: starting with the back edge, recursively add predecessors
369 // that are not already part of that loop. Set the header as part of the loop
370 // to end the recursion.
371 // This is a recursive implementation of the algorithm described in
372 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
373 blocks_.SetBit(header_->GetBlockId());
374 PopulateRecursive(back_edge);
375 return true;
376}
377
378HBasicBlock* HLoopInformation::GetPreHeader() const {
379 DCHECK_EQ(header_->GetPredecessors().Size(), 2u);
380 return header_->GetDominator();
381}
382
383bool HLoopInformation::Contains(const HBasicBlock& block) const {
384 return blocks_.IsBitSet(block.GetBlockId());
385}
386
387bool HLoopInformation::IsIn(const HLoopInformation& other) const {
388 return other.blocks_.IsBitSet(header_->GetBlockId());
389}
390
391bool HBasicBlock::Dominates(HBasicBlock* other) const {
392 // Walk up the dominator tree from `other`, to find out if `this`
393 // is an ancestor.
394 HBasicBlock* current = other;
395 while (current != nullptr) {
396 if (current == this) {
397 return true;
398 }
399 current = current->GetDominator();
400 }
401 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100402}
403
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100404static void UpdateInputsUsers(HInstruction* instruction) {
405 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
406 instruction->InputAt(i)->AddUseAt(instruction, i);
407 }
408 // Environment should be created later.
409 DCHECK(!instruction->HasEnvironment());
410}
411
Roland Levillainccc07a92014-09-16 14:48:16 +0100412void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
413 HInstruction* replacement) {
414 DCHECK(initial->GetBlock() == this);
415 InsertInstructionBefore(replacement, initial);
416 initial->ReplaceWith(replacement);
417 RemoveInstruction(initial);
418}
419
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100420static void Add(HInstructionList* instruction_list,
421 HBasicBlock* block,
422 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000423 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000424 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100425 instruction->SetBlock(block);
426 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100427 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100428 instruction_list->AddInstruction(instruction);
429}
430
431void HBasicBlock::AddInstruction(HInstruction* instruction) {
432 Add(&instructions_, this, instruction);
433}
434
435void HBasicBlock::AddPhi(HPhi* phi) {
436 Add(&phis_, this, phi);
437}
438
David Brazdilc3d743f2015-04-22 13:40:50 +0100439void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
440 DCHECK(!cursor->IsPhi());
441 DCHECK(!instruction->IsPhi());
442 DCHECK_EQ(instruction->GetId(), -1);
443 DCHECK_NE(cursor->GetId(), -1);
444 DCHECK_EQ(cursor->GetBlock(), this);
445 DCHECK(!instruction->IsControlFlow());
446 instruction->SetBlock(this);
447 instruction->SetId(GetGraph()->GetNextInstructionId());
448 UpdateInputsUsers(instruction);
449 instructions_.InsertInstructionBefore(instruction, cursor);
450}
451
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100452void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
453 DCHECK_EQ(phi->GetId(), -1);
454 DCHECK_NE(cursor->GetId(), -1);
455 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100456 phi->SetBlock(this);
457 phi->SetId(GetGraph()->GetNextInstructionId());
458 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100459 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100460}
461
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100462static void Remove(HInstructionList* instruction_list,
463 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000464 HInstruction* instruction,
465 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100466 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100467 instruction->SetBlock(nullptr);
468 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000469 if (ensure_safety) {
470 DCHECK(instruction->GetUses().IsEmpty());
471 DCHECK(instruction->GetEnvUses().IsEmpty());
472 RemoveAsUser(instruction);
473 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100474}
475
David Brazdil1abb4192015-02-17 18:33:36 +0000476void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
477 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100478}
479
David Brazdil1abb4192015-02-17 18:33:36 +0000480void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
481 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100482}
483
David Brazdiled596192015-01-23 10:39:45 +0000484void HEnvironment::CopyFrom(HEnvironment* env) {
485 for (size_t i = 0; i < env->Size(); i++) {
486 HInstruction* instruction = env->GetInstructionAt(i);
487 SetRawEnvAt(i, instruction);
488 if (instruction != nullptr) {
489 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100490 }
David Brazdiled596192015-01-23 10:39:45 +0000491 }
492}
493
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700494void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
495 HBasicBlock* loop_header) {
496 DCHECK(loop_header->IsLoopHeader());
497 for (size_t i = 0; i < env->Size(); i++) {
498 HInstruction* instruction = env->GetInstructionAt(i);
499 SetRawEnvAt(i, instruction);
500 if (instruction == nullptr) {
501 continue;
502 }
503 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
504 // At the end of the loop pre-header, the corresponding value for instruction
505 // is the first input of the phi.
506 HInstruction* initial = instruction->AsPhi()->InputAt(0);
507 DCHECK(initial->GetBlock()->Dominates(loop_header));
508 SetRawEnvAt(i, initial);
509 initial->AddEnvUseAt(this, i);
510 } else {
511 instruction->AddEnvUseAt(this, i);
512 }
513 }
514}
515
David Brazdil1abb4192015-02-17 18:33:36 +0000516void HEnvironment::RemoveAsUserOfInput(size_t index) const {
517 const HUserRecord<HEnvironment*> user_record = vregs_.Get(index);
518 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100519}
520
Calin Juravle77520bc2015-01-12 18:45:46 +0000521HInstruction* HInstruction::GetNextDisregardingMoves() const {
522 HInstruction* next = GetNext();
523 while (next != nullptr && next->IsParallelMove()) {
524 next = next->GetNext();
525 }
526 return next;
527}
528
529HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
530 HInstruction* previous = GetPrevious();
531 while (previous != nullptr && previous->IsParallelMove()) {
532 previous = previous->GetPrevious();
533 }
534 return previous;
535}
536
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100537void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000538 if (first_instruction_ == nullptr) {
539 DCHECK(last_instruction_ == nullptr);
540 first_instruction_ = last_instruction_ = instruction;
541 } else {
542 last_instruction_->next_ = instruction;
543 instruction->previous_ = last_instruction_;
544 last_instruction_ = instruction;
545 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000546}
547
David Brazdilc3d743f2015-04-22 13:40:50 +0100548void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
549 DCHECK(Contains(cursor));
550 if (cursor == first_instruction_) {
551 cursor->previous_ = instruction;
552 instruction->next_ = cursor;
553 first_instruction_ = instruction;
554 } else {
555 instruction->previous_ = cursor->previous_;
556 instruction->next_ = cursor;
557 cursor->previous_ = instruction;
558 instruction->previous_->next_ = instruction;
559 }
560}
561
562void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
563 DCHECK(Contains(cursor));
564 if (cursor == last_instruction_) {
565 cursor->next_ = instruction;
566 instruction->previous_ = cursor;
567 last_instruction_ = instruction;
568 } else {
569 instruction->next_ = cursor->next_;
570 instruction->previous_ = cursor;
571 cursor->next_ = instruction;
572 instruction->next_->previous_ = instruction;
573 }
574}
575
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100576void HInstructionList::RemoveInstruction(HInstruction* instruction) {
577 if (instruction->previous_ != nullptr) {
578 instruction->previous_->next_ = instruction->next_;
579 }
580 if (instruction->next_ != nullptr) {
581 instruction->next_->previous_ = instruction->previous_;
582 }
583 if (instruction == first_instruction_) {
584 first_instruction_ = instruction->next_;
585 }
586 if (instruction == last_instruction_) {
587 last_instruction_ = instruction->previous_;
588 }
589}
590
Roland Levillain6b469232014-09-25 10:10:38 +0100591bool HInstructionList::Contains(HInstruction* instruction) const {
592 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
593 if (it.Current() == instruction) {
594 return true;
595 }
596 }
597 return false;
598}
599
Roland Levillainccc07a92014-09-16 14:48:16 +0100600bool HInstructionList::FoundBefore(const HInstruction* instruction1,
601 const HInstruction* instruction2) const {
602 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
603 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
604 if (it.Current() == instruction1) {
605 return true;
606 }
607 if (it.Current() == instruction2) {
608 return false;
609 }
610 }
611 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
612 return true;
613}
614
Roland Levillain6c82d402014-10-13 16:10:27 +0100615bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
616 if (other_instruction == this) {
617 // An instruction does not strictly dominate itself.
618 return false;
619 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100620 HBasicBlock* block = GetBlock();
621 HBasicBlock* other_block = other_instruction->GetBlock();
622 if (block != other_block) {
623 return GetBlock()->Dominates(other_instruction->GetBlock());
624 } else {
625 // If both instructions are in the same block, ensure this
626 // instruction comes before `other_instruction`.
627 if (IsPhi()) {
628 if (!other_instruction->IsPhi()) {
629 // Phis appear before non phi-instructions so this instruction
630 // dominates `other_instruction`.
631 return true;
632 } else {
633 // There is no order among phis.
634 LOG(FATAL) << "There is no dominance between phis of a same block.";
635 return false;
636 }
637 } else {
638 // `this` is not a phi.
639 if (other_instruction->IsPhi()) {
640 // Phis appear before non phi-instructions so this instruction
641 // does not dominate `other_instruction`.
642 return false;
643 } else {
644 // Check whether this instruction comes before
645 // `other_instruction` in the instruction list.
646 return block->GetInstructions().FoundBefore(this, other_instruction);
647 }
648 }
649 }
650}
651
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100652void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100653 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000654 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
655 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100656 HInstruction* user = current->GetUser();
657 size_t input_index = current->GetIndex();
658 user->SetRawInputAt(input_index, other);
659 other->AddUseAt(user, input_index);
660 }
661
David Brazdiled596192015-01-23 10:39:45 +0000662 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
663 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100664 HEnvironment* user = current->GetUser();
665 size_t input_index = current->GetIndex();
666 user->SetRawEnvAt(input_index, other);
667 other->AddEnvUseAt(user, input_index);
668 }
669
David Brazdiled596192015-01-23 10:39:45 +0000670 uses_.Clear();
671 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100672}
673
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100674void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000675 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100676 SetRawInputAt(index, replacement);
677 replacement->AddUseAt(this, index);
678}
679
Nicolas Geoffray39468442014-09-02 15:17:15 +0100680size_t HInstruction::EnvironmentSize() const {
681 return HasEnvironment() ? environment_->Size() : 0;
682}
683
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100684void HPhi::AddInput(HInstruction* input) {
685 DCHECK(input->GetBlock() != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +0000686 inputs_.Add(HUserRecord<HInstruction*>(input));
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100687 input->AddUseAt(this, inputs_.Size() - 1);
688}
689
David Brazdil2d7352b2015-04-20 14:52:42 +0100690void HPhi::RemoveInputAt(size_t index) {
691 RemoveAsUserOfInput(index);
692 inputs_.DeleteAt(index);
693}
694
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100695#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000696void H##name::Accept(HGraphVisitor* visitor) { \
697 visitor->Visit##name(this); \
698}
699
700FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
701
702#undef DEFINE_ACCEPT
703
704void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100705 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
706 for (size_t i = 0 ; i < blocks.Size(); i++) {
David Brazdil46e2a392015-03-16 17:31:52 +0000707 HBasicBlock* block = blocks.Get(i);
708 if (block != nullptr) {
709 VisitBasicBlock(block);
710 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000711 }
712}
713
Roland Levillain633021e2014-10-01 14:12:25 +0100714void HGraphVisitor::VisitReversePostOrder() {
715 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
716 VisitBasicBlock(it.Current());
717 }
718}
719
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000720void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100721 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100722 it.Current()->Accept(this);
723 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100724 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000725 it.Current()->Accept(this);
726 }
727}
728
Roland Levillain9240d6a2014-10-20 16:47:04 +0100729HConstant* HUnaryOperation::TryStaticEvaluation() const {
730 if (GetInput()->IsIntConstant()) {
731 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000732 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain9240d6a2014-10-20 16:47:04 +0100733 } else if (GetInput()->IsLongConstant()) {
Roland Levillainb762d2e2014-10-22 10:11:06 +0100734 // TODO: Implement static evaluation of long unary operations.
735 //
736 // Do not exit with a fatal condition here. Instead, simply
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700737 // return `null' to notify the caller that this instruction
Roland Levillainb762d2e2014-10-22 10:11:06 +0100738 // cannot (yet) be statically evaluated.
Roland Levillain9240d6a2014-10-20 16:47:04 +0100739 return nullptr;
740 }
741 return nullptr;
742}
743
744HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +0100745 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
746 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
747 GetRight()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000748 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100749 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
750 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
751 GetRight()->AsLongConstant()->GetValue());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000752 if (GetResultType() == Primitive::kPrimLong) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000753 return GetBlock()->GetGraph()->GetLongConstant(value);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000754 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000755 DCHECK_EQ(GetResultType(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000756 return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000757 }
Roland Levillain556c3d12014-09-18 15:25:07 +0100758 }
759 return nullptr;
760}
Dave Allison20dfc792014-06-16 20:44:29 -0700761
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000762HConstant* HBinaryOperation::GetConstantRight() const {
763 if (GetRight()->IsConstant()) {
764 return GetRight()->AsConstant();
765 } else if (IsCommutative() && GetLeft()->IsConstant()) {
766 return GetLeft()->AsConstant();
767 } else {
768 return nullptr;
769 }
770}
771
772// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700773// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000774HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
775 HInstruction* most_constant_right = GetConstantRight();
776 if (most_constant_right == nullptr) {
777 return nullptr;
778 } else if (most_constant_right == GetLeft()) {
779 return GetRight();
780 } else {
781 return GetLeft();
782 }
783}
784
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700785bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
786 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100787}
788
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100789bool HInstruction::Equals(HInstruction* other) const {
790 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100791 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100792 if (!InstructionDataEquals(other)) return false;
793 if (GetType() != other->GetType()) return false;
794 if (InputCount() != other->InputCount()) return false;
795
796 for (size_t i = 0, e = InputCount(); i < e; ++i) {
797 if (InputAt(i) != other->InputAt(i)) return false;
798 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100799 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100800 return true;
801}
802
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700803std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
804#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
805 switch (rhs) {
806 FOR_EACH_INSTRUCTION(DECLARE_CASE)
807 default:
808 os << "Unknown instruction kind " << static_cast<int>(rhs);
809 break;
810 }
811#undef DECLARE_CASE
812 return os;
813}
814
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000815void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000816 next_->previous_ = previous_;
817 if (previous_ != nullptr) {
818 previous_->next_ = next_;
819 }
820 if (block_->instructions_.first_instruction_ == this) {
821 block_->instructions_.first_instruction_ = next_;
822 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000823 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000824
825 previous_ = cursor->previous_;
826 if (previous_ != nullptr) {
827 previous_->next_ = this;
828 }
829 next_ = cursor;
830 cursor->previous_ = this;
831 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000832
833 if (block_->instructions_.first_instruction_ == cursor) {
834 block_->instructions_.first_instruction_ = this;
835 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000836}
837
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000838HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
839 DCHECK(!cursor->IsControlFlow());
840 DCHECK_NE(instructions_.last_instruction_, cursor);
841 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000842
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000843 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
844 new_block->instructions_.first_instruction_ = cursor->GetNext();
845 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
846 cursor->next_->previous_ = nullptr;
847 cursor->next_ = nullptr;
848 instructions_.last_instruction_ = cursor;
849
850 new_block->instructions_.SetBlockOfInstructions(new_block);
851 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
852 HBasicBlock* successor = GetSuccessors().Get(i);
853 new_block->successors_.Add(successor);
854 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
855 }
856 successors_.Reset();
857
858 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
859 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
860 dominated->dominator_ = new_block;
861 new_block->dominated_blocks_.Add(dominated);
862 }
863 dominated_blocks_.Reset();
864 return new_block;
865}
866
David Brazdil46e2a392015-03-16 17:31:52 +0000867bool HBasicBlock::IsSingleGoto() const {
868 HLoopInformation* loop_info = GetLoopInformation();
869 // TODO: Remove the null check b/19084197.
870 return GetFirstInstruction() != nullptr
871 && GetPhis().IsEmpty()
872 && GetFirstInstruction() == GetLastInstruction()
873 && GetLastInstruction()->IsGoto()
874 // Back edges generate the suspend check.
875 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
876}
877
David Brazdil8d5b8b22015-03-24 10:51:52 +0000878bool HBasicBlock::EndsWithControlFlowInstruction() const {
879 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
880}
881
David Brazdilb2bd1c52015-03-25 11:17:37 +0000882bool HBasicBlock::EndsWithIf() const {
883 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
884}
885
886bool HBasicBlock::HasSinglePhi() const {
887 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
888}
889
David Brazdil2d7352b2015-04-20 14:52:42 +0100890size_t HInstructionList::CountSize() const {
891 size_t size = 0;
892 HInstruction* current = first_instruction_;
893 for (; current != nullptr; current = current->GetNext()) {
894 size++;
895 }
896 return size;
897}
898
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000899void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
900 for (HInstruction* current = first_instruction_;
901 current != nullptr;
902 current = current->GetNext()) {
903 current->SetBlock(block);
904 }
905}
906
907void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
908 DCHECK(Contains(cursor));
909 if (!instruction_list.IsEmpty()) {
910 if (cursor == last_instruction_) {
911 last_instruction_ = instruction_list.last_instruction_;
912 } else {
913 cursor->next_->previous_ = instruction_list.last_instruction_;
914 }
915 instruction_list.last_instruction_->next_ = cursor->next_;
916 cursor->next_ = instruction_list.first_instruction_;
917 instruction_list.first_instruction_->previous_ = cursor;
918 }
919}
920
921void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +0000922 if (IsEmpty()) {
923 first_instruction_ = instruction_list.first_instruction_;
924 last_instruction_ = instruction_list.last_instruction_;
925 } else {
926 AddAfter(last_instruction_, instruction_list);
927 }
928}
929
David Brazdil2d7352b2015-04-20 14:52:42 +0100930void HBasicBlock::DisconnectAndDelete() {
931 // Dominators must be removed after all the blocks they dominate. This way
932 // a loop header is removed last, a requirement for correct loop information
933 // iteration.
934 DCHECK(dominated_blocks_.IsEmpty());
David Brazdil46e2a392015-03-16 17:31:52 +0000935
David Brazdil2d7352b2015-04-20 14:52:42 +0100936 // Remove the block from all loops it is included in.
937 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
938 HLoopInformation* loop_info = it.Current();
939 loop_info->Remove(this);
940 if (loop_info->IsBackEdge(*this)) {
941 // This deliberately leaves the loop in an inconsistent state and will
942 // fail SSAChecker unless the entire loop is removed during the pass.
943 loop_info->RemoveBackEdge(this);
944 }
945 }
946
947 // Disconnect the block from its predecessors and update their control-flow
948 // instructions.
David Brazdil46e2a392015-03-16 17:31:52 +0000949 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
David Brazdil2d7352b2015-04-20 14:52:42 +0100950 HBasicBlock* predecessor = predecessors_.Get(i);
951 HInstruction* last_instruction = predecessor->GetLastInstruction();
952 predecessor->RemoveInstruction(last_instruction);
953 predecessor->RemoveSuccessor(this);
954 if (predecessor->GetSuccessors().Size() == 1u) {
955 DCHECK(last_instruction->IsIf());
956 predecessor->AddInstruction(new (graph_->GetArena()) HGoto());
957 } else {
958 // The predecessor has no remaining successors and therefore must be dead.
959 // We deliberately leave it without a control-flow instruction so that the
960 // SSAChecker fails unless it is not removed during the pass too.
961 DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u);
962 }
David Brazdil46e2a392015-03-16 17:31:52 +0000963 }
David Brazdil46e2a392015-03-16 17:31:52 +0000964 predecessors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +0100965
966 // Disconnect the block from its successors and update their dominators
967 // and phis.
968 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
969 HBasicBlock* successor = successors_.Get(i);
970 // Delete this block from the list of predecessors.
971 size_t this_index = successor->GetPredecessorIndexOf(this);
972 successor->predecessors_.DeleteAt(this_index);
973
974 // Check that `successor` has other predecessors, otherwise `this` is the
975 // dominator of `successor` which violates the order DCHECKed at the top.
976 DCHECK(!successor->predecessors_.IsEmpty());
977
978 // Recompute the successor's dominator.
979 HBasicBlock* old_dominator = successor->GetDominator();
980 HBasicBlock* new_dominator = successor->predecessors_.Get(0);
981 for (size_t j = 1, f = successor->predecessors_.Size(); j < f; ++j) {
982 new_dominator = graph_->FindCommonDominator(
983 new_dominator, successor->predecessors_.Get(j));
984 }
985 if (old_dominator != new_dominator) {
986 successor->SetDominator(new_dominator);
987 old_dominator->RemoveDominatedBlock(successor);
988 new_dominator->AddDominatedBlock(successor);
989 }
990
991 // Remove this block's entries in the successor's phis.
992 if (successor->predecessors_.Size() == 1u) {
993 // The successor has just one predecessor left. Replace phis with the only
994 // remaining input.
995 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
996 HPhi* phi = phi_it.Current()->AsPhi();
997 phi->ReplaceWith(phi->InputAt(1 - this_index));
998 successor->RemovePhi(phi);
999 }
1000 } else {
1001 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1002 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1003 }
1004 }
1005 }
David Brazdil46e2a392015-03-16 17:31:52 +00001006 successors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001007
1008 // Disconnect from the dominator.
1009 dominator_->RemoveDominatedBlock(this);
1010 SetDominator(nullptr);
1011
1012 // Delete from the graph. The function safely deletes remaining instructions
1013 // and updates the reverse post order.
1014 graph_->DeleteDeadBlock(this);
1015 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001016}
1017
1018void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001019 DCHECK_EQ(GetGraph(), other->GetGraph());
1020 DCHECK(GetDominatedBlocks().Contains(other));
1021 DCHECK_EQ(GetSuccessors().Size(), 1u);
1022 DCHECK_EQ(GetSuccessors().Get(0), other);
1023 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1024 DCHECK_EQ(other->GetPredecessors().Get(0), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001025 DCHECK(other->GetPhis().IsEmpty());
1026
David Brazdil2d7352b2015-04-20 14:52:42 +01001027 // Move instructions from `other` to `this`.
1028 DCHECK(EndsWithControlFlowInstruction());
1029 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001030 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001031 other->instructions_.SetBlockOfInstructions(this);
1032 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001033
David Brazdil2d7352b2015-04-20 14:52:42 +01001034 // Remove `other` from the loops it is included in.
1035 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1036 HLoopInformation* loop_info = it.Current();
1037 loop_info->Remove(other);
1038 if (loop_info->IsBackEdge(*other)) {
1039 loop_info->ClearBackEdges();
1040 loop_info->AddBackEdge(this);
1041 }
1042 }
1043
1044 // Update links to the successors of `other`.
1045 successors_.Reset();
1046 while (!other->successors_.IsEmpty()) {
1047 HBasicBlock* successor = other->successors_.Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001048 successor->ReplacePredecessor(other, this);
1049 }
1050
David Brazdil2d7352b2015-04-20 14:52:42 +01001051 // Update the dominator tree.
1052 dominated_blocks_.Delete(other);
1053 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1054 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1055 dominated_blocks_.Add(dominated);
1056 dominated->SetDominator(this);
1057 }
1058 other->dominated_blocks_.Reset();
1059 other->dominator_ = nullptr;
1060
1061 // Clear the list of predecessors of `other` in preparation of deleting it.
1062 other->predecessors_.Reset();
1063
1064 // Delete `other` from the graph. The function updates reverse post order.
1065 graph_->DeleteDeadBlock(other);
1066 other->SetGraph(nullptr);
1067}
1068
1069void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1070 DCHECK_NE(GetGraph(), other->GetGraph());
1071 DCHECK(GetDominatedBlocks().IsEmpty());
1072 DCHECK(GetSuccessors().IsEmpty());
1073 DCHECK(!EndsWithControlFlowInstruction());
1074 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1075 DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock());
1076 DCHECK(other->GetPhis().IsEmpty());
1077 DCHECK(!other->IsInLoop());
1078
1079 // Move instructions from `other` to `this`.
1080 instructions_.Add(other->GetInstructions());
1081 other->instructions_.SetBlockOfInstructions(this);
1082
1083 // Update links to the successors of `other`.
1084 successors_.Reset();
1085 while (!other->successors_.IsEmpty()) {
1086 HBasicBlock* successor = other->successors_.Get(0);
1087 successor->ReplacePredecessor(other, this);
1088 }
1089
1090 // Update the dominator tree.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001091 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1092 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1093 dominated_blocks_.Add(dominated);
1094 dominated->SetDominator(this);
1095 }
1096 other->dominated_blocks_.Reset();
1097 other->dominator_ = nullptr;
1098 other->graph_ = nullptr;
1099}
1100
1101void HBasicBlock::ReplaceWith(HBasicBlock* other) {
1102 while (!GetPredecessors().IsEmpty()) {
1103 HBasicBlock* predecessor = GetPredecessors().Get(0);
1104 predecessor->ReplaceSuccessor(this, other);
1105 }
1106 while (!GetSuccessors().IsEmpty()) {
1107 HBasicBlock* successor = GetSuccessors().Get(0);
1108 successor->ReplacePredecessor(this, other);
1109 }
1110 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
1111 other->AddDominatedBlock(dominated_blocks_.Get(i));
1112 }
1113 GetDominator()->ReplaceDominatedBlock(this, other);
1114 other->SetDominator(GetDominator());
1115 dominator_ = nullptr;
1116 graph_ = nullptr;
1117}
1118
1119// Create space in `blocks` for adding `number_of_new_blocks` entries
1120// starting at location `at`. Blocks after `at` are moved accordingly.
1121static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
1122 size_t number_of_new_blocks,
1123 size_t at) {
1124 size_t old_size = blocks->Size();
1125 size_t new_size = old_size + number_of_new_blocks;
1126 blocks->SetSize(new_size);
1127 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
1128 blocks->Put(j, blocks->Get(i));
1129 }
1130}
1131
David Brazdil2d7352b2015-04-20 14:52:42 +01001132void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1133 DCHECK_EQ(block->GetGraph(), this);
1134 DCHECK(block->GetSuccessors().IsEmpty());
1135 DCHECK(block->GetPredecessors().IsEmpty());
1136 DCHECK(block->GetDominatedBlocks().IsEmpty());
1137 DCHECK(block->GetDominator() == nullptr);
1138
1139 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1140 block->RemoveInstruction(it.Current());
1141 }
1142 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1143 block->RemovePhi(it.Current()->AsPhi());
1144 }
1145
1146 reverse_post_order_.Delete(block);
1147 blocks_.Put(block->GetBlockId(), nullptr);
1148}
1149
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001150void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001151 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001152 // Simple case of an entry block, a body block, and an exit block.
1153 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001154 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001155 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
1156 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001157 DCHECK(!body->IsExitBlock());
1158 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001159
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001160 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1161 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001162
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001163 // Replace the invoke with the return value of the inlined graph.
1164 if (last->IsReturn()) {
1165 invoke->ReplaceWith(last->InputAt(0));
1166 } else {
1167 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001168 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001169
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001170 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001171 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001172 // Need to inline multiple blocks. We split `invoke`'s block
1173 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001174 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001175 // with the second half.
1176 ArenaAllocator* allocator = outer_graph->GetArena();
1177 HBasicBlock* at = invoke->GetBlock();
1178 HBasicBlock* to = at->SplitAfter(invoke);
1179
1180 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
1181 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001182 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001183 exit_block_->ReplaceWith(to);
1184
1185 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001186 // to not `HReturn` but `HGoto` instead.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001187 HInstruction* return_value = nullptr;
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001188 bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
1189 if (to->GetPredecessors().Size() == 1) {
1190 HBasicBlock* predecessor = to->GetPredecessors().Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001191 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001192 if (!returns_void) {
1193 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001194 }
1195 predecessor->AddInstruction(new (allocator) HGoto());
1196 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001197 } else {
1198 if (!returns_void) {
1199 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001200 return_value = new (allocator) HPhi(
1201 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001202 to->AddPhi(return_value->AsPhi());
1203 }
1204 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
1205 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
1206 HInstruction* last = predecessor->GetLastInstruction();
1207 if (!returns_void) {
1208 return_value->AsPhi()->AddInput(last->InputAt(0));
1209 }
1210 predecessor->AddInstruction(new (allocator) HGoto());
1211 predecessor->RemoveInstruction(last);
1212 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001213 }
1214
1215 if (return_value != nullptr) {
1216 invoke->ReplaceWith(return_value);
1217 }
1218
1219 // Update the meta information surrounding blocks:
1220 // (1) the graph they are now in,
1221 // (2) the reverse post order of that graph,
1222 // (3) the potential loop information they are now in.
1223
1224 // We don't add the entry block, the exit block, and the first block, which
1225 // has been merged with `at`.
1226 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1227
1228 // We add the `to` block.
1229 static constexpr int kNumberOfNewBlocksInCaller = 1;
1230 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
1231 + kNumberOfNewBlocksInCaller;
1232
1233 // Find the location of `at` in the outer graph's reverse post order. The new
1234 // blocks will be added after it.
1235 size_t index_of_at = 0;
1236 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
1237 index_of_at++;
1238 }
1239 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1240
1241 // Do a reverse post order of the blocks in the callee and do (1), (2),
1242 // and (3) to the blocks that apply.
1243 HLoopInformation* info = at->GetLoopInformation();
1244 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1245 HBasicBlock* current = it.Current();
1246 if (current != exit_block_ && current != entry_block_ && current != first) {
1247 DCHECK(!current->IsInLoop());
1248 DCHECK(current->GetGraph() == this);
1249 current->SetGraph(outer_graph);
1250 outer_graph->AddBlock(current);
1251 outer_graph->reverse_post_order_.Put(++index_of_at, current);
1252 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001253 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001254 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1255 loop_it.Current()->Add(current);
1256 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001257 }
1258 }
1259 }
1260
1261 // Do (1), (2), and (3) to `to`.
1262 to->SetGraph(outer_graph);
1263 outer_graph->AddBlock(to);
1264 outer_graph->reverse_post_order_.Put(++index_of_at, to);
1265 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001266 to->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001267 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1268 loop_it.Current()->Add(to);
1269 }
David Brazdil46e2a392015-03-16 17:31:52 +00001270 if (info->IsBackEdge(*at)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001271 // Only `at` can become a back edge, as the inlined blocks
1272 // are predecessors of `at`.
1273 DCHECK_EQ(1u, info->NumberOfBackEdges());
1274 info->ClearBackEdges();
1275 info->AddBackEdge(to);
1276 }
1277 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001278 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001279
David Brazdil05144f42015-04-16 15:18:00 +01001280 // Update the next instruction id of the outer graph, so that instructions
1281 // added later get bigger ids than those in the inner graph.
1282 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1283
1284 // Walk over the entry block and:
1285 // - Move constants from the entry block to the outer_graph's entry block,
1286 // - Replace HParameterValue instructions with their real value.
1287 // - Remove suspend checks, that hold an environment.
1288 // We must do this after the other blocks have been inlined, otherwise ids of
1289 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001290 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001291 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1292 HInstruction* current = it.Current();
1293 if (current->IsNullConstant()) {
1294 current->ReplaceWith(outer_graph->GetNullConstant());
1295 } else if (current->IsIntConstant()) {
1296 current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue()));
1297 } else if (current->IsLongConstant()) {
1298 current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue()));
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001299 } else if (current->IsFloatConstant()) {
1300 current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue()));
1301 } else if (current->IsDoubleConstant()) {
1302 current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue()));
David Brazdil05144f42015-04-16 15:18:00 +01001303 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001304 if (kIsDebugBuild
1305 && invoke->IsInvokeStaticOrDirect()
1306 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1307 // Ensure we do not use the last input of `invoke`, as it
1308 // contains a clinit check which is not an actual argument.
1309 size_t last_input_index = invoke->InputCount() - 1;
1310 DCHECK(parameter_index != last_input_index);
1311 }
David Brazdil05144f42015-04-16 15:18:00 +01001312 current->ReplaceWith(invoke->InputAt(parameter_index++));
1313 } else {
1314 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1315 entry_block_->RemoveInstruction(current);
1316 }
1317 }
1318
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001319 // Finally remove the invoke from the caller.
1320 invoke->GetBlock()->RemoveInstruction(invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001321}
1322
Calin Juravleacf735c2015-02-12 15:25:22 +00001323std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1324 ScopedObjectAccess soa(Thread::Current());
1325 os << "["
1326 << " is_top=" << rhs.IsTop()
1327 << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
1328 << " is_exact=" << rhs.IsExact()
1329 << " ]";
1330 return os;
1331}
1332
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001333} // namespace art