blob: e2eb46aabbddc079ecc52705dcdad40ff39d2431 [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) {
David Brazdilc7508e92015-04-27 13:28:57 +0100477 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000478 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100479}
480
David Brazdil1abb4192015-02-17 18:33:36 +0000481void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
482 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100483}
484
David Brazdilc7508e92015-04-27 13:28:57 +0100485void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
486 if (instruction->IsPhi()) {
487 RemovePhi(instruction->AsPhi(), ensure_safety);
488 } else {
489 RemoveInstruction(instruction, ensure_safety);
490 }
491}
492
David Brazdiled596192015-01-23 10:39:45 +0000493void HEnvironment::CopyFrom(HEnvironment* env) {
494 for (size_t i = 0; i < env->Size(); i++) {
495 HInstruction* instruction = env->GetInstructionAt(i);
496 SetRawEnvAt(i, instruction);
497 if (instruction != nullptr) {
498 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100499 }
David Brazdiled596192015-01-23 10:39:45 +0000500 }
501}
502
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700503void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
504 HBasicBlock* loop_header) {
505 DCHECK(loop_header->IsLoopHeader());
506 for (size_t i = 0; i < env->Size(); i++) {
507 HInstruction* instruction = env->GetInstructionAt(i);
508 SetRawEnvAt(i, instruction);
509 if (instruction == nullptr) {
510 continue;
511 }
512 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
513 // At the end of the loop pre-header, the corresponding value for instruction
514 // is the first input of the phi.
515 HInstruction* initial = instruction->AsPhi()->InputAt(0);
516 DCHECK(initial->GetBlock()->Dominates(loop_header));
517 SetRawEnvAt(i, initial);
518 initial->AddEnvUseAt(this, i);
519 } else {
520 instruction->AddEnvUseAt(this, i);
521 }
522 }
523}
524
David Brazdil1abb4192015-02-17 18:33:36 +0000525void HEnvironment::RemoveAsUserOfInput(size_t index) const {
526 const HUserRecord<HEnvironment*> user_record = vregs_.Get(index);
527 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100528}
529
Calin Juravle77520bc2015-01-12 18:45:46 +0000530HInstruction* HInstruction::GetNextDisregardingMoves() const {
531 HInstruction* next = GetNext();
532 while (next != nullptr && next->IsParallelMove()) {
533 next = next->GetNext();
534 }
535 return next;
536}
537
538HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
539 HInstruction* previous = GetPrevious();
540 while (previous != nullptr && previous->IsParallelMove()) {
541 previous = previous->GetPrevious();
542 }
543 return previous;
544}
545
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100546void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000547 if (first_instruction_ == nullptr) {
548 DCHECK(last_instruction_ == nullptr);
549 first_instruction_ = last_instruction_ = instruction;
550 } else {
551 last_instruction_->next_ = instruction;
552 instruction->previous_ = last_instruction_;
553 last_instruction_ = instruction;
554 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000555}
556
David Brazdilc3d743f2015-04-22 13:40:50 +0100557void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
558 DCHECK(Contains(cursor));
559 if (cursor == first_instruction_) {
560 cursor->previous_ = instruction;
561 instruction->next_ = cursor;
562 first_instruction_ = instruction;
563 } else {
564 instruction->previous_ = cursor->previous_;
565 instruction->next_ = cursor;
566 cursor->previous_ = instruction;
567 instruction->previous_->next_ = instruction;
568 }
569}
570
571void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
572 DCHECK(Contains(cursor));
573 if (cursor == last_instruction_) {
574 cursor->next_ = instruction;
575 instruction->previous_ = cursor;
576 last_instruction_ = instruction;
577 } else {
578 instruction->next_ = cursor->next_;
579 instruction->previous_ = cursor;
580 cursor->next_ = instruction;
581 instruction->next_->previous_ = instruction;
582 }
583}
584
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100585void HInstructionList::RemoveInstruction(HInstruction* instruction) {
586 if (instruction->previous_ != nullptr) {
587 instruction->previous_->next_ = instruction->next_;
588 }
589 if (instruction->next_ != nullptr) {
590 instruction->next_->previous_ = instruction->previous_;
591 }
592 if (instruction == first_instruction_) {
593 first_instruction_ = instruction->next_;
594 }
595 if (instruction == last_instruction_) {
596 last_instruction_ = instruction->previous_;
597 }
598}
599
Roland Levillain6b469232014-09-25 10:10:38 +0100600bool HInstructionList::Contains(HInstruction* instruction) const {
601 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
602 if (it.Current() == instruction) {
603 return true;
604 }
605 }
606 return false;
607}
608
Roland Levillainccc07a92014-09-16 14:48:16 +0100609bool HInstructionList::FoundBefore(const HInstruction* instruction1,
610 const HInstruction* instruction2) const {
611 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
612 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
613 if (it.Current() == instruction1) {
614 return true;
615 }
616 if (it.Current() == instruction2) {
617 return false;
618 }
619 }
620 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
621 return true;
622}
623
Roland Levillain6c82d402014-10-13 16:10:27 +0100624bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
625 if (other_instruction == this) {
626 // An instruction does not strictly dominate itself.
627 return false;
628 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100629 HBasicBlock* block = GetBlock();
630 HBasicBlock* other_block = other_instruction->GetBlock();
631 if (block != other_block) {
632 return GetBlock()->Dominates(other_instruction->GetBlock());
633 } else {
634 // If both instructions are in the same block, ensure this
635 // instruction comes before `other_instruction`.
636 if (IsPhi()) {
637 if (!other_instruction->IsPhi()) {
638 // Phis appear before non phi-instructions so this instruction
639 // dominates `other_instruction`.
640 return true;
641 } else {
642 // There is no order among phis.
643 LOG(FATAL) << "There is no dominance between phis of a same block.";
644 return false;
645 }
646 } else {
647 // `this` is not a phi.
648 if (other_instruction->IsPhi()) {
649 // Phis appear before non phi-instructions so this instruction
650 // does not dominate `other_instruction`.
651 return false;
652 } else {
653 // Check whether this instruction comes before
654 // `other_instruction` in the instruction list.
655 return block->GetInstructions().FoundBefore(this, other_instruction);
656 }
657 }
658 }
659}
660
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100661void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100662 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000663 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
664 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100665 HInstruction* user = current->GetUser();
666 size_t input_index = current->GetIndex();
667 user->SetRawInputAt(input_index, other);
668 other->AddUseAt(user, input_index);
669 }
670
David Brazdiled596192015-01-23 10:39:45 +0000671 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
672 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100673 HEnvironment* user = current->GetUser();
674 size_t input_index = current->GetIndex();
675 user->SetRawEnvAt(input_index, other);
676 other->AddEnvUseAt(user, input_index);
677 }
678
David Brazdiled596192015-01-23 10:39:45 +0000679 uses_.Clear();
680 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681}
682
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100683void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000684 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100685 SetRawInputAt(index, replacement);
686 replacement->AddUseAt(this, index);
687}
688
Nicolas Geoffray39468442014-09-02 15:17:15 +0100689size_t HInstruction::EnvironmentSize() const {
690 return HasEnvironment() ? environment_->Size() : 0;
691}
692
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100693void HPhi::AddInput(HInstruction* input) {
694 DCHECK(input->GetBlock() != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +0000695 inputs_.Add(HUserRecord<HInstruction*>(input));
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100696 input->AddUseAt(this, inputs_.Size() - 1);
697}
698
David Brazdil2d7352b2015-04-20 14:52:42 +0100699void HPhi::RemoveInputAt(size_t index) {
700 RemoveAsUserOfInput(index);
701 inputs_.DeleteAt(index);
702}
703
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100704#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000705void H##name::Accept(HGraphVisitor* visitor) { \
706 visitor->Visit##name(this); \
707}
708
709FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
710
711#undef DEFINE_ACCEPT
712
713void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100714 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
715 for (size_t i = 0 ; i < blocks.Size(); i++) {
David Brazdil46e2a392015-03-16 17:31:52 +0000716 HBasicBlock* block = blocks.Get(i);
717 if (block != nullptr) {
718 VisitBasicBlock(block);
719 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000720 }
721}
722
Roland Levillain633021e2014-10-01 14:12:25 +0100723void HGraphVisitor::VisitReversePostOrder() {
724 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
725 VisitBasicBlock(it.Current());
726 }
727}
728
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000729void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100730 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100731 it.Current()->Accept(this);
732 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100733 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000734 it.Current()->Accept(this);
735 }
736}
737
Roland Levillain9240d6a2014-10-20 16:47:04 +0100738HConstant* HUnaryOperation::TryStaticEvaluation() const {
739 if (GetInput()->IsIntConstant()) {
740 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000741 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain9240d6a2014-10-20 16:47:04 +0100742 } else if (GetInput()->IsLongConstant()) {
Roland Levillainb762d2e2014-10-22 10:11:06 +0100743 // TODO: Implement static evaluation of long unary operations.
744 //
745 // Do not exit with a fatal condition here. Instead, simply
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700746 // return `null' to notify the caller that this instruction
Roland Levillainb762d2e2014-10-22 10:11:06 +0100747 // cannot (yet) be statically evaluated.
Roland Levillain9240d6a2014-10-20 16:47:04 +0100748 return nullptr;
749 }
750 return nullptr;
751}
752
753HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +0100754 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
755 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
756 GetRight()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000757 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100758 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
759 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
760 GetRight()->AsLongConstant()->GetValue());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000761 if (GetResultType() == Primitive::kPrimLong) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000762 return GetBlock()->GetGraph()->GetLongConstant(value);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000763 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000764 DCHECK_EQ(GetResultType(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000765 return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000766 }
Roland Levillain556c3d12014-09-18 15:25:07 +0100767 }
768 return nullptr;
769}
Dave Allison20dfc792014-06-16 20:44:29 -0700770
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000771HConstant* HBinaryOperation::GetConstantRight() const {
772 if (GetRight()->IsConstant()) {
773 return GetRight()->AsConstant();
774 } else if (IsCommutative() && GetLeft()->IsConstant()) {
775 return GetLeft()->AsConstant();
776 } else {
777 return nullptr;
778 }
779}
780
781// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700782// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000783HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
784 HInstruction* most_constant_right = GetConstantRight();
785 if (most_constant_right == nullptr) {
786 return nullptr;
787 } else if (most_constant_right == GetLeft()) {
788 return GetRight();
789 } else {
790 return GetLeft();
791 }
792}
793
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700794bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
795 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100796}
797
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100798bool HInstruction::Equals(HInstruction* other) const {
799 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100800 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100801 if (!InstructionDataEquals(other)) return false;
802 if (GetType() != other->GetType()) return false;
803 if (InputCount() != other->InputCount()) return false;
804
805 for (size_t i = 0, e = InputCount(); i < e; ++i) {
806 if (InputAt(i) != other->InputAt(i)) return false;
807 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100808 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100809 return true;
810}
811
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700812std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
813#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
814 switch (rhs) {
815 FOR_EACH_INSTRUCTION(DECLARE_CASE)
816 default:
817 os << "Unknown instruction kind " << static_cast<int>(rhs);
818 break;
819 }
820#undef DECLARE_CASE
821 return os;
822}
823
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000824void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000825 next_->previous_ = previous_;
826 if (previous_ != nullptr) {
827 previous_->next_ = next_;
828 }
829 if (block_->instructions_.first_instruction_ == this) {
830 block_->instructions_.first_instruction_ = next_;
831 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000832 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000833
834 previous_ = cursor->previous_;
835 if (previous_ != nullptr) {
836 previous_->next_ = this;
837 }
838 next_ = cursor;
839 cursor->previous_ = this;
840 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000841
842 if (block_->instructions_.first_instruction_ == cursor) {
843 block_->instructions_.first_instruction_ = this;
844 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000845}
846
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000847HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
848 DCHECK(!cursor->IsControlFlow());
849 DCHECK_NE(instructions_.last_instruction_, cursor);
850 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000851
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000852 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
853 new_block->instructions_.first_instruction_ = cursor->GetNext();
854 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
855 cursor->next_->previous_ = nullptr;
856 cursor->next_ = nullptr;
857 instructions_.last_instruction_ = cursor;
858
859 new_block->instructions_.SetBlockOfInstructions(new_block);
860 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
861 HBasicBlock* successor = GetSuccessors().Get(i);
862 new_block->successors_.Add(successor);
863 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
864 }
865 successors_.Reset();
866
867 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
868 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
869 dominated->dominator_ = new_block;
870 new_block->dominated_blocks_.Add(dominated);
871 }
872 dominated_blocks_.Reset();
873 return new_block;
874}
875
David Brazdil46e2a392015-03-16 17:31:52 +0000876bool HBasicBlock::IsSingleGoto() const {
877 HLoopInformation* loop_info = GetLoopInformation();
878 // TODO: Remove the null check b/19084197.
879 return GetFirstInstruction() != nullptr
880 && GetPhis().IsEmpty()
881 && GetFirstInstruction() == GetLastInstruction()
882 && GetLastInstruction()->IsGoto()
883 // Back edges generate the suspend check.
884 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
885}
886
David Brazdil8d5b8b22015-03-24 10:51:52 +0000887bool HBasicBlock::EndsWithControlFlowInstruction() const {
888 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
889}
890
David Brazdilb2bd1c52015-03-25 11:17:37 +0000891bool HBasicBlock::EndsWithIf() const {
892 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
893}
894
895bool HBasicBlock::HasSinglePhi() const {
896 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
897}
898
David Brazdil2d7352b2015-04-20 14:52:42 +0100899size_t HInstructionList::CountSize() const {
900 size_t size = 0;
901 HInstruction* current = first_instruction_;
902 for (; current != nullptr; current = current->GetNext()) {
903 size++;
904 }
905 return size;
906}
907
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000908void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
909 for (HInstruction* current = first_instruction_;
910 current != nullptr;
911 current = current->GetNext()) {
912 current->SetBlock(block);
913 }
914}
915
916void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
917 DCHECK(Contains(cursor));
918 if (!instruction_list.IsEmpty()) {
919 if (cursor == last_instruction_) {
920 last_instruction_ = instruction_list.last_instruction_;
921 } else {
922 cursor->next_->previous_ = instruction_list.last_instruction_;
923 }
924 instruction_list.last_instruction_->next_ = cursor->next_;
925 cursor->next_ = instruction_list.first_instruction_;
926 instruction_list.first_instruction_->previous_ = cursor;
927 }
928}
929
930void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +0000931 if (IsEmpty()) {
932 first_instruction_ = instruction_list.first_instruction_;
933 last_instruction_ = instruction_list.last_instruction_;
934 } else {
935 AddAfter(last_instruction_, instruction_list);
936 }
937}
938
David Brazdil2d7352b2015-04-20 14:52:42 +0100939void HBasicBlock::DisconnectAndDelete() {
940 // Dominators must be removed after all the blocks they dominate. This way
941 // a loop header is removed last, a requirement for correct loop information
942 // iteration.
943 DCHECK(dominated_blocks_.IsEmpty());
David Brazdil46e2a392015-03-16 17:31:52 +0000944
David Brazdil2d7352b2015-04-20 14:52:42 +0100945 // Remove the block from all loops it is included in.
946 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
947 HLoopInformation* loop_info = it.Current();
948 loop_info->Remove(this);
949 if (loop_info->IsBackEdge(*this)) {
950 // This deliberately leaves the loop in an inconsistent state and will
951 // fail SSAChecker unless the entire loop is removed during the pass.
952 loop_info->RemoveBackEdge(this);
953 }
954 }
955
956 // Disconnect the block from its predecessors and update their control-flow
957 // instructions.
David Brazdil46e2a392015-03-16 17:31:52 +0000958 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
David Brazdil2d7352b2015-04-20 14:52:42 +0100959 HBasicBlock* predecessor = predecessors_.Get(i);
960 HInstruction* last_instruction = predecessor->GetLastInstruction();
961 predecessor->RemoveInstruction(last_instruction);
962 predecessor->RemoveSuccessor(this);
963 if (predecessor->GetSuccessors().Size() == 1u) {
964 DCHECK(last_instruction->IsIf());
965 predecessor->AddInstruction(new (graph_->GetArena()) HGoto());
966 } else {
967 // The predecessor has no remaining successors and therefore must be dead.
968 // We deliberately leave it without a control-flow instruction so that the
969 // SSAChecker fails unless it is not removed during the pass too.
970 DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u);
971 }
David Brazdil46e2a392015-03-16 17:31:52 +0000972 }
David Brazdil46e2a392015-03-16 17:31:52 +0000973 predecessors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +0100974
975 // Disconnect the block from its successors and update their dominators
976 // and phis.
977 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
978 HBasicBlock* successor = successors_.Get(i);
979 // Delete this block from the list of predecessors.
980 size_t this_index = successor->GetPredecessorIndexOf(this);
981 successor->predecessors_.DeleteAt(this_index);
982
983 // Check that `successor` has other predecessors, otherwise `this` is the
984 // dominator of `successor` which violates the order DCHECKed at the top.
985 DCHECK(!successor->predecessors_.IsEmpty());
986
987 // Recompute the successor's dominator.
988 HBasicBlock* old_dominator = successor->GetDominator();
989 HBasicBlock* new_dominator = successor->predecessors_.Get(0);
990 for (size_t j = 1, f = successor->predecessors_.Size(); j < f; ++j) {
991 new_dominator = graph_->FindCommonDominator(
992 new_dominator, successor->predecessors_.Get(j));
993 }
994 if (old_dominator != new_dominator) {
995 successor->SetDominator(new_dominator);
996 old_dominator->RemoveDominatedBlock(successor);
997 new_dominator->AddDominatedBlock(successor);
998 }
999
1000 // Remove this block's entries in the successor's phis.
1001 if (successor->predecessors_.Size() == 1u) {
1002 // The successor has just one predecessor left. Replace phis with the only
1003 // remaining input.
1004 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1005 HPhi* phi = phi_it.Current()->AsPhi();
1006 phi->ReplaceWith(phi->InputAt(1 - this_index));
1007 successor->RemovePhi(phi);
1008 }
1009 } else {
1010 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1011 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1012 }
1013 }
1014 }
David Brazdil46e2a392015-03-16 17:31:52 +00001015 successors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001016
1017 // Disconnect from the dominator.
1018 dominator_->RemoveDominatedBlock(this);
1019 SetDominator(nullptr);
1020
1021 // Delete from the graph. The function safely deletes remaining instructions
1022 // and updates the reverse post order.
1023 graph_->DeleteDeadBlock(this);
1024 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001025}
1026
1027void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001028 DCHECK_EQ(GetGraph(), other->GetGraph());
1029 DCHECK(GetDominatedBlocks().Contains(other));
1030 DCHECK_EQ(GetSuccessors().Size(), 1u);
1031 DCHECK_EQ(GetSuccessors().Get(0), other);
1032 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1033 DCHECK_EQ(other->GetPredecessors().Get(0), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001034 DCHECK(other->GetPhis().IsEmpty());
1035
David Brazdil2d7352b2015-04-20 14:52:42 +01001036 // Move instructions from `other` to `this`.
1037 DCHECK(EndsWithControlFlowInstruction());
1038 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001039 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001040 other->instructions_.SetBlockOfInstructions(this);
1041 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001042
David Brazdil2d7352b2015-04-20 14:52:42 +01001043 // Remove `other` from the loops it is included in.
1044 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1045 HLoopInformation* loop_info = it.Current();
1046 loop_info->Remove(other);
1047 if (loop_info->IsBackEdge(*other)) {
1048 loop_info->ClearBackEdges();
1049 loop_info->AddBackEdge(this);
1050 }
1051 }
1052
1053 // Update links to the successors of `other`.
1054 successors_.Reset();
1055 while (!other->successors_.IsEmpty()) {
1056 HBasicBlock* successor = other->successors_.Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001057 successor->ReplacePredecessor(other, this);
1058 }
1059
David Brazdil2d7352b2015-04-20 14:52:42 +01001060 // Update the dominator tree.
1061 dominated_blocks_.Delete(other);
1062 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1063 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1064 dominated_blocks_.Add(dominated);
1065 dominated->SetDominator(this);
1066 }
1067 other->dominated_blocks_.Reset();
1068 other->dominator_ = nullptr;
1069
1070 // Clear the list of predecessors of `other` in preparation of deleting it.
1071 other->predecessors_.Reset();
1072
1073 // Delete `other` from the graph. The function updates reverse post order.
1074 graph_->DeleteDeadBlock(other);
1075 other->SetGraph(nullptr);
1076}
1077
1078void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1079 DCHECK_NE(GetGraph(), other->GetGraph());
1080 DCHECK(GetDominatedBlocks().IsEmpty());
1081 DCHECK(GetSuccessors().IsEmpty());
1082 DCHECK(!EndsWithControlFlowInstruction());
1083 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1084 DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock());
1085 DCHECK(other->GetPhis().IsEmpty());
1086 DCHECK(!other->IsInLoop());
1087
1088 // Move instructions from `other` to `this`.
1089 instructions_.Add(other->GetInstructions());
1090 other->instructions_.SetBlockOfInstructions(this);
1091
1092 // Update links to the successors of `other`.
1093 successors_.Reset();
1094 while (!other->successors_.IsEmpty()) {
1095 HBasicBlock* successor = other->successors_.Get(0);
1096 successor->ReplacePredecessor(other, this);
1097 }
1098
1099 // Update the dominator tree.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001100 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1101 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1102 dominated_blocks_.Add(dominated);
1103 dominated->SetDominator(this);
1104 }
1105 other->dominated_blocks_.Reset();
1106 other->dominator_ = nullptr;
1107 other->graph_ = nullptr;
1108}
1109
1110void HBasicBlock::ReplaceWith(HBasicBlock* other) {
1111 while (!GetPredecessors().IsEmpty()) {
1112 HBasicBlock* predecessor = GetPredecessors().Get(0);
1113 predecessor->ReplaceSuccessor(this, other);
1114 }
1115 while (!GetSuccessors().IsEmpty()) {
1116 HBasicBlock* successor = GetSuccessors().Get(0);
1117 successor->ReplacePredecessor(this, other);
1118 }
1119 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
1120 other->AddDominatedBlock(dominated_blocks_.Get(i));
1121 }
1122 GetDominator()->ReplaceDominatedBlock(this, other);
1123 other->SetDominator(GetDominator());
1124 dominator_ = nullptr;
1125 graph_ = nullptr;
1126}
1127
1128// Create space in `blocks` for adding `number_of_new_blocks` entries
1129// starting at location `at`. Blocks after `at` are moved accordingly.
1130static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
1131 size_t number_of_new_blocks,
1132 size_t at) {
1133 size_t old_size = blocks->Size();
1134 size_t new_size = old_size + number_of_new_blocks;
1135 blocks->SetSize(new_size);
1136 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
1137 blocks->Put(j, blocks->Get(i));
1138 }
1139}
1140
David Brazdil2d7352b2015-04-20 14:52:42 +01001141void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1142 DCHECK_EQ(block->GetGraph(), this);
1143 DCHECK(block->GetSuccessors().IsEmpty());
1144 DCHECK(block->GetPredecessors().IsEmpty());
1145 DCHECK(block->GetDominatedBlocks().IsEmpty());
1146 DCHECK(block->GetDominator() == nullptr);
1147
1148 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1149 block->RemoveInstruction(it.Current());
1150 }
1151 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1152 block->RemovePhi(it.Current()->AsPhi());
1153 }
1154
1155 reverse_post_order_.Delete(block);
1156 blocks_.Put(block->GetBlockId(), nullptr);
1157}
1158
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001159void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001160 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001161 // Simple case of an entry block, a body block, and an exit block.
1162 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001163 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001164 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
1165 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001166 DCHECK(!body->IsExitBlock());
1167 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001168
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001169 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1170 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001171
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001172 // Replace the invoke with the return value of the inlined graph.
1173 if (last->IsReturn()) {
1174 invoke->ReplaceWith(last->InputAt(0));
1175 } else {
1176 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001177 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001178
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001179 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001180 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001181 // Need to inline multiple blocks. We split `invoke`'s block
1182 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001183 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001184 // with the second half.
1185 ArenaAllocator* allocator = outer_graph->GetArena();
1186 HBasicBlock* at = invoke->GetBlock();
1187 HBasicBlock* to = at->SplitAfter(invoke);
1188
1189 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
1190 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001191 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001192 exit_block_->ReplaceWith(to);
1193
1194 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001195 // to not `HReturn` but `HGoto` instead.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001196 HInstruction* return_value = nullptr;
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001197 bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
1198 if (to->GetPredecessors().Size() == 1) {
1199 HBasicBlock* predecessor = to->GetPredecessors().Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001200 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001201 if (!returns_void) {
1202 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001203 }
1204 predecessor->AddInstruction(new (allocator) HGoto());
1205 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001206 } else {
1207 if (!returns_void) {
1208 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001209 return_value = new (allocator) HPhi(
1210 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001211 to->AddPhi(return_value->AsPhi());
1212 }
1213 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
1214 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
1215 HInstruction* last = predecessor->GetLastInstruction();
1216 if (!returns_void) {
1217 return_value->AsPhi()->AddInput(last->InputAt(0));
1218 }
1219 predecessor->AddInstruction(new (allocator) HGoto());
1220 predecessor->RemoveInstruction(last);
1221 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001222 }
1223
1224 if (return_value != nullptr) {
1225 invoke->ReplaceWith(return_value);
1226 }
1227
1228 // Update the meta information surrounding blocks:
1229 // (1) the graph they are now in,
1230 // (2) the reverse post order of that graph,
1231 // (3) the potential loop information they are now in.
1232
1233 // We don't add the entry block, the exit block, and the first block, which
1234 // has been merged with `at`.
1235 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1236
1237 // We add the `to` block.
1238 static constexpr int kNumberOfNewBlocksInCaller = 1;
1239 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
1240 + kNumberOfNewBlocksInCaller;
1241
1242 // Find the location of `at` in the outer graph's reverse post order. The new
1243 // blocks will be added after it.
1244 size_t index_of_at = 0;
1245 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
1246 index_of_at++;
1247 }
1248 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1249
1250 // Do a reverse post order of the blocks in the callee and do (1), (2),
1251 // and (3) to the blocks that apply.
1252 HLoopInformation* info = at->GetLoopInformation();
1253 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1254 HBasicBlock* current = it.Current();
1255 if (current != exit_block_ && current != entry_block_ && current != first) {
1256 DCHECK(!current->IsInLoop());
1257 DCHECK(current->GetGraph() == this);
1258 current->SetGraph(outer_graph);
1259 outer_graph->AddBlock(current);
1260 outer_graph->reverse_post_order_.Put(++index_of_at, current);
1261 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001262 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001263 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1264 loop_it.Current()->Add(current);
1265 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001266 }
1267 }
1268 }
1269
1270 // Do (1), (2), and (3) to `to`.
1271 to->SetGraph(outer_graph);
1272 outer_graph->AddBlock(to);
1273 outer_graph->reverse_post_order_.Put(++index_of_at, to);
1274 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001275 to->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001276 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1277 loop_it.Current()->Add(to);
1278 }
David Brazdil46e2a392015-03-16 17:31:52 +00001279 if (info->IsBackEdge(*at)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001280 // Only `at` can become a back edge, as the inlined blocks
1281 // are predecessors of `at`.
1282 DCHECK_EQ(1u, info->NumberOfBackEdges());
1283 info->ClearBackEdges();
1284 info->AddBackEdge(to);
1285 }
1286 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001287 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001288
David Brazdil05144f42015-04-16 15:18:00 +01001289 // Update the next instruction id of the outer graph, so that instructions
1290 // added later get bigger ids than those in the inner graph.
1291 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1292
1293 // Walk over the entry block and:
1294 // - Move constants from the entry block to the outer_graph's entry block,
1295 // - Replace HParameterValue instructions with their real value.
1296 // - Remove suspend checks, that hold an environment.
1297 // We must do this after the other blocks have been inlined, otherwise ids of
1298 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001299 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001300 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1301 HInstruction* current = it.Current();
1302 if (current->IsNullConstant()) {
1303 current->ReplaceWith(outer_graph->GetNullConstant());
1304 } else if (current->IsIntConstant()) {
1305 current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue()));
1306 } else if (current->IsLongConstant()) {
1307 current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue()));
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001308 } else if (current->IsFloatConstant()) {
1309 current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue()));
1310 } else if (current->IsDoubleConstant()) {
1311 current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue()));
David Brazdil05144f42015-04-16 15:18:00 +01001312 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001313 if (kIsDebugBuild
1314 && invoke->IsInvokeStaticOrDirect()
1315 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1316 // Ensure we do not use the last input of `invoke`, as it
1317 // contains a clinit check which is not an actual argument.
1318 size_t last_input_index = invoke->InputCount() - 1;
1319 DCHECK(parameter_index != last_input_index);
1320 }
David Brazdil05144f42015-04-16 15:18:00 +01001321 current->ReplaceWith(invoke->InputAt(parameter_index++));
1322 } else {
1323 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1324 entry_block_->RemoveInstruction(current);
1325 }
1326 }
1327
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001328 // Finally remove the invoke from the caller.
1329 invoke->GetBlock()->RemoveInstruction(invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001330}
1331
Calin Juravleacf735c2015-02-12 15:25:22 +00001332std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1333 ScopedObjectAccess soa(Thread::Current());
1334 os << "["
1335 << " is_top=" << rhs.IsTop()
1336 << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
1337 << " is_exact=" << rhs.IsExact()
1338 << " ]";
1339 return os;
1340}
1341
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001342} // namespace art