blob: df3b1fc68163f53cf93a3f12af711879c9bfa23b [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 +0000306template <class InstructionType, typename ValueType>
307InstructionType* HGraph::CreateConstant(ValueType value,
308 ArenaSafeMap<ValueType, InstructionType*>* cache) {
309 // Try to find an existing constant of the given value.
310 InstructionType* constant = nullptr;
311 auto cached_constant = cache->find(value);
312 if (cached_constant != cache->end()) {
313 constant = cached_constant->second;
David Brazdil46e2a392015-03-16 17:31:52 +0000314 }
David Brazdil8d5b8b22015-03-24 10:51:52 +0000315
316 // If not found or previously deleted, create and cache a new instruction.
317 if (constant == nullptr || constant->GetBlock() == nullptr) {
318 constant = new (arena_) InstructionType(value);
319 cache->Overwrite(value, constant);
320 InsertConstant(constant);
321 }
322 return constant;
David Brazdil46e2a392015-03-16 17:31:52 +0000323}
324
David Brazdil8d5b8b22015-03-24 10:51:52 +0000325HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value) {
326 switch (type) {
327 case Primitive::Type::kPrimBoolean:
328 DCHECK(IsUint<1>(value));
329 FALLTHROUGH_INTENDED;
330 case Primitive::Type::kPrimByte:
331 case Primitive::Type::kPrimChar:
332 case Primitive::Type::kPrimShort:
333 case Primitive::Type::kPrimInt:
334 DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value));
335 return GetIntConstant(static_cast<int32_t>(value));
336
337 case Primitive::Type::kPrimLong:
338 return GetLongConstant(value);
339
340 default:
341 LOG(FATAL) << "Unsupported constant type";
342 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000343 }
David Brazdil46e2a392015-03-16 17:31:52 +0000344}
345
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000346void HLoopInformation::Add(HBasicBlock* block) {
347 blocks_.SetBit(block->GetBlockId());
348}
349
David Brazdil46e2a392015-03-16 17:31:52 +0000350void HLoopInformation::Remove(HBasicBlock* block) {
351 blocks_.ClearBit(block->GetBlockId());
352}
353
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100354void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
355 if (blocks_.IsBitSet(block->GetBlockId())) {
356 return;
357 }
358
359 blocks_.SetBit(block->GetBlockId());
360 block->SetInLoop(this);
361 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
362 PopulateRecursive(block->GetPredecessors().Get(i));
363 }
364}
365
366bool HLoopInformation::Populate() {
367 DCHECK_EQ(GetBackEdges().Size(), 1u);
368 HBasicBlock* back_edge = GetBackEdges().Get(0);
369 DCHECK(back_edge->GetDominator() != nullptr);
370 if (!header_->Dominates(back_edge)) {
371 // This loop is not natural. Do not bother going further.
372 return false;
373 }
374
375 // Populate this loop: starting with the back edge, recursively add predecessors
376 // that are not already part of that loop. Set the header as part of the loop
377 // to end the recursion.
378 // This is a recursive implementation of the algorithm described in
379 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
380 blocks_.SetBit(header_->GetBlockId());
381 PopulateRecursive(back_edge);
382 return true;
383}
384
385HBasicBlock* HLoopInformation::GetPreHeader() const {
386 DCHECK_EQ(header_->GetPredecessors().Size(), 2u);
387 return header_->GetDominator();
388}
389
390bool HLoopInformation::Contains(const HBasicBlock& block) const {
391 return blocks_.IsBitSet(block.GetBlockId());
392}
393
394bool HLoopInformation::IsIn(const HLoopInformation& other) const {
395 return other.blocks_.IsBitSet(header_->GetBlockId());
396}
397
398bool HBasicBlock::Dominates(HBasicBlock* other) const {
399 // Walk up the dominator tree from `other`, to find out if `this`
400 // is an ancestor.
401 HBasicBlock* current = other;
402 while (current != nullptr) {
403 if (current == this) {
404 return true;
405 }
406 current = current->GetDominator();
407 }
408 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100409}
410
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100411static void UpdateInputsUsers(HInstruction* instruction) {
412 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
413 instruction->InputAt(i)->AddUseAt(instruction, i);
414 }
415 // Environment should be created later.
416 DCHECK(!instruction->HasEnvironment());
417}
418
Roland Levillainccc07a92014-09-16 14:48:16 +0100419void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
420 HInstruction* replacement) {
421 DCHECK(initial->GetBlock() == this);
422 InsertInstructionBefore(replacement, initial);
423 initial->ReplaceWith(replacement);
424 RemoveInstruction(initial);
425}
426
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100427static void Add(HInstructionList* instruction_list,
428 HBasicBlock* block,
429 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000430 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000431 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100432 instruction->SetBlock(block);
433 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100434 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100435 instruction_list->AddInstruction(instruction);
436}
437
438void HBasicBlock::AddInstruction(HInstruction* instruction) {
439 Add(&instructions_, this, instruction);
440}
441
442void HBasicBlock::AddPhi(HPhi* phi) {
443 Add(&phis_, this, phi);
444}
445
David Brazdilc3d743f2015-04-22 13:40:50 +0100446void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
447 DCHECK(!cursor->IsPhi());
448 DCHECK(!instruction->IsPhi());
449 DCHECK_EQ(instruction->GetId(), -1);
450 DCHECK_NE(cursor->GetId(), -1);
451 DCHECK_EQ(cursor->GetBlock(), this);
452 DCHECK(!instruction->IsControlFlow());
453 instruction->SetBlock(this);
454 instruction->SetId(GetGraph()->GetNextInstructionId());
455 UpdateInputsUsers(instruction);
456 instructions_.InsertInstructionBefore(instruction, cursor);
457}
458
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100459void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
460 DCHECK_EQ(phi->GetId(), -1);
461 DCHECK_NE(cursor->GetId(), -1);
462 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100463 phi->SetBlock(this);
464 phi->SetId(GetGraph()->GetNextInstructionId());
465 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100466 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100467}
468
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100469static void Remove(HInstructionList* instruction_list,
470 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000471 HInstruction* instruction,
472 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100473 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100474 instruction->SetBlock(nullptr);
475 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000476 if (ensure_safety) {
477 DCHECK(instruction->GetUses().IsEmpty());
478 DCHECK(instruction->GetEnvUses().IsEmpty());
479 RemoveAsUser(instruction);
480 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100481}
482
David Brazdil1abb4192015-02-17 18:33:36 +0000483void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100484 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000485 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100486}
487
David Brazdil1abb4192015-02-17 18:33:36 +0000488void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
489 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100490}
491
David Brazdilc7508e92015-04-27 13:28:57 +0100492void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
493 if (instruction->IsPhi()) {
494 RemovePhi(instruction->AsPhi(), ensure_safety);
495 } else {
496 RemoveInstruction(instruction, ensure_safety);
497 }
498}
499
David Brazdiled596192015-01-23 10:39:45 +0000500void HEnvironment::CopyFrom(HEnvironment* env) {
501 for (size_t i = 0; i < env->Size(); i++) {
502 HInstruction* instruction = env->GetInstructionAt(i);
503 SetRawEnvAt(i, instruction);
504 if (instruction != nullptr) {
505 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100506 }
David Brazdiled596192015-01-23 10:39:45 +0000507 }
508}
509
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700510void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
511 HBasicBlock* loop_header) {
512 DCHECK(loop_header->IsLoopHeader());
513 for (size_t i = 0; i < env->Size(); i++) {
514 HInstruction* instruction = env->GetInstructionAt(i);
515 SetRawEnvAt(i, instruction);
516 if (instruction == nullptr) {
517 continue;
518 }
519 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
520 // At the end of the loop pre-header, the corresponding value for instruction
521 // is the first input of the phi.
522 HInstruction* initial = instruction->AsPhi()->InputAt(0);
523 DCHECK(initial->GetBlock()->Dominates(loop_header));
524 SetRawEnvAt(i, initial);
525 initial->AddEnvUseAt(this, i);
526 } else {
527 instruction->AddEnvUseAt(this, i);
528 }
529 }
530}
531
David Brazdil1abb4192015-02-17 18:33:36 +0000532void HEnvironment::RemoveAsUserOfInput(size_t index) const {
533 const HUserRecord<HEnvironment*> user_record = vregs_.Get(index);
534 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100535}
536
Calin Juravle77520bc2015-01-12 18:45:46 +0000537HInstruction* HInstruction::GetNextDisregardingMoves() const {
538 HInstruction* next = GetNext();
539 while (next != nullptr && next->IsParallelMove()) {
540 next = next->GetNext();
541 }
542 return next;
543}
544
545HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
546 HInstruction* previous = GetPrevious();
547 while (previous != nullptr && previous->IsParallelMove()) {
548 previous = previous->GetPrevious();
549 }
550 return previous;
551}
552
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100553void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000554 if (first_instruction_ == nullptr) {
555 DCHECK(last_instruction_ == nullptr);
556 first_instruction_ = last_instruction_ = instruction;
557 } else {
558 last_instruction_->next_ = instruction;
559 instruction->previous_ = last_instruction_;
560 last_instruction_ = instruction;
561 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000562}
563
David Brazdilc3d743f2015-04-22 13:40:50 +0100564void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
565 DCHECK(Contains(cursor));
566 if (cursor == first_instruction_) {
567 cursor->previous_ = instruction;
568 instruction->next_ = cursor;
569 first_instruction_ = instruction;
570 } else {
571 instruction->previous_ = cursor->previous_;
572 instruction->next_ = cursor;
573 cursor->previous_ = instruction;
574 instruction->previous_->next_ = instruction;
575 }
576}
577
578void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
579 DCHECK(Contains(cursor));
580 if (cursor == last_instruction_) {
581 cursor->next_ = instruction;
582 instruction->previous_ = cursor;
583 last_instruction_ = instruction;
584 } else {
585 instruction->next_ = cursor->next_;
586 instruction->previous_ = cursor;
587 cursor->next_ = instruction;
588 instruction->next_->previous_ = instruction;
589 }
590}
591
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100592void HInstructionList::RemoveInstruction(HInstruction* instruction) {
593 if (instruction->previous_ != nullptr) {
594 instruction->previous_->next_ = instruction->next_;
595 }
596 if (instruction->next_ != nullptr) {
597 instruction->next_->previous_ = instruction->previous_;
598 }
599 if (instruction == first_instruction_) {
600 first_instruction_ = instruction->next_;
601 }
602 if (instruction == last_instruction_) {
603 last_instruction_ = instruction->previous_;
604 }
605}
606
Roland Levillain6b469232014-09-25 10:10:38 +0100607bool HInstructionList::Contains(HInstruction* instruction) const {
608 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
609 if (it.Current() == instruction) {
610 return true;
611 }
612 }
613 return false;
614}
615
Roland Levillainccc07a92014-09-16 14:48:16 +0100616bool HInstructionList::FoundBefore(const HInstruction* instruction1,
617 const HInstruction* instruction2) const {
618 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
619 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
620 if (it.Current() == instruction1) {
621 return true;
622 }
623 if (it.Current() == instruction2) {
624 return false;
625 }
626 }
627 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
628 return true;
629}
630
Roland Levillain6c82d402014-10-13 16:10:27 +0100631bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
632 if (other_instruction == this) {
633 // An instruction does not strictly dominate itself.
634 return false;
635 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100636 HBasicBlock* block = GetBlock();
637 HBasicBlock* other_block = other_instruction->GetBlock();
638 if (block != other_block) {
639 return GetBlock()->Dominates(other_instruction->GetBlock());
640 } else {
641 // If both instructions are in the same block, ensure this
642 // instruction comes before `other_instruction`.
643 if (IsPhi()) {
644 if (!other_instruction->IsPhi()) {
645 // Phis appear before non phi-instructions so this instruction
646 // dominates `other_instruction`.
647 return true;
648 } else {
649 // There is no order among phis.
650 LOG(FATAL) << "There is no dominance between phis of a same block.";
651 return false;
652 }
653 } else {
654 // `this` is not a phi.
655 if (other_instruction->IsPhi()) {
656 // Phis appear before non phi-instructions so this instruction
657 // does not dominate `other_instruction`.
658 return false;
659 } else {
660 // Check whether this instruction comes before
661 // `other_instruction` in the instruction list.
662 return block->GetInstructions().FoundBefore(this, other_instruction);
663 }
664 }
665 }
666}
667
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100668void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100669 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000670 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
671 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100672 HInstruction* user = current->GetUser();
673 size_t input_index = current->GetIndex();
674 user->SetRawInputAt(input_index, other);
675 other->AddUseAt(user, input_index);
676 }
677
David Brazdiled596192015-01-23 10:39:45 +0000678 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
679 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100680 HEnvironment* user = current->GetUser();
681 size_t input_index = current->GetIndex();
682 user->SetRawEnvAt(input_index, other);
683 other->AddEnvUseAt(user, input_index);
684 }
685
David Brazdiled596192015-01-23 10:39:45 +0000686 uses_.Clear();
687 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100688}
689
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100690void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000691 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100692 SetRawInputAt(index, replacement);
693 replacement->AddUseAt(this, index);
694}
695
Nicolas Geoffray39468442014-09-02 15:17:15 +0100696size_t HInstruction::EnvironmentSize() const {
697 return HasEnvironment() ? environment_->Size() : 0;
698}
699
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100700void HPhi::AddInput(HInstruction* input) {
701 DCHECK(input->GetBlock() != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +0000702 inputs_.Add(HUserRecord<HInstruction*>(input));
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100703 input->AddUseAt(this, inputs_.Size() - 1);
704}
705
David Brazdil2d7352b2015-04-20 14:52:42 +0100706void HPhi::RemoveInputAt(size_t index) {
707 RemoveAsUserOfInput(index);
708 inputs_.DeleteAt(index);
709}
710
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100711#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000712void H##name::Accept(HGraphVisitor* visitor) { \
713 visitor->Visit##name(this); \
714}
715
716FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
717
718#undef DEFINE_ACCEPT
719
720void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100721 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
722 for (size_t i = 0 ; i < blocks.Size(); i++) {
David Brazdil46e2a392015-03-16 17:31:52 +0000723 HBasicBlock* block = blocks.Get(i);
724 if (block != nullptr) {
725 VisitBasicBlock(block);
726 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000727 }
728}
729
Roland Levillain633021e2014-10-01 14:12:25 +0100730void HGraphVisitor::VisitReversePostOrder() {
731 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
732 VisitBasicBlock(it.Current());
733 }
734}
735
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000736void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100737 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100738 it.Current()->Accept(this);
739 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100740 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000741 it.Current()->Accept(this);
742 }
743}
744
Roland Levillain9240d6a2014-10-20 16:47:04 +0100745HConstant* HUnaryOperation::TryStaticEvaluation() const {
746 if (GetInput()->IsIntConstant()) {
747 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000748 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain9240d6a2014-10-20 16:47:04 +0100749 } else if (GetInput()->IsLongConstant()) {
Roland Levillainb762d2e2014-10-22 10:11:06 +0100750 // TODO: Implement static evaluation of long unary operations.
751 //
752 // Do not exit with a fatal condition here. Instead, simply
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700753 // return `null' to notify the caller that this instruction
Roland Levillainb762d2e2014-10-22 10:11:06 +0100754 // cannot (yet) be statically evaluated.
Roland Levillain9240d6a2014-10-20 16:47:04 +0100755 return nullptr;
756 }
757 return nullptr;
758}
759
760HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +0100761 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
762 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
763 GetRight()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000764 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100765 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
766 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
767 GetRight()->AsLongConstant()->GetValue());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000768 if (GetResultType() == Primitive::kPrimLong) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000769 return GetBlock()->GetGraph()->GetLongConstant(value);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000770 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000771 DCHECK_EQ(GetResultType(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000772 return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000773 }
Roland Levillain556c3d12014-09-18 15:25:07 +0100774 }
775 return nullptr;
776}
Dave Allison20dfc792014-06-16 20:44:29 -0700777
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000778HConstant* HBinaryOperation::GetConstantRight() const {
779 if (GetRight()->IsConstant()) {
780 return GetRight()->AsConstant();
781 } else if (IsCommutative() && GetLeft()->IsConstant()) {
782 return GetLeft()->AsConstant();
783 } else {
784 return nullptr;
785 }
786}
787
788// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700789// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000790HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
791 HInstruction* most_constant_right = GetConstantRight();
792 if (most_constant_right == nullptr) {
793 return nullptr;
794 } else if (most_constant_right == GetLeft()) {
795 return GetRight();
796 } else {
797 return GetLeft();
798 }
799}
800
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700801bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
802 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100803}
804
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100805bool HInstruction::Equals(HInstruction* other) const {
806 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100807 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100808 if (!InstructionDataEquals(other)) return false;
809 if (GetType() != other->GetType()) return false;
810 if (InputCount() != other->InputCount()) return false;
811
812 for (size_t i = 0, e = InputCount(); i < e; ++i) {
813 if (InputAt(i) != other->InputAt(i)) return false;
814 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100815 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100816 return true;
817}
818
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700819std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
820#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
821 switch (rhs) {
822 FOR_EACH_INSTRUCTION(DECLARE_CASE)
823 default:
824 os << "Unknown instruction kind " << static_cast<int>(rhs);
825 break;
826 }
827#undef DECLARE_CASE
828 return os;
829}
830
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000831void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000832 next_->previous_ = previous_;
833 if (previous_ != nullptr) {
834 previous_->next_ = next_;
835 }
836 if (block_->instructions_.first_instruction_ == this) {
837 block_->instructions_.first_instruction_ = next_;
838 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000839 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000840
841 previous_ = cursor->previous_;
842 if (previous_ != nullptr) {
843 previous_->next_ = this;
844 }
845 next_ = cursor;
846 cursor->previous_ = this;
847 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000848
849 if (block_->instructions_.first_instruction_ == cursor) {
850 block_->instructions_.first_instruction_ = this;
851 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000852}
853
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000854HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
855 DCHECK(!cursor->IsControlFlow());
856 DCHECK_NE(instructions_.last_instruction_, cursor);
857 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000858
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000859 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
860 new_block->instructions_.first_instruction_ = cursor->GetNext();
861 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
862 cursor->next_->previous_ = nullptr;
863 cursor->next_ = nullptr;
864 instructions_.last_instruction_ = cursor;
865
866 new_block->instructions_.SetBlockOfInstructions(new_block);
867 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
868 HBasicBlock* successor = GetSuccessors().Get(i);
869 new_block->successors_.Add(successor);
870 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
871 }
872 successors_.Reset();
873
874 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
875 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
876 dominated->dominator_ = new_block;
877 new_block->dominated_blocks_.Add(dominated);
878 }
879 dominated_blocks_.Reset();
880 return new_block;
881}
882
David Brazdil46e2a392015-03-16 17:31:52 +0000883bool HBasicBlock::IsSingleGoto() const {
884 HLoopInformation* loop_info = GetLoopInformation();
885 // TODO: Remove the null check b/19084197.
886 return GetFirstInstruction() != nullptr
887 && GetPhis().IsEmpty()
888 && GetFirstInstruction() == GetLastInstruction()
889 && GetLastInstruction()->IsGoto()
890 // Back edges generate the suspend check.
891 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
892}
893
David Brazdil8d5b8b22015-03-24 10:51:52 +0000894bool HBasicBlock::EndsWithControlFlowInstruction() const {
895 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
896}
897
David Brazdilb2bd1c52015-03-25 11:17:37 +0000898bool HBasicBlock::EndsWithIf() const {
899 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
900}
901
902bool HBasicBlock::HasSinglePhi() const {
903 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
904}
905
David Brazdil2d7352b2015-04-20 14:52:42 +0100906size_t HInstructionList::CountSize() const {
907 size_t size = 0;
908 HInstruction* current = first_instruction_;
909 for (; current != nullptr; current = current->GetNext()) {
910 size++;
911 }
912 return size;
913}
914
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000915void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
916 for (HInstruction* current = first_instruction_;
917 current != nullptr;
918 current = current->GetNext()) {
919 current->SetBlock(block);
920 }
921}
922
923void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
924 DCHECK(Contains(cursor));
925 if (!instruction_list.IsEmpty()) {
926 if (cursor == last_instruction_) {
927 last_instruction_ = instruction_list.last_instruction_;
928 } else {
929 cursor->next_->previous_ = instruction_list.last_instruction_;
930 }
931 instruction_list.last_instruction_->next_ = cursor->next_;
932 cursor->next_ = instruction_list.first_instruction_;
933 instruction_list.first_instruction_->previous_ = cursor;
934 }
935}
936
937void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +0000938 if (IsEmpty()) {
939 first_instruction_ = instruction_list.first_instruction_;
940 last_instruction_ = instruction_list.last_instruction_;
941 } else {
942 AddAfter(last_instruction_, instruction_list);
943 }
944}
945
David Brazdil2d7352b2015-04-20 14:52:42 +0100946void HBasicBlock::DisconnectAndDelete() {
947 // Dominators must be removed after all the blocks they dominate. This way
948 // a loop header is removed last, a requirement for correct loop information
949 // iteration.
950 DCHECK(dominated_blocks_.IsEmpty());
David Brazdil46e2a392015-03-16 17:31:52 +0000951
David Brazdil2d7352b2015-04-20 14:52:42 +0100952 // Remove the block from all loops it is included in.
953 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
954 HLoopInformation* loop_info = it.Current();
955 loop_info->Remove(this);
956 if (loop_info->IsBackEdge(*this)) {
957 // This deliberately leaves the loop in an inconsistent state and will
958 // fail SSAChecker unless the entire loop is removed during the pass.
959 loop_info->RemoveBackEdge(this);
960 }
961 }
962
963 // Disconnect the block from its predecessors and update their control-flow
964 // instructions.
David Brazdil46e2a392015-03-16 17:31:52 +0000965 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
David Brazdil2d7352b2015-04-20 14:52:42 +0100966 HBasicBlock* predecessor = predecessors_.Get(i);
967 HInstruction* last_instruction = predecessor->GetLastInstruction();
968 predecessor->RemoveInstruction(last_instruction);
969 predecessor->RemoveSuccessor(this);
970 if (predecessor->GetSuccessors().Size() == 1u) {
971 DCHECK(last_instruction->IsIf());
972 predecessor->AddInstruction(new (graph_->GetArena()) HGoto());
973 } else {
974 // The predecessor has no remaining successors and therefore must be dead.
975 // We deliberately leave it without a control-flow instruction so that the
976 // SSAChecker fails unless it is not removed during the pass too.
977 DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u);
978 }
David Brazdil46e2a392015-03-16 17:31:52 +0000979 }
David Brazdil46e2a392015-03-16 17:31:52 +0000980 predecessors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +0100981
982 // Disconnect the block from its successors and update their dominators
983 // and phis.
984 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
985 HBasicBlock* successor = successors_.Get(i);
986 // Delete this block from the list of predecessors.
987 size_t this_index = successor->GetPredecessorIndexOf(this);
988 successor->predecessors_.DeleteAt(this_index);
989
990 // Check that `successor` has other predecessors, otherwise `this` is the
991 // dominator of `successor` which violates the order DCHECKed at the top.
992 DCHECK(!successor->predecessors_.IsEmpty());
993
994 // Recompute the successor's dominator.
995 HBasicBlock* old_dominator = successor->GetDominator();
996 HBasicBlock* new_dominator = successor->predecessors_.Get(0);
997 for (size_t j = 1, f = successor->predecessors_.Size(); j < f; ++j) {
998 new_dominator = graph_->FindCommonDominator(
999 new_dominator, successor->predecessors_.Get(j));
1000 }
1001 if (old_dominator != new_dominator) {
1002 successor->SetDominator(new_dominator);
1003 old_dominator->RemoveDominatedBlock(successor);
1004 new_dominator->AddDominatedBlock(successor);
1005 }
1006
1007 // Remove this block's entries in the successor's phis.
1008 if (successor->predecessors_.Size() == 1u) {
1009 // The successor has just one predecessor left. Replace phis with the only
1010 // remaining input.
1011 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1012 HPhi* phi = phi_it.Current()->AsPhi();
1013 phi->ReplaceWith(phi->InputAt(1 - this_index));
1014 successor->RemovePhi(phi);
1015 }
1016 } else {
1017 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1018 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1019 }
1020 }
1021 }
David Brazdil46e2a392015-03-16 17:31:52 +00001022 successors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001023
1024 // Disconnect from the dominator.
1025 dominator_->RemoveDominatedBlock(this);
1026 SetDominator(nullptr);
1027
1028 // Delete from the graph. The function safely deletes remaining instructions
1029 // and updates the reverse post order.
1030 graph_->DeleteDeadBlock(this);
1031 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001032}
1033
1034void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001035 DCHECK_EQ(GetGraph(), other->GetGraph());
1036 DCHECK(GetDominatedBlocks().Contains(other));
1037 DCHECK_EQ(GetSuccessors().Size(), 1u);
1038 DCHECK_EQ(GetSuccessors().Get(0), other);
1039 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1040 DCHECK_EQ(other->GetPredecessors().Get(0), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001041 DCHECK(other->GetPhis().IsEmpty());
1042
David Brazdil2d7352b2015-04-20 14:52:42 +01001043 // Move instructions from `other` to `this`.
1044 DCHECK(EndsWithControlFlowInstruction());
1045 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001046 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001047 other->instructions_.SetBlockOfInstructions(this);
1048 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001049
David Brazdil2d7352b2015-04-20 14:52:42 +01001050 // Remove `other` from the loops it is included in.
1051 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1052 HLoopInformation* loop_info = it.Current();
1053 loop_info->Remove(other);
1054 if (loop_info->IsBackEdge(*other)) {
1055 loop_info->ClearBackEdges();
1056 loop_info->AddBackEdge(this);
1057 }
1058 }
1059
1060 // Update links to the successors of `other`.
1061 successors_.Reset();
1062 while (!other->successors_.IsEmpty()) {
1063 HBasicBlock* successor = other->successors_.Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001064 successor->ReplacePredecessor(other, this);
1065 }
1066
David Brazdil2d7352b2015-04-20 14:52:42 +01001067 // Update the dominator tree.
1068 dominated_blocks_.Delete(other);
1069 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1070 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1071 dominated_blocks_.Add(dominated);
1072 dominated->SetDominator(this);
1073 }
1074 other->dominated_blocks_.Reset();
1075 other->dominator_ = nullptr;
1076
1077 // Clear the list of predecessors of `other` in preparation of deleting it.
1078 other->predecessors_.Reset();
1079
1080 // Delete `other` from the graph. The function updates reverse post order.
1081 graph_->DeleteDeadBlock(other);
1082 other->SetGraph(nullptr);
1083}
1084
1085void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1086 DCHECK_NE(GetGraph(), other->GetGraph());
1087 DCHECK(GetDominatedBlocks().IsEmpty());
1088 DCHECK(GetSuccessors().IsEmpty());
1089 DCHECK(!EndsWithControlFlowInstruction());
1090 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1091 DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock());
1092 DCHECK(other->GetPhis().IsEmpty());
1093 DCHECK(!other->IsInLoop());
1094
1095 // Move instructions from `other` to `this`.
1096 instructions_.Add(other->GetInstructions());
1097 other->instructions_.SetBlockOfInstructions(this);
1098
1099 // Update links to the successors of `other`.
1100 successors_.Reset();
1101 while (!other->successors_.IsEmpty()) {
1102 HBasicBlock* successor = other->successors_.Get(0);
1103 successor->ReplacePredecessor(other, this);
1104 }
1105
1106 // Update the dominator tree.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001107 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1108 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1109 dominated_blocks_.Add(dominated);
1110 dominated->SetDominator(this);
1111 }
1112 other->dominated_blocks_.Reset();
1113 other->dominator_ = nullptr;
1114 other->graph_ = nullptr;
1115}
1116
1117void HBasicBlock::ReplaceWith(HBasicBlock* other) {
1118 while (!GetPredecessors().IsEmpty()) {
1119 HBasicBlock* predecessor = GetPredecessors().Get(0);
1120 predecessor->ReplaceSuccessor(this, other);
1121 }
1122 while (!GetSuccessors().IsEmpty()) {
1123 HBasicBlock* successor = GetSuccessors().Get(0);
1124 successor->ReplacePredecessor(this, other);
1125 }
1126 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
1127 other->AddDominatedBlock(dominated_blocks_.Get(i));
1128 }
1129 GetDominator()->ReplaceDominatedBlock(this, other);
1130 other->SetDominator(GetDominator());
1131 dominator_ = nullptr;
1132 graph_ = nullptr;
1133}
1134
1135// Create space in `blocks` for adding `number_of_new_blocks` entries
1136// starting at location `at`. Blocks after `at` are moved accordingly.
1137static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
1138 size_t number_of_new_blocks,
1139 size_t at) {
1140 size_t old_size = blocks->Size();
1141 size_t new_size = old_size + number_of_new_blocks;
1142 blocks->SetSize(new_size);
1143 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
1144 blocks->Put(j, blocks->Get(i));
1145 }
1146}
1147
David Brazdil2d7352b2015-04-20 14:52:42 +01001148void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1149 DCHECK_EQ(block->GetGraph(), this);
1150 DCHECK(block->GetSuccessors().IsEmpty());
1151 DCHECK(block->GetPredecessors().IsEmpty());
1152 DCHECK(block->GetDominatedBlocks().IsEmpty());
1153 DCHECK(block->GetDominator() == nullptr);
1154
1155 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1156 block->RemoveInstruction(it.Current());
1157 }
1158 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1159 block->RemovePhi(it.Current()->AsPhi());
1160 }
1161
1162 reverse_post_order_.Delete(block);
1163 blocks_.Put(block->GetBlockId(), nullptr);
1164}
1165
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001166void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001167 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001168 // Simple case of an entry block, a body block, and an exit block.
1169 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001170 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001171 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
1172 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001173 DCHECK(!body->IsExitBlock());
1174 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001175
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001176 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1177 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001178
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001179 // Replace the invoke with the return value of the inlined graph.
1180 if (last->IsReturn()) {
1181 invoke->ReplaceWith(last->InputAt(0));
1182 } else {
1183 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001184 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001185
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001186 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001187 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001188 // Need to inline multiple blocks. We split `invoke`'s block
1189 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001190 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001191 // with the second half.
1192 ArenaAllocator* allocator = outer_graph->GetArena();
1193 HBasicBlock* at = invoke->GetBlock();
1194 HBasicBlock* to = at->SplitAfter(invoke);
1195
1196 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
1197 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001198 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001199 exit_block_->ReplaceWith(to);
1200
1201 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001202 // to not `HReturn` but `HGoto` instead.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001203 HInstruction* return_value = nullptr;
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001204 bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
1205 if (to->GetPredecessors().Size() == 1) {
1206 HBasicBlock* predecessor = to->GetPredecessors().Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001207 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001208 if (!returns_void) {
1209 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001210 }
1211 predecessor->AddInstruction(new (allocator) HGoto());
1212 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001213 } else {
1214 if (!returns_void) {
1215 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001216 return_value = new (allocator) HPhi(
1217 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001218 to->AddPhi(return_value->AsPhi());
1219 }
1220 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
1221 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
1222 HInstruction* last = predecessor->GetLastInstruction();
1223 if (!returns_void) {
1224 return_value->AsPhi()->AddInput(last->InputAt(0));
1225 }
1226 predecessor->AddInstruction(new (allocator) HGoto());
1227 predecessor->RemoveInstruction(last);
1228 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001229 }
1230
1231 if (return_value != nullptr) {
1232 invoke->ReplaceWith(return_value);
1233 }
1234
1235 // Update the meta information surrounding blocks:
1236 // (1) the graph they are now in,
1237 // (2) the reverse post order of that graph,
1238 // (3) the potential loop information they are now in.
1239
1240 // We don't add the entry block, the exit block, and the first block, which
1241 // has been merged with `at`.
1242 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1243
1244 // We add the `to` block.
1245 static constexpr int kNumberOfNewBlocksInCaller = 1;
1246 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
1247 + kNumberOfNewBlocksInCaller;
1248
1249 // Find the location of `at` in the outer graph's reverse post order. The new
1250 // blocks will be added after it.
1251 size_t index_of_at = 0;
1252 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
1253 index_of_at++;
1254 }
1255 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1256
1257 // Do a reverse post order of the blocks in the callee and do (1), (2),
1258 // and (3) to the blocks that apply.
1259 HLoopInformation* info = at->GetLoopInformation();
1260 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1261 HBasicBlock* current = it.Current();
1262 if (current != exit_block_ && current != entry_block_ && current != first) {
1263 DCHECK(!current->IsInLoop());
1264 DCHECK(current->GetGraph() == this);
1265 current->SetGraph(outer_graph);
1266 outer_graph->AddBlock(current);
1267 outer_graph->reverse_post_order_.Put(++index_of_at, current);
1268 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001269 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001270 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1271 loop_it.Current()->Add(current);
1272 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001273 }
1274 }
1275 }
1276
1277 // Do (1), (2), and (3) to `to`.
1278 to->SetGraph(outer_graph);
1279 outer_graph->AddBlock(to);
1280 outer_graph->reverse_post_order_.Put(++index_of_at, to);
1281 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001282 to->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001283 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1284 loop_it.Current()->Add(to);
1285 }
David Brazdil46e2a392015-03-16 17:31:52 +00001286 if (info->IsBackEdge(*at)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001287 // Only `at` can become a back edge, as the inlined blocks
1288 // are predecessors of `at`.
1289 DCHECK_EQ(1u, info->NumberOfBackEdges());
1290 info->ClearBackEdges();
1291 info->AddBackEdge(to);
1292 }
1293 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001294 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001295
David Brazdil05144f42015-04-16 15:18:00 +01001296 // Update the next instruction id of the outer graph, so that instructions
1297 // added later get bigger ids than those in the inner graph.
1298 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1299
1300 // Walk over the entry block and:
1301 // - Move constants from the entry block to the outer_graph's entry block,
1302 // - Replace HParameterValue instructions with their real value.
1303 // - Remove suspend checks, that hold an environment.
1304 // We must do this after the other blocks have been inlined, otherwise ids of
1305 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001306 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001307 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1308 HInstruction* current = it.Current();
1309 if (current->IsNullConstant()) {
1310 current->ReplaceWith(outer_graph->GetNullConstant());
1311 } else if (current->IsIntConstant()) {
1312 current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue()));
1313 } else if (current->IsLongConstant()) {
1314 current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue()));
1315 } else if (current->IsFloatConstant() || current->IsDoubleConstant()) {
1316 // TODO: Don't duplicate floating-point constants.
1317 current->MoveBefore(outer_graph->GetEntryBlock()->GetLastInstruction());
1318 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001319 if (kIsDebugBuild
1320 && invoke->IsInvokeStaticOrDirect()
1321 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1322 // Ensure we do not use the last input of `invoke`, as it
1323 // contains a clinit check which is not an actual argument.
1324 size_t last_input_index = invoke->InputCount() - 1;
1325 DCHECK(parameter_index != last_input_index);
1326 }
David Brazdil05144f42015-04-16 15:18:00 +01001327 current->ReplaceWith(invoke->InputAt(parameter_index++));
1328 } else {
1329 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1330 entry_block_->RemoveInstruction(current);
1331 }
1332 }
1333
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001334 // Finally remove the invoke from the caller.
1335 invoke->GetBlock()->RemoveInstruction(invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001336}
1337
Calin Juravleacf735c2015-02-12 15:25:22 +00001338std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1339 ScopedObjectAccess soa(Thread::Current());
1340 os << "["
1341 << " is_top=" << rhs.IsTop()
1342 << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
1343 << " is_exact=" << rhs.IsExact()
1344 << " ]";
1345 return os;
1346}
1347
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001348} // namespace art