blob: 85c0361f463adc1fbca522326923ce132444bde3 [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
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100194 // Make sure the loop has only one pre header. This simplifies SSA building by having
195 // to just look at the pre header to know which locals are initialized at entry of the
196 // loop.
197 size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges();
198 if (number_of_incomings != 1) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100199 HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100200 AddBlock(pre_header);
201 pre_header->AddInstruction(new (arena_) HGoto());
202
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100203 for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) {
204 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100205 if (!info->IsBackEdge(*predecessor)) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100206 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100207 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100208 }
209 }
210 pre_header->AddSuccessor(header);
211 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100212
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100213 // Make sure the first predecessor of a loop header is the incoming block.
214 if (info->IsBackEdge(*header->GetPredecessors().Get(0))) {
215 HBasicBlock* to_swap = header->GetPredecessors().Get(0);
216 for (size_t pred = 1, e = header->GetPredecessors().Size(); pred < e; ++pred) {
217 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
218 if (!info->IsBackEdge(*predecessor)) {
219 header->predecessors_.Put(pred, to_swap);
220 header->predecessors_.Put(0, predecessor);
221 break;
222 }
223 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100224 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100225
226 // Place the suspend check at the beginning of the header, so that live registers
227 // will be known when allocating registers. Note that code generation can still
228 // generate the suspend check at the back edge, but needs to be careful with
229 // loop phi spill slots (which are not written to at back edge).
230 HInstruction* first_instruction = header->GetFirstInstruction();
231 if (!first_instruction->IsSuspendCheck()) {
232 HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc());
233 header->InsertInstructionBefore(check, first_instruction);
234 first_instruction = check;
235 }
236 info->SetSuspendCheck(first_instruction->AsSuspendCheck());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100237}
238
239void HGraph::SimplifyCFG() {
240 // Simplify the CFG for future analysis, and code generation:
241 // (1): Split critical edges.
242 // (2): Simplify loops by having only one back edge, and one preheader.
243 for (size_t i = 0; i < blocks_.Size(); ++i) {
244 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100245 if (block == nullptr) continue;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100246 if (block->GetSuccessors().Size() > 1) {
247 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
248 HBasicBlock* successor = block->GetSuccessors().Get(j);
249 if (successor->GetPredecessors().Size() > 1) {
250 SplitCriticalEdge(block, successor);
251 --j;
252 }
253 }
254 }
255 if (block->IsLoopHeader()) {
256 SimplifyLoop(block);
257 }
258 }
259}
260
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000261bool HGraph::AnalyzeNaturalLoops() const {
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100262 // Order does not matter.
263 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
264 HBasicBlock* block = it.Current();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100265 if (block->IsLoopHeader()) {
266 HLoopInformation* info = block->GetLoopInformation();
267 if (!info->Populate()) {
268 // Abort if the loop is non natural. We currently bailout in such cases.
269 return false;
270 }
271 }
272 }
273 return true;
274}
275
David Brazdil8d5b8b22015-03-24 10:51:52 +0000276void HGraph::InsertConstant(HConstant* constant) {
277 // New constants are inserted before the final control-flow instruction
278 // of the graph, or at its end if called from the graph builder.
279 if (entry_block_->EndsWithControlFlowInstruction()) {
280 entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction());
David Brazdil46e2a392015-03-16 17:31:52 +0000281 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000282 entry_block_->AddInstruction(constant);
David Brazdil46e2a392015-03-16 17:31:52 +0000283 }
284}
285
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000286HNullConstant* HGraph::GetNullConstant() {
287 if (cached_null_constant_ == nullptr) {
288 cached_null_constant_ = new (arena_) HNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000289 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000290 }
291 return cached_null_constant_;
292}
293
David Brazdil8d5b8b22015-03-24 10:51:52 +0000294HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value) {
295 switch (type) {
296 case Primitive::Type::kPrimBoolean:
297 DCHECK(IsUint<1>(value));
298 FALLTHROUGH_INTENDED;
299 case Primitive::Type::kPrimByte:
300 case Primitive::Type::kPrimChar:
301 case Primitive::Type::kPrimShort:
302 case Primitive::Type::kPrimInt:
303 DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value));
304 return GetIntConstant(static_cast<int32_t>(value));
305
306 case Primitive::Type::kPrimLong:
307 return GetLongConstant(value);
308
309 default:
310 LOG(FATAL) << "Unsupported constant type";
311 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000312 }
David Brazdil46e2a392015-03-16 17:31:52 +0000313}
314
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000315void HGraph::CacheFloatConstant(HFloatConstant* constant) {
316 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
317 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
318 cached_float_constants_.Overwrite(value, constant);
319}
320
321void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
322 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
323 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
324 cached_double_constants_.Overwrite(value, constant);
325}
326
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000327void HLoopInformation::Add(HBasicBlock* block) {
328 blocks_.SetBit(block->GetBlockId());
329}
330
David Brazdil46e2a392015-03-16 17:31:52 +0000331void HLoopInformation::Remove(HBasicBlock* block) {
332 blocks_.ClearBit(block->GetBlockId());
333}
334
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100335void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
336 if (blocks_.IsBitSet(block->GetBlockId())) {
337 return;
338 }
339
340 blocks_.SetBit(block->GetBlockId());
341 block->SetInLoop(this);
342 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
343 PopulateRecursive(block->GetPredecessors().Get(i));
344 }
345}
346
347bool HLoopInformation::Populate() {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100348 for (size_t i = 0, e = GetBackEdges().Size(); i < e; ++i) {
349 HBasicBlock* back_edge = GetBackEdges().Get(i);
350 DCHECK(back_edge->GetDominator() != nullptr);
351 if (!header_->Dominates(back_edge)) {
352 // This loop is not natural. Do not bother going further.
353 return false;
354 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100355
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100356 // Populate this loop: starting with the back edge, recursively add predecessors
357 // that are not already part of that loop. Set the header as part of the loop
358 // to end the recursion.
359 // This is a recursive implementation of the algorithm described in
360 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
361 blocks_.SetBit(header_->GetBlockId());
362 PopulateRecursive(back_edge);
363 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100364 return true;
365}
366
367HBasicBlock* HLoopInformation::GetPreHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100368 return header_->GetDominator();
369}
370
371bool HLoopInformation::Contains(const HBasicBlock& block) const {
372 return blocks_.IsBitSet(block.GetBlockId());
373}
374
375bool HLoopInformation::IsIn(const HLoopInformation& other) const {
376 return other.blocks_.IsBitSet(header_->GetBlockId());
377}
378
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100379size_t HLoopInformation::GetLifetimeEnd() const {
380 size_t last_position = 0;
381 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
382 last_position = std::max(back_edges_.Get(i)->GetLifetimeEnd(), last_position);
383 }
384 return last_position;
385}
386
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100387bool HBasicBlock::Dominates(HBasicBlock* other) const {
388 // Walk up the dominator tree from `other`, to find out if `this`
389 // is an ancestor.
390 HBasicBlock* current = other;
391 while (current != nullptr) {
392 if (current == this) {
393 return true;
394 }
395 current = current->GetDominator();
396 }
397 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100398}
399
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100400static void UpdateInputsUsers(HInstruction* instruction) {
401 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
402 instruction->InputAt(i)->AddUseAt(instruction, i);
403 }
404 // Environment should be created later.
405 DCHECK(!instruction->HasEnvironment());
406}
407
Roland Levillainccc07a92014-09-16 14:48:16 +0100408void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
409 HInstruction* replacement) {
410 DCHECK(initial->GetBlock() == this);
411 InsertInstructionBefore(replacement, initial);
412 initial->ReplaceWith(replacement);
413 RemoveInstruction(initial);
414}
415
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100416static void Add(HInstructionList* instruction_list,
417 HBasicBlock* block,
418 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000419 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000420 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100421 instruction->SetBlock(block);
422 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100423 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100424 instruction_list->AddInstruction(instruction);
425}
426
427void HBasicBlock::AddInstruction(HInstruction* instruction) {
428 Add(&instructions_, this, instruction);
429}
430
431void HBasicBlock::AddPhi(HPhi* phi) {
432 Add(&phis_, this, phi);
433}
434
David Brazdilc3d743f2015-04-22 13:40:50 +0100435void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
436 DCHECK(!cursor->IsPhi());
437 DCHECK(!instruction->IsPhi());
438 DCHECK_EQ(instruction->GetId(), -1);
439 DCHECK_NE(cursor->GetId(), -1);
440 DCHECK_EQ(cursor->GetBlock(), this);
441 DCHECK(!instruction->IsControlFlow());
442 instruction->SetBlock(this);
443 instruction->SetId(GetGraph()->GetNextInstructionId());
444 UpdateInputsUsers(instruction);
445 instructions_.InsertInstructionBefore(instruction, cursor);
446}
447
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100448void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
449 DCHECK(!cursor->IsPhi());
450 DCHECK(!instruction->IsPhi());
451 DCHECK_EQ(instruction->GetId(), -1);
452 DCHECK_NE(cursor->GetId(), -1);
453 DCHECK_EQ(cursor->GetBlock(), this);
454 DCHECK(!instruction->IsControlFlow());
455 DCHECK(!cursor->IsControlFlow());
456 instruction->SetBlock(this);
457 instruction->SetId(GetGraph()->GetNextInstructionId());
458 UpdateInputsUsers(instruction);
459 instructions_.InsertInstructionAfter(instruction, cursor);
460}
461
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100462void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
463 DCHECK_EQ(phi->GetId(), -1);
464 DCHECK_NE(cursor->GetId(), -1);
465 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100466 phi->SetBlock(this);
467 phi->SetId(GetGraph()->GetNextInstructionId());
468 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100469 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100470}
471
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100472static void Remove(HInstructionList* instruction_list,
473 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000474 HInstruction* instruction,
475 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100476 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100477 instruction->SetBlock(nullptr);
478 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000479 if (ensure_safety) {
480 DCHECK(instruction->GetUses().IsEmpty());
481 DCHECK(instruction->GetEnvUses().IsEmpty());
482 RemoveAsUser(instruction);
483 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100484}
485
David Brazdil1abb4192015-02-17 18:33:36 +0000486void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100487 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000488 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100489}
490
David Brazdil1abb4192015-02-17 18:33:36 +0000491void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
492 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100493}
494
David Brazdilc7508e92015-04-27 13:28:57 +0100495void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
496 if (instruction->IsPhi()) {
497 RemovePhi(instruction->AsPhi(), ensure_safety);
498 } else {
499 RemoveInstruction(instruction, ensure_safety);
500 }
501}
502
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +0100503void HEnvironment::CopyFrom(const GrowableArray<HInstruction*>& locals) {
504 for (size_t i = 0; i < locals.Size(); i++) {
505 HInstruction* instruction = locals.Get(i);
506 SetRawEnvAt(i, instruction);
507 if (instruction != nullptr) {
508 instruction->AddEnvUseAt(this, i);
509 }
510 }
511}
512
David Brazdiled596192015-01-23 10:39:45 +0000513void HEnvironment::CopyFrom(HEnvironment* env) {
514 for (size_t i = 0; i < env->Size(); i++) {
515 HInstruction* instruction = env->GetInstructionAt(i);
516 SetRawEnvAt(i, instruction);
517 if (instruction != nullptr) {
518 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100519 }
David Brazdiled596192015-01-23 10:39:45 +0000520 }
521}
522
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700523void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
524 HBasicBlock* loop_header) {
525 DCHECK(loop_header->IsLoopHeader());
526 for (size_t i = 0; i < env->Size(); i++) {
527 HInstruction* instruction = env->GetInstructionAt(i);
528 SetRawEnvAt(i, instruction);
529 if (instruction == nullptr) {
530 continue;
531 }
532 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
533 // At the end of the loop pre-header, the corresponding value for instruction
534 // is the first input of the phi.
535 HInstruction* initial = instruction->AsPhi()->InputAt(0);
536 DCHECK(initial->GetBlock()->Dominates(loop_header));
537 SetRawEnvAt(i, initial);
538 initial->AddEnvUseAt(this, i);
539 } else {
540 instruction->AddEnvUseAt(this, i);
541 }
542 }
543}
544
David Brazdil1abb4192015-02-17 18:33:36 +0000545void HEnvironment::RemoveAsUserOfInput(size_t index) const {
546 const HUserRecord<HEnvironment*> user_record = vregs_.Get(index);
547 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100548}
549
Calin Juravle77520bc2015-01-12 18:45:46 +0000550HInstruction* HInstruction::GetNextDisregardingMoves() const {
551 HInstruction* next = GetNext();
552 while (next != nullptr && next->IsParallelMove()) {
553 next = next->GetNext();
554 }
555 return next;
556}
557
558HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
559 HInstruction* previous = GetPrevious();
560 while (previous != nullptr && previous->IsParallelMove()) {
561 previous = previous->GetPrevious();
562 }
563 return previous;
564}
565
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100566void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000567 if (first_instruction_ == nullptr) {
568 DCHECK(last_instruction_ == nullptr);
569 first_instruction_ = last_instruction_ = instruction;
570 } else {
571 last_instruction_->next_ = instruction;
572 instruction->previous_ = last_instruction_;
573 last_instruction_ = instruction;
574 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000575}
576
David Brazdilc3d743f2015-04-22 13:40:50 +0100577void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
578 DCHECK(Contains(cursor));
579 if (cursor == first_instruction_) {
580 cursor->previous_ = instruction;
581 instruction->next_ = cursor;
582 first_instruction_ = instruction;
583 } else {
584 instruction->previous_ = cursor->previous_;
585 instruction->next_ = cursor;
586 cursor->previous_ = instruction;
587 instruction->previous_->next_ = instruction;
588 }
589}
590
591void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
592 DCHECK(Contains(cursor));
593 if (cursor == last_instruction_) {
594 cursor->next_ = instruction;
595 instruction->previous_ = cursor;
596 last_instruction_ = instruction;
597 } else {
598 instruction->next_ = cursor->next_;
599 instruction->previous_ = cursor;
600 cursor->next_ = instruction;
601 instruction->next_->previous_ = instruction;
602 }
603}
604
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100605void HInstructionList::RemoveInstruction(HInstruction* instruction) {
606 if (instruction->previous_ != nullptr) {
607 instruction->previous_->next_ = instruction->next_;
608 }
609 if (instruction->next_ != nullptr) {
610 instruction->next_->previous_ = instruction->previous_;
611 }
612 if (instruction == first_instruction_) {
613 first_instruction_ = instruction->next_;
614 }
615 if (instruction == last_instruction_) {
616 last_instruction_ = instruction->previous_;
617 }
618}
619
Roland Levillain6b469232014-09-25 10:10:38 +0100620bool HInstructionList::Contains(HInstruction* instruction) const {
621 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
622 if (it.Current() == instruction) {
623 return true;
624 }
625 }
626 return false;
627}
628
Roland Levillainccc07a92014-09-16 14:48:16 +0100629bool HInstructionList::FoundBefore(const HInstruction* instruction1,
630 const HInstruction* instruction2) const {
631 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
632 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
633 if (it.Current() == instruction1) {
634 return true;
635 }
636 if (it.Current() == instruction2) {
637 return false;
638 }
639 }
640 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
641 return true;
642}
643
Roland Levillain6c82d402014-10-13 16:10:27 +0100644bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
645 if (other_instruction == this) {
646 // An instruction does not strictly dominate itself.
647 return false;
648 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100649 HBasicBlock* block = GetBlock();
650 HBasicBlock* other_block = other_instruction->GetBlock();
651 if (block != other_block) {
652 return GetBlock()->Dominates(other_instruction->GetBlock());
653 } else {
654 // If both instructions are in the same block, ensure this
655 // instruction comes before `other_instruction`.
656 if (IsPhi()) {
657 if (!other_instruction->IsPhi()) {
658 // Phis appear before non phi-instructions so this instruction
659 // dominates `other_instruction`.
660 return true;
661 } else {
662 // There is no order among phis.
663 LOG(FATAL) << "There is no dominance between phis of a same block.";
664 return false;
665 }
666 } else {
667 // `this` is not a phi.
668 if (other_instruction->IsPhi()) {
669 // Phis appear before non phi-instructions so this instruction
670 // does not dominate `other_instruction`.
671 return false;
672 } else {
673 // Check whether this instruction comes before
674 // `other_instruction` in the instruction list.
675 return block->GetInstructions().FoundBefore(this, other_instruction);
676 }
677 }
678 }
679}
680
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100682 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000683 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
684 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100685 HInstruction* user = current->GetUser();
686 size_t input_index = current->GetIndex();
687 user->SetRawInputAt(input_index, other);
688 other->AddUseAt(user, input_index);
689 }
690
David Brazdiled596192015-01-23 10:39:45 +0000691 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
692 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100693 HEnvironment* user = current->GetUser();
694 size_t input_index = current->GetIndex();
695 user->SetRawEnvAt(input_index, other);
696 other->AddEnvUseAt(user, input_index);
697 }
698
David Brazdiled596192015-01-23 10:39:45 +0000699 uses_.Clear();
700 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100701}
702
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100703void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000704 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100705 SetRawInputAt(index, replacement);
706 replacement->AddUseAt(this, index);
707}
708
Nicolas Geoffray39468442014-09-02 15:17:15 +0100709size_t HInstruction::EnvironmentSize() const {
710 return HasEnvironment() ? environment_->Size() : 0;
711}
712
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100713void HPhi::AddInput(HInstruction* input) {
714 DCHECK(input->GetBlock() != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +0000715 inputs_.Add(HUserRecord<HInstruction*>(input));
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100716 input->AddUseAt(this, inputs_.Size() - 1);
717}
718
David Brazdil2d7352b2015-04-20 14:52:42 +0100719void HPhi::RemoveInputAt(size_t index) {
720 RemoveAsUserOfInput(index);
721 inputs_.DeleteAt(index);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100722 for (size_t i = index, e = InputCount(); i < e; ++i) {
723 InputRecordAt(i).GetUseNode()->SetIndex(i);
724 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100725}
726
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100727#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000728void H##name::Accept(HGraphVisitor* visitor) { \
729 visitor->Visit##name(this); \
730}
731
732FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
733
734#undef DEFINE_ACCEPT
735
736void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100737 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
738 for (size_t i = 0 ; i < blocks.Size(); i++) {
David Brazdil46e2a392015-03-16 17:31:52 +0000739 HBasicBlock* block = blocks.Get(i);
740 if (block != nullptr) {
741 VisitBasicBlock(block);
742 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000743 }
744}
745
Roland Levillain633021e2014-10-01 14:12:25 +0100746void HGraphVisitor::VisitReversePostOrder() {
747 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
748 VisitBasicBlock(it.Current());
749 }
750}
751
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000752void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100753 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100754 it.Current()->Accept(this);
755 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100756 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000757 it.Current()->Accept(this);
758 }
759}
760
Roland Levillain9240d6a2014-10-20 16:47:04 +0100761HConstant* HUnaryOperation::TryStaticEvaluation() const {
762 if (GetInput()->IsIntConstant()) {
763 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000764 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain9240d6a2014-10-20 16:47:04 +0100765 } else if (GetInput()->IsLongConstant()) {
Roland Levillainb762d2e2014-10-22 10:11:06 +0100766 // TODO: Implement static evaluation of long unary operations.
767 //
768 // Do not exit with a fatal condition here. Instead, simply
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700769 // return `null' to notify the caller that this instruction
Roland Levillainb762d2e2014-10-22 10:11:06 +0100770 // cannot (yet) be statically evaluated.
Roland Levillain9240d6a2014-10-20 16:47:04 +0100771 return nullptr;
772 }
773 return nullptr;
774}
775
776HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +0100777 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
778 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
779 GetRight()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000780 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100781 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
782 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
783 GetRight()->AsLongConstant()->GetValue());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000784 if (GetResultType() == Primitive::kPrimLong) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000785 return GetBlock()->GetGraph()->GetLongConstant(value);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000786 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000787 DCHECK_EQ(GetResultType(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000788 return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000789 }
Roland Levillain556c3d12014-09-18 15:25:07 +0100790 }
791 return nullptr;
792}
Dave Allison20dfc792014-06-16 20:44:29 -0700793
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000794HConstant* HBinaryOperation::GetConstantRight() const {
795 if (GetRight()->IsConstant()) {
796 return GetRight()->AsConstant();
797 } else if (IsCommutative() && GetLeft()->IsConstant()) {
798 return GetLeft()->AsConstant();
799 } else {
800 return nullptr;
801 }
802}
803
804// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700805// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000806HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
807 HInstruction* most_constant_right = GetConstantRight();
808 if (most_constant_right == nullptr) {
809 return nullptr;
810 } else if (most_constant_right == GetLeft()) {
811 return GetRight();
812 } else {
813 return GetLeft();
814 }
815}
816
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700817bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
818 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100819}
820
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100821bool HInstruction::Equals(HInstruction* other) const {
822 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100823 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100824 if (!InstructionDataEquals(other)) return false;
825 if (GetType() != other->GetType()) return false;
826 if (InputCount() != other->InputCount()) return false;
827
828 for (size_t i = 0, e = InputCount(); i < e; ++i) {
829 if (InputAt(i) != other->InputAt(i)) return false;
830 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100831 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100832 return true;
833}
834
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700835std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
836#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
837 switch (rhs) {
838 FOR_EACH_INSTRUCTION(DECLARE_CASE)
839 default:
840 os << "Unknown instruction kind " << static_cast<int>(rhs);
841 break;
842 }
843#undef DECLARE_CASE
844 return os;
845}
846
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000847void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000848 next_->previous_ = previous_;
849 if (previous_ != nullptr) {
850 previous_->next_ = next_;
851 }
852 if (block_->instructions_.first_instruction_ == this) {
853 block_->instructions_.first_instruction_ = next_;
854 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000855 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000856
857 previous_ = cursor->previous_;
858 if (previous_ != nullptr) {
859 previous_->next_ = this;
860 }
861 next_ = cursor;
862 cursor->previous_ = this;
863 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000864
865 if (block_->instructions_.first_instruction_ == cursor) {
866 block_->instructions_.first_instruction_ = this;
867 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000868}
869
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000870HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
871 DCHECK(!cursor->IsControlFlow());
872 DCHECK_NE(instructions_.last_instruction_, cursor);
873 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000874
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000875 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
876 new_block->instructions_.first_instruction_ = cursor->GetNext();
877 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
878 cursor->next_->previous_ = nullptr;
879 cursor->next_ = nullptr;
880 instructions_.last_instruction_ = cursor;
881
882 new_block->instructions_.SetBlockOfInstructions(new_block);
883 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
884 HBasicBlock* successor = GetSuccessors().Get(i);
885 new_block->successors_.Add(successor);
886 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
887 }
888 successors_.Reset();
889
890 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
891 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
892 dominated->dominator_ = new_block;
893 new_block->dominated_blocks_.Add(dominated);
894 }
895 dominated_blocks_.Reset();
896 return new_block;
897}
898
David Brazdil46e2a392015-03-16 17:31:52 +0000899bool HBasicBlock::IsSingleGoto() const {
900 HLoopInformation* loop_info = GetLoopInformation();
901 // TODO: Remove the null check b/19084197.
902 return GetFirstInstruction() != nullptr
903 && GetPhis().IsEmpty()
904 && GetFirstInstruction() == GetLastInstruction()
905 && GetLastInstruction()->IsGoto()
906 // Back edges generate the suspend check.
907 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
908}
909
David Brazdil8d5b8b22015-03-24 10:51:52 +0000910bool HBasicBlock::EndsWithControlFlowInstruction() const {
911 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
912}
913
David Brazdilb2bd1c52015-03-25 11:17:37 +0000914bool HBasicBlock::EndsWithIf() const {
915 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
916}
917
918bool HBasicBlock::HasSinglePhi() const {
919 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
920}
921
David Brazdil2d7352b2015-04-20 14:52:42 +0100922size_t HInstructionList::CountSize() const {
923 size_t size = 0;
924 HInstruction* current = first_instruction_;
925 for (; current != nullptr; current = current->GetNext()) {
926 size++;
927 }
928 return size;
929}
930
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000931void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
932 for (HInstruction* current = first_instruction_;
933 current != nullptr;
934 current = current->GetNext()) {
935 current->SetBlock(block);
936 }
937}
938
939void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
940 DCHECK(Contains(cursor));
941 if (!instruction_list.IsEmpty()) {
942 if (cursor == last_instruction_) {
943 last_instruction_ = instruction_list.last_instruction_;
944 } else {
945 cursor->next_->previous_ = instruction_list.last_instruction_;
946 }
947 instruction_list.last_instruction_->next_ = cursor->next_;
948 cursor->next_ = instruction_list.first_instruction_;
949 instruction_list.first_instruction_->previous_ = cursor;
950 }
951}
952
953void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +0000954 if (IsEmpty()) {
955 first_instruction_ = instruction_list.first_instruction_;
956 last_instruction_ = instruction_list.last_instruction_;
957 } else {
958 AddAfter(last_instruction_, instruction_list);
959 }
960}
961
David Brazdil2d7352b2015-04-20 14:52:42 +0100962void HBasicBlock::DisconnectAndDelete() {
963 // Dominators must be removed after all the blocks they dominate. This way
964 // a loop header is removed last, a requirement for correct loop information
965 // iteration.
966 DCHECK(dominated_blocks_.IsEmpty());
David Brazdil46e2a392015-03-16 17:31:52 +0000967
David Brazdil2d7352b2015-04-20 14:52:42 +0100968 // Remove the block from all loops it is included in.
969 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
970 HLoopInformation* loop_info = it.Current();
971 loop_info->Remove(this);
972 if (loop_info->IsBackEdge(*this)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100973 // If this was the last back edge of the loop, we deliberately leave the
974 // loop in an inconsistent state and will fail SSAChecker unless the
975 // entire loop is removed during the pass.
David Brazdil2d7352b2015-04-20 14:52:42 +0100976 loop_info->RemoveBackEdge(this);
977 }
978 }
979
980 // Disconnect the block from its predecessors and update their control-flow
981 // instructions.
David Brazdil46e2a392015-03-16 17:31:52 +0000982 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
David Brazdil2d7352b2015-04-20 14:52:42 +0100983 HBasicBlock* predecessor = predecessors_.Get(i);
984 HInstruction* last_instruction = predecessor->GetLastInstruction();
985 predecessor->RemoveInstruction(last_instruction);
986 predecessor->RemoveSuccessor(this);
987 if (predecessor->GetSuccessors().Size() == 1u) {
988 DCHECK(last_instruction->IsIf());
989 predecessor->AddInstruction(new (graph_->GetArena()) HGoto());
990 } else {
991 // The predecessor has no remaining successors and therefore must be dead.
992 // We deliberately leave it without a control-flow instruction so that the
993 // SSAChecker fails unless it is not removed during the pass too.
994 DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u);
995 }
David Brazdil46e2a392015-03-16 17:31:52 +0000996 }
David Brazdil46e2a392015-03-16 17:31:52 +0000997 predecessors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +0100998
999 // Disconnect the block from its successors and update their dominators
1000 // and phis.
1001 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
1002 HBasicBlock* successor = successors_.Get(i);
1003 // Delete this block from the list of predecessors.
1004 size_t this_index = successor->GetPredecessorIndexOf(this);
1005 successor->predecessors_.DeleteAt(this_index);
1006
1007 // Check that `successor` has other predecessors, otherwise `this` is the
1008 // dominator of `successor` which violates the order DCHECKed at the top.
1009 DCHECK(!successor->predecessors_.IsEmpty());
1010
1011 // Recompute the successor's dominator.
1012 HBasicBlock* old_dominator = successor->GetDominator();
1013 HBasicBlock* new_dominator = successor->predecessors_.Get(0);
1014 for (size_t j = 1, f = successor->predecessors_.Size(); j < f; ++j) {
1015 new_dominator = graph_->FindCommonDominator(
1016 new_dominator, successor->predecessors_.Get(j));
1017 }
1018 if (old_dominator != new_dominator) {
1019 successor->SetDominator(new_dominator);
1020 old_dominator->RemoveDominatedBlock(successor);
1021 new_dominator->AddDominatedBlock(successor);
1022 }
1023
1024 // Remove this block's entries in the successor's phis.
1025 if (successor->predecessors_.Size() == 1u) {
1026 // The successor has just one predecessor left. Replace phis with the only
1027 // remaining input.
1028 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1029 HPhi* phi = phi_it.Current()->AsPhi();
1030 phi->ReplaceWith(phi->InputAt(1 - this_index));
1031 successor->RemovePhi(phi);
1032 }
1033 } else {
1034 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1035 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1036 }
1037 }
1038 }
David Brazdil46e2a392015-03-16 17:31:52 +00001039 successors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001040
1041 // Disconnect from the dominator.
1042 dominator_->RemoveDominatedBlock(this);
1043 SetDominator(nullptr);
1044
1045 // Delete from the graph. The function safely deletes remaining instructions
1046 // and updates the reverse post order.
1047 graph_->DeleteDeadBlock(this);
1048 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001049}
1050
David Brazdil69a28042015-04-29 17:16:07 +01001051void HBasicBlock::UpdateLoopInformation() {
1052 // Check if loop information points to a dismantled loop. If so, replace with
1053 // the loop information of a larger loop which contains this block, or nullptr
1054 // otherwise. We iterate in case the larger loop has been destroyed too.
1055 while (IsInLoop() && loop_information_->GetBackEdges().IsEmpty()) {
1056 if (IsLoopHeader()) {
1057 HSuspendCheck* suspend_check = loop_information_->GetSuspendCheck();
1058 DCHECK_EQ(suspend_check->GetBlock(), this);
1059 RemoveInstruction(suspend_check);
1060 }
1061 loop_information_ = loop_information_->GetPreHeader()->GetLoopInformation();
1062 }
1063}
1064
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001065void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001066 DCHECK_EQ(GetGraph(), other->GetGraph());
1067 DCHECK(GetDominatedBlocks().Contains(other));
1068 DCHECK_EQ(GetSuccessors().Size(), 1u);
1069 DCHECK_EQ(GetSuccessors().Get(0), other);
1070 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1071 DCHECK_EQ(other->GetPredecessors().Get(0), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072 DCHECK(other->GetPhis().IsEmpty());
1073
David Brazdil2d7352b2015-04-20 14:52:42 +01001074 // Move instructions from `other` to `this`.
1075 DCHECK(EndsWithControlFlowInstruction());
1076 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001077 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001078 other->instructions_.SetBlockOfInstructions(this);
1079 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001080
David Brazdil2d7352b2015-04-20 14:52:42 +01001081 // Remove `other` from the loops it is included in.
1082 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1083 HLoopInformation* loop_info = it.Current();
1084 loop_info->Remove(other);
1085 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001086 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01001087 }
1088 }
1089
1090 // Update links to the successors of `other`.
1091 successors_.Reset();
1092 while (!other->successors_.IsEmpty()) {
1093 HBasicBlock* successor = other->successors_.Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001094 successor->ReplacePredecessor(other, this);
1095 }
1096
David Brazdil2d7352b2015-04-20 14:52:42 +01001097 // Update the dominator tree.
1098 dominated_blocks_.Delete(other);
1099 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1100 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1101 dominated_blocks_.Add(dominated);
1102 dominated->SetDominator(this);
1103 }
1104 other->dominated_blocks_.Reset();
1105 other->dominator_ = nullptr;
1106
1107 // Clear the list of predecessors of `other` in preparation of deleting it.
1108 other->predecessors_.Reset();
1109
1110 // Delete `other` from the graph. The function updates reverse post order.
1111 graph_->DeleteDeadBlock(other);
1112 other->SetGraph(nullptr);
1113}
1114
1115void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1116 DCHECK_NE(GetGraph(), other->GetGraph());
1117 DCHECK(GetDominatedBlocks().IsEmpty());
1118 DCHECK(GetSuccessors().IsEmpty());
1119 DCHECK(!EndsWithControlFlowInstruction());
1120 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1121 DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock());
1122 DCHECK(other->GetPhis().IsEmpty());
1123 DCHECK(!other->IsInLoop());
1124
1125 // Move instructions from `other` to `this`.
1126 instructions_.Add(other->GetInstructions());
1127 other->instructions_.SetBlockOfInstructions(this);
1128
1129 // Update links to the successors of `other`.
1130 successors_.Reset();
1131 while (!other->successors_.IsEmpty()) {
1132 HBasicBlock* successor = other->successors_.Get(0);
1133 successor->ReplacePredecessor(other, this);
1134 }
1135
1136 // Update the dominator tree.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001137 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1138 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1139 dominated_blocks_.Add(dominated);
1140 dominated->SetDominator(this);
1141 }
1142 other->dominated_blocks_.Reset();
1143 other->dominator_ = nullptr;
1144 other->graph_ = nullptr;
1145}
1146
1147void HBasicBlock::ReplaceWith(HBasicBlock* other) {
1148 while (!GetPredecessors().IsEmpty()) {
1149 HBasicBlock* predecessor = GetPredecessors().Get(0);
1150 predecessor->ReplaceSuccessor(this, other);
1151 }
1152 while (!GetSuccessors().IsEmpty()) {
1153 HBasicBlock* successor = GetSuccessors().Get(0);
1154 successor->ReplacePredecessor(this, other);
1155 }
1156 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
1157 other->AddDominatedBlock(dominated_blocks_.Get(i));
1158 }
1159 GetDominator()->ReplaceDominatedBlock(this, other);
1160 other->SetDominator(GetDominator());
1161 dominator_ = nullptr;
1162 graph_ = nullptr;
1163}
1164
1165// Create space in `blocks` for adding `number_of_new_blocks` entries
1166// starting at location `at`. Blocks after `at` are moved accordingly.
1167static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
1168 size_t number_of_new_blocks,
1169 size_t at) {
1170 size_t old_size = blocks->Size();
1171 size_t new_size = old_size + number_of_new_blocks;
1172 blocks->SetSize(new_size);
1173 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
1174 blocks->Put(j, blocks->Get(i));
1175 }
1176}
1177
David Brazdil2d7352b2015-04-20 14:52:42 +01001178void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1179 DCHECK_EQ(block->GetGraph(), this);
1180 DCHECK(block->GetSuccessors().IsEmpty());
1181 DCHECK(block->GetPredecessors().IsEmpty());
1182 DCHECK(block->GetDominatedBlocks().IsEmpty());
1183 DCHECK(block->GetDominator() == nullptr);
1184
1185 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1186 block->RemoveInstruction(it.Current());
1187 }
1188 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1189 block->RemovePhi(it.Current()->AsPhi());
1190 }
1191
1192 reverse_post_order_.Delete(block);
1193 blocks_.Put(block->GetBlockId(), nullptr);
1194}
1195
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001196void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001197 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001198 // Simple case of an entry block, a body block, and an exit block.
1199 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001200 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001201 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
1202 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001203 DCHECK(!body->IsExitBlock());
1204 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001205
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001206 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1207 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001208
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001209 // Replace the invoke with the return value of the inlined graph.
1210 if (last->IsReturn()) {
1211 invoke->ReplaceWith(last->InputAt(0));
1212 } else {
1213 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001214 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001215
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001216 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001217 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001218 // Need to inline multiple blocks. We split `invoke`'s block
1219 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001220 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001221 // with the second half.
1222 ArenaAllocator* allocator = outer_graph->GetArena();
1223 HBasicBlock* at = invoke->GetBlock();
1224 HBasicBlock* to = at->SplitAfter(invoke);
1225
1226 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
1227 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001228 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001229 exit_block_->ReplaceWith(to);
1230
1231 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001232 // to not `HReturn` but `HGoto` instead.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001233 HInstruction* return_value = nullptr;
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001234 bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
1235 if (to->GetPredecessors().Size() == 1) {
1236 HBasicBlock* predecessor = to->GetPredecessors().Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001237 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001238 if (!returns_void) {
1239 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001240 }
1241 predecessor->AddInstruction(new (allocator) HGoto());
1242 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001243 } else {
1244 if (!returns_void) {
1245 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001246 return_value = new (allocator) HPhi(
1247 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001248 to->AddPhi(return_value->AsPhi());
1249 }
1250 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
1251 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
1252 HInstruction* last = predecessor->GetLastInstruction();
1253 if (!returns_void) {
1254 return_value->AsPhi()->AddInput(last->InputAt(0));
1255 }
1256 predecessor->AddInstruction(new (allocator) HGoto());
1257 predecessor->RemoveInstruction(last);
1258 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001259 }
1260
1261 if (return_value != nullptr) {
1262 invoke->ReplaceWith(return_value);
1263 }
1264
1265 // Update the meta information surrounding blocks:
1266 // (1) the graph they are now in,
1267 // (2) the reverse post order of that graph,
1268 // (3) the potential loop information they are now in.
1269
1270 // We don't add the entry block, the exit block, and the first block, which
1271 // has been merged with `at`.
1272 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1273
1274 // We add the `to` block.
1275 static constexpr int kNumberOfNewBlocksInCaller = 1;
1276 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
1277 + kNumberOfNewBlocksInCaller;
1278
1279 // Find the location of `at` in the outer graph's reverse post order. The new
1280 // blocks will be added after it.
1281 size_t index_of_at = 0;
1282 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
1283 index_of_at++;
1284 }
1285 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1286
1287 // Do a reverse post order of the blocks in the callee and do (1), (2),
1288 // and (3) to the blocks that apply.
1289 HLoopInformation* info = at->GetLoopInformation();
1290 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1291 HBasicBlock* current = it.Current();
1292 if (current != exit_block_ && current != entry_block_ && current != first) {
1293 DCHECK(!current->IsInLoop());
1294 DCHECK(current->GetGraph() == this);
1295 current->SetGraph(outer_graph);
1296 outer_graph->AddBlock(current);
1297 outer_graph->reverse_post_order_.Put(++index_of_at, current);
1298 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001299 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001300 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1301 loop_it.Current()->Add(current);
1302 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001303 }
1304 }
1305 }
1306
1307 // Do (1), (2), and (3) to `to`.
1308 to->SetGraph(outer_graph);
1309 outer_graph->AddBlock(to);
1310 outer_graph->reverse_post_order_.Put(++index_of_at, to);
1311 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001312 to->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001313 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1314 loop_it.Current()->Add(to);
1315 }
David Brazdil46e2a392015-03-16 17:31:52 +00001316 if (info->IsBackEdge(*at)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001317 // Only `to` can become a back edge, as the inlined blocks
1318 // are predecessors of `to`.
1319 info->ReplaceBackEdge(at, to);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001320 }
1321 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001322 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001323
David Brazdil05144f42015-04-16 15:18:00 +01001324 // Update the next instruction id of the outer graph, so that instructions
1325 // added later get bigger ids than those in the inner graph.
1326 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1327
1328 // Walk over the entry block and:
1329 // - Move constants from the entry block to the outer_graph's entry block,
1330 // - Replace HParameterValue instructions with their real value.
1331 // - Remove suspend checks, that hold an environment.
1332 // We must do this after the other blocks have been inlined, otherwise ids of
1333 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001334 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001335 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1336 HInstruction* current = it.Current();
1337 if (current->IsNullConstant()) {
1338 current->ReplaceWith(outer_graph->GetNullConstant());
1339 } else if (current->IsIntConstant()) {
1340 current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue()));
1341 } else if (current->IsLongConstant()) {
1342 current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue()));
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001343 } else if (current->IsFloatConstant()) {
1344 current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue()));
1345 } else if (current->IsDoubleConstant()) {
1346 current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue()));
David Brazdil05144f42015-04-16 15:18:00 +01001347 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001348 if (kIsDebugBuild
1349 && invoke->IsInvokeStaticOrDirect()
1350 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1351 // Ensure we do not use the last input of `invoke`, as it
1352 // contains a clinit check which is not an actual argument.
1353 size_t last_input_index = invoke->InputCount() - 1;
1354 DCHECK(parameter_index != last_input_index);
1355 }
David Brazdil05144f42015-04-16 15:18:00 +01001356 current->ReplaceWith(invoke->InputAt(parameter_index++));
1357 } else {
1358 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1359 entry_block_->RemoveInstruction(current);
1360 }
1361 }
1362
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001363 // Finally remove the invoke from the caller.
1364 invoke->GetBlock()->RemoveInstruction(invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001365}
1366
Calin Juravleacf735c2015-02-12 15:25:22 +00001367std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1368 ScopedObjectAccess soa(Thread::Current());
1369 os << "["
1370 << " is_top=" << rhs.IsTop()
1371 << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
1372 << " is_exact=" << rhs.IsExact()
1373 << " ]";
1374 return os;
1375}
1376
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001377} // namespace art