blob: b9e58c7032038c976cba84c19978204cd44f241b [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
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010040 for (HEnvironment* environment = instruction->GetEnvironment();
41 environment != nullptr;
42 environment = environment->GetParent()) {
Roland Levillainfc600dc2014-12-02 17:16:31 +000043 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
David Brazdil1abb4192015-02-17 18:33:36 +000044 if (environment->GetInstructionAt(i) != nullptr) {
45 environment->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000046 }
47 }
48 }
49}
50
51void HGraph::RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const {
52 for (size_t i = 0; i < blocks_.Size(); ++i) {
53 if (!visited.IsBitSet(i)) {
54 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010055 DCHECK(block->GetPhis().IsEmpty()) << "Phis are not inserted at this stage";
Roland Levillainfc600dc2014-12-02 17:16:31 +000056 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
57 RemoveAsUser(it.Current());
58 }
59 }
60 }
61}
62
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010063void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010064 for (size_t i = 0; i < blocks_.Size(); ++i) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000065 if (!visited.IsBitSet(i)) {
David Brazdil1abb4192015-02-17 18:33:36 +000066 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010067 // We only need to update the successor, which might be live.
David Brazdil1abb4192015-02-17 18:33:36 +000068 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
69 block->GetSuccessors().Get(j)->RemovePredecessor(block);
70 }
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010071 // Remove the block from the list of blocks, so that further analyses
72 // never see it.
73 blocks_.Put(i, nullptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000074 }
75 }
76}
77
78void HGraph::VisitBlockForBackEdges(HBasicBlock* block,
79 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +010080 ArenaBitVector* visiting) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000081 int id = block->GetBlockId();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000082 if (visited->IsBitSet(id)) return;
83
84 visited->SetBit(id);
85 visiting->SetBit(id);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010086 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
87 HBasicBlock* successor = block->GetSuccessors().Get(i);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000088 if (visiting->IsBitSet(successor->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000089 successor->AddBackEdge(block);
90 } else {
91 VisitBlockForBackEdges(successor, visited, visiting);
92 }
93 }
94 visiting->ClearBit(id);
95}
96
97void HGraph::BuildDominatorTree() {
98 ArenaBitVector visited(arena_, blocks_.Size(), false);
99
100 // (1) Find the back edges in the graph doing a DFS traversal.
101 FindBackEdges(&visited);
102
Roland Levillainfc600dc2014-12-02 17:16:31 +0000103 // (2) Remove instructions and phis from blocks not visited during
104 // the initial DFS as users from other instructions, so that
105 // users can be safely removed before uses later.
106 RemoveInstructionsAsUsersFromDeadBlocks(visited);
107
108 // (3) Remove blocks not visited during the initial DFS.
109 // Step (4) requires dead blocks to be removed from the
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000110 // predecessors list of live blocks.
111 RemoveDeadBlocks(visited);
112
Roland Levillainfc600dc2014-12-02 17:16:31 +0000113 // (4) Simplify the CFG now, so that we don't need to recompute
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100114 // dominators and the reverse post order.
115 SimplifyCFG();
116
Roland Levillainfc600dc2014-12-02 17:16:31 +0000117 // (5) Compute the immediate dominator of each block. We visit
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000118 // the successors of a block only when all its forward branches
119 // have been processed.
120 GrowableArray<size_t> visits(arena_, blocks_.Size());
121 visits.SetSize(blocks_.Size());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100122 reverse_post_order_.Add(entry_block_);
123 for (size_t i = 0; i < entry_block_->GetSuccessors().Size(); i++) {
124 VisitBlockForDominatorTree(entry_block_->GetSuccessors().Get(i), entry_block_, &visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000125 }
126}
127
128HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
129 ArenaBitVector visited(arena_, blocks_.Size(), false);
130 // Walk the dominator tree of the first block and mark the visited blocks.
131 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000132 visited.SetBit(first->GetBlockId());
133 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000134 }
135 // Walk the dominator tree of the second block until a marked block is found.
136 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000137 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000138 return second;
139 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000140 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000141 }
142 LOG(ERROR) << "Could not find common dominator";
143 return nullptr;
144}
145
146void HGraph::VisitBlockForDominatorTree(HBasicBlock* block,
147 HBasicBlock* predecessor,
148 GrowableArray<size_t>* visits) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000149 if (block->GetDominator() == nullptr) {
150 block->SetDominator(predecessor);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000151 } else {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000152 block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000153 }
154
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000155 visits->Increment(block->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000156 // Once all the forward edges have been visited, we know the immediate
157 // dominator of the block. We can then start visiting its successors.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000158 if (visits->Get(block->GetBlockId()) ==
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100159 block->GetPredecessors().Size() - block->NumberOfBackEdges()) {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100160 block->GetDominator()->AddDominatedBlock(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100161 reverse_post_order_.Add(block);
162 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
163 VisitBlockForDominatorTree(block->GetSuccessors().Get(i), block, visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000164 }
165 }
166}
167
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000168void HGraph::TransformToSsa() {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100169 DCHECK(!reverse_post_order_.IsEmpty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100170 SsaBuilder ssa_builder(this);
171 ssa_builder.BuildSsa();
172}
173
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100174void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) {
175 // Insert a new node between `block` and `successor` to split the
176 // critical edge.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100177 HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100178 AddBlock(new_block);
179 new_block->AddInstruction(new (arena_) HGoto());
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100180 block->ReplaceSuccessor(successor, new_block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100181 new_block->AddSuccessor(successor);
182 if (successor->IsLoopHeader()) {
183 // If we split at a back edge boundary, make the new block the back edge.
184 HLoopInformation* info = successor->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000185 if (info->IsBackEdge(*block)) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100186 info->RemoveBackEdge(block);
187 info->AddBackEdge(new_block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100188 }
189 }
190}
191
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100192void HGraph::SimplifyLoop(HBasicBlock* header) {
193 HLoopInformation* info = header->GetLoopInformation();
194
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100195 // Make sure the loop has only one pre header. This simplifies SSA building by having
196 // to just look at the pre header to know which locals are initialized at entry of the
197 // loop.
198 size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges();
199 if (number_of_incomings != 1) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100200 HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100201 AddBlock(pre_header);
202 pre_header->AddInstruction(new (arena_) HGoto());
203
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100204 for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) {
205 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100206 if (!info->IsBackEdge(*predecessor)) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100207 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100208 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100209 }
210 }
211 pre_header->AddSuccessor(header);
212 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100213
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100214 // Make sure the first predecessor of a loop header is the incoming block.
215 if (info->IsBackEdge(*header->GetPredecessors().Get(0))) {
216 HBasicBlock* to_swap = header->GetPredecessors().Get(0);
217 for (size_t pred = 1, e = header->GetPredecessors().Size(); pred < e; ++pred) {
218 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
219 if (!info->IsBackEdge(*predecessor)) {
220 header->predecessors_.Put(pred, to_swap);
221 header->predecessors_.Put(0, predecessor);
222 break;
223 }
224 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100225 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100226
227 // Place the suspend check at the beginning of the header, so that live registers
228 // will be known when allocating registers. Note that code generation can still
229 // generate the suspend check at the back edge, but needs to be careful with
230 // loop phi spill slots (which are not written to at back edge).
231 HInstruction* first_instruction = header->GetFirstInstruction();
232 if (!first_instruction->IsSuspendCheck()) {
233 HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc());
234 header->InsertInstructionBefore(check, first_instruction);
235 first_instruction = check;
236 }
237 info->SetSuspendCheck(first_instruction->AsSuspendCheck());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238}
239
240void HGraph::SimplifyCFG() {
241 // Simplify the CFG for future analysis, and code generation:
242 // (1): Split critical edges.
243 // (2): Simplify loops by having only one back edge, and one preheader.
244 for (size_t i = 0; i < blocks_.Size(); ++i) {
245 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100246 if (block == nullptr) continue;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100247 if (block->GetSuccessors().Size() > 1) {
248 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
249 HBasicBlock* successor = block->GetSuccessors().Get(j);
250 if (successor->GetPredecessors().Size() > 1) {
251 SplitCriticalEdge(block, successor);
252 --j;
253 }
254 }
255 }
256 if (block->IsLoopHeader()) {
257 SimplifyLoop(block);
258 }
259 }
260}
261
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000262bool HGraph::AnalyzeNaturalLoops() const {
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100263 // Order does not matter.
264 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
265 HBasicBlock* block = it.Current();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100266 if (block->IsLoopHeader()) {
267 HLoopInformation* info = block->GetLoopInformation();
268 if (!info->Populate()) {
269 // Abort if the loop is non natural. We currently bailout in such cases.
270 return false;
271 }
272 }
273 }
274 return true;
275}
276
David Brazdil8d5b8b22015-03-24 10:51:52 +0000277void HGraph::InsertConstant(HConstant* constant) {
278 // New constants are inserted before the final control-flow instruction
279 // of the graph, or at its end if called from the graph builder.
280 if (entry_block_->EndsWithControlFlowInstruction()) {
281 entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction());
David Brazdil46e2a392015-03-16 17:31:52 +0000282 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000283 entry_block_->AddInstruction(constant);
David Brazdil46e2a392015-03-16 17:31:52 +0000284 }
285}
286
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000287HNullConstant* HGraph::GetNullConstant() {
288 if (cached_null_constant_ == nullptr) {
289 cached_null_constant_ = new (arena_) HNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000290 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000291 }
292 return cached_null_constant_;
293}
294
David Brazdil8d5b8b22015-03-24 10:51:52 +0000295HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value) {
296 switch (type) {
297 case Primitive::Type::kPrimBoolean:
298 DCHECK(IsUint<1>(value));
299 FALLTHROUGH_INTENDED;
300 case Primitive::Type::kPrimByte:
301 case Primitive::Type::kPrimChar:
302 case Primitive::Type::kPrimShort:
303 case Primitive::Type::kPrimInt:
304 DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value));
305 return GetIntConstant(static_cast<int32_t>(value));
306
307 case Primitive::Type::kPrimLong:
308 return GetLongConstant(value);
309
310 default:
311 LOG(FATAL) << "Unsupported constant type";
312 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000313 }
David Brazdil46e2a392015-03-16 17:31:52 +0000314}
315
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000316void HGraph::CacheFloatConstant(HFloatConstant* constant) {
317 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
318 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
319 cached_float_constants_.Overwrite(value, constant);
320}
321
322void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
323 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
324 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
325 cached_double_constants_.Overwrite(value, constant);
326}
327
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000328void HLoopInformation::Add(HBasicBlock* block) {
329 blocks_.SetBit(block->GetBlockId());
330}
331
David Brazdil46e2a392015-03-16 17:31:52 +0000332void HLoopInformation::Remove(HBasicBlock* block) {
333 blocks_.ClearBit(block->GetBlockId());
334}
335
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100336void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
337 if (blocks_.IsBitSet(block->GetBlockId())) {
338 return;
339 }
340
341 blocks_.SetBit(block->GetBlockId());
342 block->SetInLoop(this);
343 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
344 PopulateRecursive(block->GetPredecessors().Get(i));
345 }
346}
347
348bool HLoopInformation::Populate() {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100349 for (size_t i = 0, e = GetBackEdges().Size(); i < e; ++i) {
350 HBasicBlock* back_edge = GetBackEdges().Get(i);
351 DCHECK(back_edge->GetDominator() != nullptr);
352 if (!header_->Dominates(back_edge)) {
353 // This loop is not natural. Do not bother going further.
354 return false;
355 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100356
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100357 // Populate this loop: starting with the back edge, recursively add predecessors
358 // that are not already part of that loop. Set the header as part of the loop
359 // to end the recursion.
360 // This is a recursive implementation of the algorithm described in
361 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
362 blocks_.SetBit(header_->GetBlockId());
363 PopulateRecursive(back_edge);
364 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100365 return true;
366}
367
368HBasicBlock* HLoopInformation::GetPreHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100369 return header_->GetDominator();
370}
371
372bool HLoopInformation::Contains(const HBasicBlock& block) const {
373 return blocks_.IsBitSet(block.GetBlockId());
374}
375
376bool HLoopInformation::IsIn(const HLoopInformation& other) const {
377 return other.blocks_.IsBitSet(header_->GetBlockId());
378}
379
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100380size_t HLoopInformation::GetLifetimeEnd() const {
381 size_t last_position = 0;
382 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
383 last_position = std::max(back_edges_.Get(i)->GetLifetimeEnd(), last_position);
384 }
385 return last_position;
386}
387
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388bool HBasicBlock::Dominates(HBasicBlock* other) const {
389 // Walk up the dominator tree from `other`, to find out if `this`
390 // is an ancestor.
391 HBasicBlock* current = other;
392 while (current != nullptr) {
393 if (current == this) {
394 return true;
395 }
396 current = current->GetDominator();
397 }
398 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100399}
400
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100401static void UpdateInputsUsers(HInstruction* instruction) {
402 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
403 instruction->InputAt(i)->AddUseAt(instruction, i);
404 }
405 // Environment should be created later.
406 DCHECK(!instruction->HasEnvironment());
407}
408
Roland Levillainccc07a92014-09-16 14:48:16 +0100409void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
410 HInstruction* replacement) {
411 DCHECK(initial->GetBlock() == this);
412 InsertInstructionBefore(replacement, initial);
413 initial->ReplaceWith(replacement);
414 RemoveInstruction(initial);
415}
416
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100417static void Add(HInstructionList* instruction_list,
418 HBasicBlock* block,
419 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000420 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000421 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100422 instruction->SetBlock(block);
423 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100424 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100425 instruction_list->AddInstruction(instruction);
426}
427
428void HBasicBlock::AddInstruction(HInstruction* instruction) {
429 Add(&instructions_, this, instruction);
430}
431
432void HBasicBlock::AddPhi(HPhi* phi) {
433 Add(&phis_, this, phi);
434}
435
David Brazdilc3d743f2015-04-22 13:40:50 +0100436void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
437 DCHECK(!cursor->IsPhi());
438 DCHECK(!instruction->IsPhi());
439 DCHECK_EQ(instruction->GetId(), -1);
440 DCHECK_NE(cursor->GetId(), -1);
441 DCHECK_EQ(cursor->GetBlock(), this);
442 DCHECK(!instruction->IsControlFlow());
443 instruction->SetBlock(this);
444 instruction->SetId(GetGraph()->GetNextInstructionId());
445 UpdateInputsUsers(instruction);
446 instructions_.InsertInstructionBefore(instruction, cursor);
447}
448
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100449void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
450 DCHECK(!cursor->IsPhi());
451 DCHECK(!instruction->IsPhi());
452 DCHECK_EQ(instruction->GetId(), -1);
453 DCHECK_NE(cursor->GetId(), -1);
454 DCHECK_EQ(cursor->GetBlock(), this);
455 DCHECK(!instruction->IsControlFlow());
456 DCHECK(!cursor->IsControlFlow());
457 instruction->SetBlock(this);
458 instruction->SetId(GetGraph()->GetNextInstructionId());
459 UpdateInputsUsers(instruction);
460 instructions_.InsertInstructionAfter(instruction, cursor);
461}
462
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100463void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
464 DCHECK_EQ(phi->GetId(), -1);
465 DCHECK_NE(cursor->GetId(), -1);
466 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100467 phi->SetBlock(this);
468 phi->SetId(GetGraph()->GetNextInstructionId());
469 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100470 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100471}
472
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100473static void Remove(HInstructionList* instruction_list,
474 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000475 HInstruction* instruction,
476 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100477 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100478 instruction->SetBlock(nullptr);
479 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000480 if (ensure_safety) {
481 DCHECK(instruction->GetUses().IsEmpty());
482 DCHECK(instruction->GetEnvUses().IsEmpty());
483 RemoveAsUser(instruction);
484 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100485}
486
David Brazdil1abb4192015-02-17 18:33:36 +0000487void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100488 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000489 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100490}
491
David Brazdil1abb4192015-02-17 18:33:36 +0000492void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
493 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100494}
495
David Brazdilc7508e92015-04-27 13:28:57 +0100496void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
497 if (instruction->IsPhi()) {
498 RemovePhi(instruction->AsPhi(), ensure_safety);
499 } else {
500 RemoveInstruction(instruction, ensure_safety);
501 }
502}
503
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +0100504void HEnvironment::CopyFrom(const GrowableArray<HInstruction*>& locals) {
505 for (size_t i = 0; i < locals.Size(); i++) {
506 HInstruction* instruction = locals.Get(i);
507 SetRawEnvAt(i, instruction);
508 if (instruction != nullptr) {
509 instruction->AddEnvUseAt(this, i);
510 }
511 }
512}
513
David Brazdiled596192015-01-23 10:39:45 +0000514void HEnvironment::CopyFrom(HEnvironment* env) {
515 for (size_t i = 0; i < env->Size(); i++) {
516 HInstruction* instruction = env->GetInstructionAt(i);
517 SetRawEnvAt(i, instruction);
518 if (instruction != nullptr) {
519 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100520 }
David Brazdiled596192015-01-23 10:39:45 +0000521 }
522}
523
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700524void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
525 HBasicBlock* loop_header) {
526 DCHECK(loop_header->IsLoopHeader());
527 for (size_t i = 0; i < env->Size(); i++) {
528 HInstruction* instruction = env->GetInstructionAt(i);
529 SetRawEnvAt(i, instruction);
530 if (instruction == nullptr) {
531 continue;
532 }
533 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
534 // At the end of the loop pre-header, the corresponding value for instruction
535 // is the first input of the phi.
536 HInstruction* initial = instruction->AsPhi()->InputAt(0);
537 DCHECK(initial->GetBlock()->Dominates(loop_header));
538 SetRawEnvAt(i, initial);
539 initial->AddEnvUseAt(this, i);
540 } else {
541 instruction->AddEnvUseAt(this, i);
542 }
543 }
544}
545
David Brazdil1abb4192015-02-17 18:33:36 +0000546void HEnvironment::RemoveAsUserOfInput(size_t index) const {
547 const HUserRecord<HEnvironment*> user_record = vregs_.Get(index);
548 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100549}
550
Calin Juravle77520bc2015-01-12 18:45:46 +0000551HInstruction* HInstruction::GetNextDisregardingMoves() const {
552 HInstruction* next = GetNext();
553 while (next != nullptr && next->IsParallelMove()) {
554 next = next->GetNext();
555 }
556 return next;
557}
558
559HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
560 HInstruction* previous = GetPrevious();
561 while (previous != nullptr && previous->IsParallelMove()) {
562 previous = previous->GetPrevious();
563 }
564 return previous;
565}
566
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100567void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000568 if (first_instruction_ == nullptr) {
569 DCHECK(last_instruction_ == nullptr);
570 first_instruction_ = last_instruction_ = instruction;
571 } else {
572 last_instruction_->next_ = instruction;
573 instruction->previous_ = last_instruction_;
574 last_instruction_ = instruction;
575 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000576}
577
David Brazdilc3d743f2015-04-22 13:40:50 +0100578void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
579 DCHECK(Contains(cursor));
580 if (cursor == first_instruction_) {
581 cursor->previous_ = instruction;
582 instruction->next_ = cursor;
583 first_instruction_ = instruction;
584 } else {
585 instruction->previous_ = cursor->previous_;
586 instruction->next_ = cursor;
587 cursor->previous_ = instruction;
588 instruction->previous_->next_ = instruction;
589 }
590}
591
592void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
593 DCHECK(Contains(cursor));
594 if (cursor == last_instruction_) {
595 cursor->next_ = instruction;
596 instruction->previous_ = cursor;
597 last_instruction_ = instruction;
598 } else {
599 instruction->next_ = cursor->next_;
600 instruction->previous_ = cursor;
601 cursor->next_ = instruction;
602 instruction->next_->previous_ = instruction;
603 }
604}
605
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100606void HInstructionList::RemoveInstruction(HInstruction* instruction) {
607 if (instruction->previous_ != nullptr) {
608 instruction->previous_->next_ = instruction->next_;
609 }
610 if (instruction->next_ != nullptr) {
611 instruction->next_->previous_ = instruction->previous_;
612 }
613 if (instruction == first_instruction_) {
614 first_instruction_ = instruction->next_;
615 }
616 if (instruction == last_instruction_) {
617 last_instruction_ = instruction->previous_;
618 }
619}
620
Roland Levillain6b469232014-09-25 10:10:38 +0100621bool HInstructionList::Contains(HInstruction* instruction) const {
622 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
623 if (it.Current() == instruction) {
624 return true;
625 }
626 }
627 return false;
628}
629
Roland Levillainccc07a92014-09-16 14:48:16 +0100630bool HInstructionList::FoundBefore(const HInstruction* instruction1,
631 const HInstruction* instruction2) const {
632 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
633 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
634 if (it.Current() == instruction1) {
635 return true;
636 }
637 if (it.Current() == instruction2) {
638 return false;
639 }
640 }
641 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
642 return true;
643}
644
Roland Levillain6c82d402014-10-13 16:10:27 +0100645bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
646 if (other_instruction == this) {
647 // An instruction does not strictly dominate itself.
648 return false;
649 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100650 HBasicBlock* block = GetBlock();
651 HBasicBlock* other_block = other_instruction->GetBlock();
652 if (block != other_block) {
653 return GetBlock()->Dominates(other_instruction->GetBlock());
654 } else {
655 // If both instructions are in the same block, ensure this
656 // instruction comes before `other_instruction`.
657 if (IsPhi()) {
658 if (!other_instruction->IsPhi()) {
659 // Phis appear before non phi-instructions so this instruction
660 // dominates `other_instruction`.
661 return true;
662 } else {
663 // There is no order among phis.
664 LOG(FATAL) << "There is no dominance between phis of a same block.";
665 return false;
666 }
667 } else {
668 // `this` is not a phi.
669 if (other_instruction->IsPhi()) {
670 // Phis appear before non phi-instructions so this instruction
671 // does not dominate `other_instruction`.
672 return false;
673 } else {
674 // Check whether this instruction comes before
675 // `other_instruction` in the instruction list.
676 return block->GetInstructions().FoundBefore(this, other_instruction);
677 }
678 }
679 }
680}
681
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100682void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100683 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000684 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
685 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100686 HInstruction* user = current->GetUser();
687 size_t input_index = current->GetIndex();
688 user->SetRawInputAt(input_index, other);
689 other->AddUseAt(user, input_index);
690 }
691
David Brazdiled596192015-01-23 10:39:45 +0000692 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
693 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100694 HEnvironment* user = current->GetUser();
695 size_t input_index = current->GetIndex();
696 user->SetRawEnvAt(input_index, other);
697 other->AddEnvUseAt(user, input_index);
698 }
699
David Brazdiled596192015-01-23 10:39:45 +0000700 uses_.Clear();
701 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100702}
703
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100704void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000705 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100706 SetRawInputAt(index, replacement);
707 replacement->AddUseAt(this, index);
708}
709
Nicolas Geoffray39468442014-09-02 15:17:15 +0100710size_t HInstruction::EnvironmentSize() const {
711 return HasEnvironment() ? environment_->Size() : 0;
712}
713
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100714void HPhi::AddInput(HInstruction* input) {
715 DCHECK(input->GetBlock() != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +0000716 inputs_.Add(HUserRecord<HInstruction*>(input));
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100717 input->AddUseAt(this, inputs_.Size() - 1);
718}
719
David Brazdil2d7352b2015-04-20 14:52:42 +0100720void HPhi::RemoveInputAt(size_t index) {
721 RemoveAsUserOfInput(index);
722 inputs_.DeleteAt(index);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100723 for (size_t i = index, e = InputCount(); i < e; ++i) {
724 InputRecordAt(i).GetUseNode()->SetIndex(i);
725 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100726}
727
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100728#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000729void H##name::Accept(HGraphVisitor* visitor) { \
730 visitor->Visit##name(this); \
731}
732
733FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
734
735#undef DEFINE_ACCEPT
736
737void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100738 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
739 for (size_t i = 0 ; i < blocks.Size(); i++) {
David Brazdil46e2a392015-03-16 17:31:52 +0000740 HBasicBlock* block = blocks.Get(i);
741 if (block != nullptr) {
742 VisitBasicBlock(block);
743 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000744 }
745}
746
Roland Levillain633021e2014-10-01 14:12:25 +0100747void HGraphVisitor::VisitReversePostOrder() {
748 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
749 VisitBasicBlock(it.Current());
750 }
751}
752
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000753void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100754 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100755 it.Current()->Accept(this);
756 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100757 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000758 it.Current()->Accept(this);
759 }
760}
761
Roland Levillain9240d6a2014-10-20 16:47:04 +0100762HConstant* HUnaryOperation::TryStaticEvaluation() const {
763 if (GetInput()->IsIntConstant()) {
764 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000765 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain9240d6a2014-10-20 16:47:04 +0100766 } else if (GetInput()->IsLongConstant()) {
Roland Levillainb762d2e2014-10-22 10:11:06 +0100767 // TODO: Implement static evaluation of long unary operations.
768 //
769 // Do not exit with a fatal condition here. Instead, simply
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700770 // return `null' to notify the caller that this instruction
Roland Levillainb762d2e2014-10-22 10:11:06 +0100771 // cannot (yet) be statically evaluated.
Roland Levillain9240d6a2014-10-20 16:47:04 +0100772 return nullptr;
773 }
774 return nullptr;
775}
776
777HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +0100778 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
779 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
780 GetRight()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000781 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100782 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
783 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
784 GetRight()->AsLongConstant()->GetValue());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000785 if (GetResultType() == Primitive::kPrimLong) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000786 return GetBlock()->GetGraph()->GetLongConstant(value);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000787 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000788 DCHECK_EQ(GetResultType(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000789 return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000790 }
Roland Levillain556c3d12014-09-18 15:25:07 +0100791 }
792 return nullptr;
793}
Dave Allison20dfc792014-06-16 20:44:29 -0700794
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000795HConstant* HBinaryOperation::GetConstantRight() const {
796 if (GetRight()->IsConstant()) {
797 return GetRight()->AsConstant();
798 } else if (IsCommutative() && GetLeft()->IsConstant()) {
799 return GetLeft()->AsConstant();
800 } else {
801 return nullptr;
802 }
803}
804
805// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700806// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000807HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
808 HInstruction* most_constant_right = GetConstantRight();
809 if (most_constant_right == nullptr) {
810 return nullptr;
811 } else if (most_constant_right == GetLeft()) {
812 return GetRight();
813 } else {
814 return GetLeft();
815 }
816}
817
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700818bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
819 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100820}
821
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100822bool HInstruction::Equals(HInstruction* other) const {
823 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100824 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100825 if (!InstructionDataEquals(other)) return false;
826 if (GetType() != other->GetType()) return false;
827 if (InputCount() != other->InputCount()) return false;
828
829 for (size_t i = 0, e = InputCount(); i < e; ++i) {
830 if (InputAt(i) != other->InputAt(i)) return false;
831 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100832 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100833 return true;
834}
835
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700836std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
837#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
838 switch (rhs) {
839 FOR_EACH_INSTRUCTION(DECLARE_CASE)
840 default:
841 os << "Unknown instruction kind " << static_cast<int>(rhs);
842 break;
843 }
844#undef DECLARE_CASE
845 return os;
846}
847
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000848void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000849 next_->previous_ = previous_;
850 if (previous_ != nullptr) {
851 previous_->next_ = next_;
852 }
853 if (block_->instructions_.first_instruction_ == this) {
854 block_->instructions_.first_instruction_ = next_;
855 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000856 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000857
858 previous_ = cursor->previous_;
859 if (previous_ != nullptr) {
860 previous_->next_ = this;
861 }
862 next_ = cursor;
863 cursor->previous_ = this;
864 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000865
866 if (block_->instructions_.first_instruction_ == cursor) {
867 block_->instructions_.first_instruction_ = this;
868 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000869}
870
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000871HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
872 DCHECK(!cursor->IsControlFlow());
873 DCHECK_NE(instructions_.last_instruction_, cursor);
874 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000875
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000876 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
877 new_block->instructions_.first_instruction_ = cursor->GetNext();
878 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
879 cursor->next_->previous_ = nullptr;
880 cursor->next_ = nullptr;
881 instructions_.last_instruction_ = cursor;
882
883 new_block->instructions_.SetBlockOfInstructions(new_block);
884 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
885 HBasicBlock* successor = GetSuccessors().Get(i);
886 new_block->successors_.Add(successor);
887 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
888 }
889 successors_.Reset();
890
891 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
892 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
893 dominated->dominator_ = new_block;
894 new_block->dominated_blocks_.Add(dominated);
895 }
896 dominated_blocks_.Reset();
897 return new_block;
898}
899
David Brazdil46e2a392015-03-16 17:31:52 +0000900bool HBasicBlock::IsSingleGoto() const {
901 HLoopInformation* loop_info = GetLoopInformation();
902 // TODO: Remove the null check b/19084197.
903 return GetFirstInstruction() != nullptr
904 && GetPhis().IsEmpty()
905 && GetFirstInstruction() == GetLastInstruction()
906 && GetLastInstruction()->IsGoto()
907 // Back edges generate the suspend check.
908 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
909}
910
David Brazdil8d5b8b22015-03-24 10:51:52 +0000911bool HBasicBlock::EndsWithControlFlowInstruction() const {
912 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
913}
914
David Brazdilb2bd1c52015-03-25 11:17:37 +0000915bool HBasicBlock::EndsWithIf() const {
916 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
917}
918
919bool HBasicBlock::HasSinglePhi() const {
920 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
921}
922
David Brazdil2d7352b2015-04-20 14:52:42 +0100923size_t HInstructionList::CountSize() const {
924 size_t size = 0;
925 HInstruction* current = first_instruction_;
926 for (; current != nullptr; current = current->GetNext()) {
927 size++;
928 }
929 return size;
930}
931
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000932void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
933 for (HInstruction* current = first_instruction_;
934 current != nullptr;
935 current = current->GetNext()) {
936 current->SetBlock(block);
937 }
938}
939
940void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
941 DCHECK(Contains(cursor));
942 if (!instruction_list.IsEmpty()) {
943 if (cursor == last_instruction_) {
944 last_instruction_ = instruction_list.last_instruction_;
945 } else {
946 cursor->next_->previous_ = instruction_list.last_instruction_;
947 }
948 instruction_list.last_instruction_->next_ = cursor->next_;
949 cursor->next_ = instruction_list.first_instruction_;
950 instruction_list.first_instruction_->previous_ = cursor;
951 }
952}
953
954void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +0000955 if (IsEmpty()) {
956 first_instruction_ = instruction_list.first_instruction_;
957 last_instruction_ = instruction_list.last_instruction_;
958 } else {
959 AddAfter(last_instruction_, instruction_list);
960 }
961}
962
David Brazdil2d7352b2015-04-20 14:52:42 +0100963void HBasicBlock::DisconnectAndDelete() {
964 // Dominators must be removed after all the blocks they dominate. This way
965 // a loop header is removed last, a requirement for correct loop information
966 // iteration.
967 DCHECK(dominated_blocks_.IsEmpty());
David Brazdil46e2a392015-03-16 17:31:52 +0000968
David Brazdil2d7352b2015-04-20 14:52:42 +0100969 // Remove the block from all loops it is included in.
970 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
971 HLoopInformation* loop_info = it.Current();
972 loop_info->Remove(this);
973 if (loop_info->IsBackEdge(*this)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100974 // If this was the last back edge of the loop, we deliberately leave the
975 // loop in an inconsistent state and will fail SSAChecker unless the
976 // entire loop is removed during the pass.
David Brazdil2d7352b2015-04-20 14:52:42 +0100977 loop_info->RemoveBackEdge(this);
978 }
979 }
980
981 // Disconnect the block from its predecessors and update their control-flow
982 // instructions.
David Brazdil46e2a392015-03-16 17:31:52 +0000983 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
David Brazdil2d7352b2015-04-20 14:52:42 +0100984 HBasicBlock* predecessor = predecessors_.Get(i);
985 HInstruction* last_instruction = predecessor->GetLastInstruction();
986 predecessor->RemoveInstruction(last_instruction);
987 predecessor->RemoveSuccessor(this);
988 if (predecessor->GetSuccessors().Size() == 1u) {
989 DCHECK(last_instruction->IsIf());
990 predecessor->AddInstruction(new (graph_->GetArena()) HGoto());
991 } else {
992 // The predecessor has no remaining successors and therefore must be dead.
993 // We deliberately leave it without a control-flow instruction so that the
994 // SSAChecker fails unless it is not removed during the pass too.
995 DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u);
996 }
David Brazdil46e2a392015-03-16 17:31:52 +0000997 }
David Brazdil46e2a392015-03-16 17:31:52 +0000998 predecessors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +0100999
1000 // Disconnect the block from its successors and update their dominators
1001 // and phis.
1002 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
1003 HBasicBlock* successor = successors_.Get(i);
1004 // Delete this block from the list of predecessors.
1005 size_t this_index = successor->GetPredecessorIndexOf(this);
1006 successor->predecessors_.DeleteAt(this_index);
1007
1008 // Check that `successor` has other predecessors, otherwise `this` is the
1009 // dominator of `successor` which violates the order DCHECKed at the top.
1010 DCHECK(!successor->predecessors_.IsEmpty());
1011
1012 // Recompute the successor's dominator.
1013 HBasicBlock* old_dominator = successor->GetDominator();
1014 HBasicBlock* new_dominator = successor->predecessors_.Get(0);
1015 for (size_t j = 1, f = successor->predecessors_.Size(); j < f; ++j) {
1016 new_dominator = graph_->FindCommonDominator(
1017 new_dominator, successor->predecessors_.Get(j));
1018 }
1019 if (old_dominator != new_dominator) {
1020 successor->SetDominator(new_dominator);
1021 old_dominator->RemoveDominatedBlock(successor);
1022 new_dominator->AddDominatedBlock(successor);
1023 }
1024
1025 // Remove this block's entries in the successor's phis.
1026 if (successor->predecessors_.Size() == 1u) {
1027 // The successor has just one predecessor left. Replace phis with the only
1028 // remaining input.
1029 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1030 HPhi* phi = phi_it.Current()->AsPhi();
1031 phi->ReplaceWith(phi->InputAt(1 - this_index));
1032 successor->RemovePhi(phi);
1033 }
1034 } else {
1035 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1036 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1037 }
1038 }
1039 }
David Brazdil46e2a392015-03-16 17:31:52 +00001040 successors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001041
1042 // Disconnect from the dominator.
1043 dominator_->RemoveDominatedBlock(this);
1044 SetDominator(nullptr);
1045
1046 // Delete from the graph. The function safely deletes remaining instructions
1047 // and updates the reverse post order.
1048 graph_->DeleteDeadBlock(this);
1049 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001050}
1051
David Brazdil69a28042015-04-29 17:16:07 +01001052void HBasicBlock::UpdateLoopInformation() {
1053 // Check if loop information points to a dismantled loop. If so, replace with
1054 // the loop information of a larger loop which contains this block, or nullptr
1055 // otherwise. We iterate in case the larger loop has been destroyed too.
1056 while (IsInLoop() && loop_information_->GetBackEdges().IsEmpty()) {
1057 if (IsLoopHeader()) {
1058 HSuspendCheck* suspend_check = loop_information_->GetSuspendCheck();
1059 DCHECK_EQ(suspend_check->GetBlock(), this);
1060 RemoveInstruction(suspend_check);
1061 }
1062 loop_information_ = loop_information_->GetPreHeader()->GetLoopInformation();
1063 }
1064}
1065
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001066void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001067 DCHECK_EQ(GetGraph(), other->GetGraph());
1068 DCHECK(GetDominatedBlocks().Contains(other));
1069 DCHECK_EQ(GetSuccessors().Size(), 1u);
1070 DCHECK_EQ(GetSuccessors().Get(0), other);
1071 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1072 DCHECK_EQ(other->GetPredecessors().Get(0), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001073 DCHECK(other->GetPhis().IsEmpty());
1074
David Brazdil2d7352b2015-04-20 14:52:42 +01001075 // Move instructions from `other` to `this`.
1076 DCHECK(EndsWithControlFlowInstruction());
1077 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001078 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001079 other->instructions_.SetBlockOfInstructions(this);
1080 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001081
David Brazdil2d7352b2015-04-20 14:52:42 +01001082 // Remove `other` from the loops it is included in.
1083 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1084 HLoopInformation* loop_info = it.Current();
1085 loop_info->Remove(other);
1086 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001087 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01001088 }
1089 }
1090
1091 // Update links to the successors of `other`.
1092 successors_.Reset();
1093 while (!other->successors_.IsEmpty()) {
1094 HBasicBlock* successor = other->successors_.Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001095 successor->ReplacePredecessor(other, this);
1096 }
1097
David Brazdil2d7352b2015-04-20 14:52:42 +01001098 // Update the dominator tree.
1099 dominated_blocks_.Delete(other);
1100 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
1108 // Clear the list of predecessors of `other` in preparation of deleting it.
1109 other->predecessors_.Reset();
1110
1111 // Delete `other` from the graph. The function updates reverse post order.
1112 graph_->DeleteDeadBlock(other);
1113 other->SetGraph(nullptr);
1114}
1115
1116void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1117 DCHECK_NE(GetGraph(), other->GetGraph());
1118 DCHECK(GetDominatedBlocks().IsEmpty());
1119 DCHECK(GetSuccessors().IsEmpty());
1120 DCHECK(!EndsWithControlFlowInstruction());
1121 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1122 DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock());
1123 DCHECK(other->GetPhis().IsEmpty());
1124 DCHECK(!other->IsInLoop());
1125
1126 // Move instructions from `other` to `this`.
1127 instructions_.Add(other->GetInstructions());
1128 other->instructions_.SetBlockOfInstructions(this);
1129
1130 // Update links to the successors of `other`.
1131 successors_.Reset();
1132 while (!other->successors_.IsEmpty()) {
1133 HBasicBlock* successor = other->successors_.Get(0);
1134 successor->ReplacePredecessor(other, this);
1135 }
1136
1137 // Update the dominator tree.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001138 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1139 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1140 dominated_blocks_.Add(dominated);
1141 dominated->SetDominator(this);
1142 }
1143 other->dominated_blocks_.Reset();
1144 other->dominator_ = nullptr;
1145 other->graph_ = nullptr;
1146}
1147
1148void HBasicBlock::ReplaceWith(HBasicBlock* other) {
1149 while (!GetPredecessors().IsEmpty()) {
1150 HBasicBlock* predecessor = GetPredecessors().Get(0);
1151 predecessor->ReplaceSuccessor(this, other);
1152 }
1153 while (!GetSuccessors().IsEmpty()) {
1154 HBasicBlock* successor = GetSuccessors().Get(0);
1155 successor->ReplacePredecessor(this, other);
1156 }
1157 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
1158 other->AddDominatedBlock(dominated_blocks_.Get(i));
1159 }
1160 GetDominator()->ReplaceDominatedBlock(this, other);
1161 other->SetDominator(GetDominator());
1162 dominator_ = nullptr;
1163 graph_ = nullptr;
1164}
1165
1166// Create space in `blocks` for adding `number_of_new_blocks` entries
1167// starting at location `at`. Blocks after `at` are moved accordingly.
1168static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
1169 size_t number_of_new_blocks,
1170 size_t at) {
1171 size_t old_size = blocks->Size();
1172 size_t new_size = old_size + number_of_new_blocks;
1173 blocks->SetSize(new_size);
1174 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
1175 blocks->Put(j, blocks->Get(i));
1176 }
1177}
1178
David Brazdil2d7352b2015-04-20 14:52:42 +01001179void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1180 DCHECK_EQ(block->GetGraph(), this);
1181 DCHECK(block->GetSuccessors().IsEmpty());
1182 DCHECK(block->GetPredecessors().IsEmpty());
1183 DCHECK(block->GetDominatedBlocks().IsEmpty());
1184 DCHECK(block->GetDominator() == nullptr);
1185
1186 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1187 block->RemoveInstruction(it.Current());
1188 }
1189 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1190 block->RemovePhi(it.Current()->AsPhi());
1191 }
1192
1193 reverse_post_order_.Delete(block);
1194 blocks_.Put(block->GetBlockId(), nullptr);
1195}
1196
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001197void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001198 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001199 // Simple case of an entry block, a body block, and an exit block.
1200 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001201 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001202 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
1203 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001204 DCHECK(!body->IsExitBlock());
1205 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001206
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001207 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1208 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001209
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001210 // Replace the invoke with the return value of the inlined graph.
1211 if (last->IsReturn()) {
1212 invoke->ReplaceWith(last->InputAt(0));
1213 } else {
1214 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001215 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001216
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001217 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001218 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001219 // Need to inline multiple blocks. We split `invoke`'s block
1220 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001221 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001222 // with the second half.
1223 ArenaAllocator* allocator = outer_graph->GetArena();
1224 HBasicBlock* at = invoke->GetBlock();
1225 HBasicBlock* to = at->SplitAfter(invoke);
1226
1227 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
1228 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001229 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001230 exit_block_->ReplaceWith(to);
1231
1232 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001233 // to not `HReturn` but `HGoto` instead.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001234 HInstruction* return_value = nullptr;
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001235 bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
1236 if (to->GetPredecessors().Size() == 1) {
1237 HBasicBlock* predecessor = to->GetPredecessors().Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001238 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001239 if (!returns_void) {
1240 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001241 }
1242 predecessor->AddInstruction(new (allocator) HGoto());
1243 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001244 } else {
1245 if (!returns_void) {
1246 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001247 return_value = new (allocator) HPhi(
1248 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001249 to->AddPhi(return_value->AsPhi());
1250 }
1251 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
1252 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
1253 HInstruction* last = predecessor->GetLastInstruction();
1254 if (!returns_void) {
1255 return_value->AsPhi()->AddInput(last->InputAt(0));
1256 }
1257 predecessor->AddInstruction(new (allocator) HGoto());
1258 predecessor->RemoveInstruction(last);
1259 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001260 }
1261
1262 if (return_value != nullptr) {
1263 invoke->ReplaceWith(return_value);
1264 }
1265
1266 // Update the meta information surrounding blocks:
1267 // (1) the graph they are now in,
1268 // (2) the reverse post order of that graph,
1269 // (3) the potential loop information they are now in.
1270
1271 // We don't add the entry block, the exit block, and the first block, which
1272 // has been merged with `at`.
1273 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1274
1275 // We add the `to` block.
1276 static constexpr int kNumberOfNewBlocksInCaller = 1;
1277 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
1278 + kNumberOfNewBlocksInCaller;
1279
1280 // Find the location of `at` in the outer graph's reverse post order. The new
1281 // blocks will be added after it.
1282 size_t index_of_at = 0;
1283 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
1284 index_of_at++;
1285 }
1286 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1287
1288 // Do a reverse post order of the blocks in the callee and do (1), (2),
1289 // and (3) to the blocks that apply.
1290 HLoopInformation* info = at->GetLoopInformation();
1291 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1292 HBasicBlock* current = it.Current();
1293 if (current != exit_block_ && current != entry_block_ && current != first) {
1294 DCHECK(!current->IsInLoop());
1295 DCHECK(current->GetGraph() == this);
1296 current->SetGraph(outer_graph);
1297 outer_graph->AddBlock(current);
1298 outer_graph->reverse_post_order_.Put(++index_of_at, current);
1299 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001300 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001301 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1302 loop_it.Current()->Add(current);
1303 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001304 }
1305 }
1306 }
1307
1308 // Do (1), (2), and (3) to `to`.
1309 to->SetGraph(outer_graph);
1310 outer_graph->AddBlock(to);
1311 outer_graph->reverse_post_order_.Put(++index_of_at, to);
1312 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001313 to->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001314 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1315 loop_it.Current()->Add(to);
1316 }
David Brazdil46e2a392015-03-16 17:31:52 +00001317 if (info->IsBackEdge(*at)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001318 // Only `to` can become a back edge, as the inlined blocks
1319 // are predecessors of `to`.
1320 info->ReplaceBackEdge(at, to);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001321 }
1322 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001323 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001324
David Brazdil05144f42015-04-16 15:18:00 +01001325 // Update the next instruction id of the outer graph, so that instructions
1326 // added later get bigger ids than those in the inner graph.
1327 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1328
1329 // Walk over the entry block and:
1330 // - Move constants from the entry block to the outer_graph's entry block,
1331 // - Replace HParameterValue instructions with their real value.
1332 // - Remove suspend checks, that hold an environment.
1333 // We must do this after the other blocks have been inlined, otherwise ids of
1334 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001335 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001336 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1337 HInstruction* current = it.Current();
1338 if (current->IsNullConstant()) {
1339 current->ReplaceWith(outer_graph->GetNullConstant());
1340 } else if (current->IsIntConstant()) {
1341 current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue()));
1342 } else if (current->IsLongConstant()) {
1343 current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue()));
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001344 } else if (current->IsFloatConstant()) {
1345 current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue()));
1346 } else if (current->IsDoubleConstant()) {
1347 current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue()));
David Brazdil05144f42015-04-16 15:18:00 +01001348 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001349 if (kIsDebugBuild
1350 && invoke->IsInvokeStaticOrDirect()
1351 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1352 // Ensure we do not use the last input of `invoke`, as it
1353 // contains a clinit check which is not an actual argument.
1354 size_t last_input_index = invoke->InputCount() - 1;
1355 DCHECK(parameter_index != last_input_index);
1356 }
David Brazdil05144f42015-04-16 15:18:00 +01001357 current->ReplaceWith(invoke->InputAt(parameter_index++));
1358 } else {
1359 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1360 entry_block_->RemoveInstruction(current);
1361 }
1362 }
1363
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001364 // Finally remove the invoke from the caller.
1365 invoke->GetBlock()->RemoveInstruction(invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001366}
1367
Calin Juravleacf735c2015-02-12 15:25:22 +00001368std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1369 ScopedObjectAccess soa(Thread::Current());
1370 os << "["
1371 << " is_top=" << rhs.IsTop()
1372 << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
1373 << " is_exact=" << rhs.IsExact()
1374 << " ]";
1375 return os;
1376}
1377
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001378} // namespace art