blob: ab56aff32f41cc5be33da0e901bb000f50516956 [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
David Brazdiled596192015-01-23 10:39:45 +0000503void HEnvironment::CopyFrom(HEnvironment* env) {
504 for (size_t i = 0; i < env->Size(); i++) {
505 HInstruction* instruction = env->GetInstructionAt(i);
506 SetRawEnvAt(i, instruction);
507 if (instruction != nullptr) {
508 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100509 }
David Brazdiled596192015-01-23 10:39:45 +0000510 }
511}
512
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700513void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
514 HBasicBlock* loop_header) {
515 DCHECK(loop_header->IsLoopHeader());
516 for (size_t i = 0; i < env->Size(); i++) {
517 HInstruction* instruction = env->GetInstructionAt(i);
518 SetRawEnvAt(i, instruction);
519 if (instruction == nullptr) {
520 continue;
521 }
522 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
523 // At the end of the loop pre-header, the corresponding value for instruction
524 // is the first input of the phi.
525 HInstruction* initial = instruction->AsPhi()->InputAt(0);
526 DCHECK(initial->GetBlock()->Dominates(loop_header));
527 SetRawEnvAt(i, initial);
528 initial->AddEnvUseAt(this, i);
529 } else {
530 instruction->AddEnvUseAt(this, i);
531 }
532 }
533}
534
David Brazdil1abb4192015-02-17 18:33:36 +0000535void HEnvironment::RemoveAsUserOfInput(size_t index) const {
536 const HUserRecord<HEnvironment*> user_record = vregs_.Get(index);
537 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100538}
539
Calin Juravle77520bc2015-01-12 18:45:46 +0000540HInstruction* HInstruction::GetNextDisregardingMoves() const {
541 HInstruction* next = GetNext();
542 while (next != nullptr && next->IsParallelMove()) {
543 next = next->GetNext();
544 }
545 return next;
546}
547
548HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
549 HInstruction* previous = GetPrevious();
550 while (previous != nullptr && previous->IsParallelMove()) {
551 previous = previous->GetPrevious();
552 }
553 return previous;
554}
555
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100556void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000557 if (first_instruction_ == nullptr) {
558 DCHECK(last_instruction_ == nullptr);
559 first_instruction_ = last_instruction_ = instruction;
560 } else {
561 last_instruction_->next_ = instruction;
562 instruction->previous_ = last_instruction_;
563 last_instruction_ = instruction;
564 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000565}
566
David Brazdilc3d743f2015-04-22 13:40:50 +0100567void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
568 DCHECK(Contains(cursor));
569 if (cursor == first_instruction_) {
570 cursor->previous_ = instruction;
571 instruction->next_ = cursor;
572 first_instruction_ = instruction;
573 } else {
574 instruction->previous_ = cursor->previous_;
575 instruction->next_ = cursor;
576 cursor->previous_ = instruction;
577 instruction->previous_->next_ = instruction;
578 }
579}
580
581void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
582 DCHECK(Contains(cursor));
583 if (cursor == last_instruction_) {
584 cursor->next_ = instruction;
585 instruction->previous_ = cursor;
586 last_instruction_ = instruction;
587 } else {
588 instruction->next_ = cursor->next_;
589 instruction->previous_ = cursor;
590 cursor->next_ = instruction;
591 instruction->next_->previous_ = instruction;
592 }
593}
594
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100595void HInstructionList::RemoveInstruction(HInstruction* instruction) {
596 if (instruction->previous_ != nullptr) {
597 instruction->previous_->next_ = instruction->next_;
598 }
599 if (instruction->next_ != nullptr) {
600 instruction->next_->previous_ = instruction->previous_;
601 }
602 if (instruction == first_instruction_) {
603 first_instruction_ = instruction->next_;
604 }
605 if (instruction == last_instruction_) {
606 last_instruction_ = instruction->previous_;
607 }
608}
609
Roland Levillain6b469232014-09-25 10:10:38 +0100610bool HInstructionList::Contains(HInstruction* instruction) const {
611 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
612 if (it.Current() == instruction) {
613 return true;
614 }
615 }
616 return false;
617}
618
Roland Levillainccc07a92014-09-16 14:48:16 +0100619bool HInstructionList::FoundBefore(const HInstruction* instruction1,
620 const HInstruction* instruction2) const {
621 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
622 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
623 if (it.Current() == instruction1) {
624 return true;
625 }
626 if (it.Current() == instruction2) {
627 return false;
628 }
629 }
630 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
631 return true;
632}
633
Roland Levillain6c82d402014-10-13 16:10:27 +0100634bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
635 if (other_instruction == this) {
636 // An instruction does not strictly dominate itself.
637 return false;
638 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100639 HBasicBlock* block = GetBlock();
640 HBasicBlock* other_block = other_instruction->GetBlock();
641 if (block != other_block) {
642 return GetBlock()->Dominates(other_instruction->GetBlock());
643 } else {
644 // If both instructions are in the same block, ensure this
645 // instruction comes before `other_instruction`.
646 if (IsPhi()) {
647 if (!other_instruction->IsPhi()) {
648 // Phis appear before non phi-instructions so this instruction
649 // dominates `other_instruction`.
650 return true;
651 } else {
652 // There is no order among phis.
653 LOG(FATAL) << "There is no dominance between phis of a same block.";
654 return false;
655 }
656 } else {
657 // `this` is not a phi.
658 if (other_instruction->IsPhi()) {
659 // Phis appear before non phi-instructions so this instruction
660 // does not dominate `other_instruction`.
661 return false;
662 } else {
663 // Check whether this instruction comes before
664 // `other_instruction` in the instruction list.
665 return block->GetInstructions().FoundBefore(this, other_instruction);
666 }
667 }
668 }
669}
670
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100671void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100672 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000673 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
674 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100675 HInstruction* user = current->GetUser();
676 size_t input_index = current->GetIndex();
677 user->SetRawInputAt(input_index, other);
678 other->AddUseAt(user, input_index);
679 }
680
David Brazdiled596192015-01-23 10:39:45 +0000681 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
682 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100683 HEnvironment* user = current->GetUser();
684 size_t input_index = current->GetIndex();
685 user->SetRawEnvAt(input_index, other);
686 other->AddEnvUseAt(user, input_index);
687 }
688
David Brazdiled596192015-01-23 10:39:45 +0000689 uses_.Clear();
690 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100691}
692
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100693void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000694 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100695 SetRawInputAt(index, replacement);
696 replacement->AddUseAt(this, index);
697}
698
Nicolas Geoffray39468442014-09-02 15:17:15 +0100699size_t HInstruction::EnvironmentSize() const {
700 return HasEnvironment() ? environment_->Size() : 0;
701}
702
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100703void HPhi::AddInput(HInstruction* input) {
704 DCHECK(input->GetBlock() != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +0000705 inputs_.Add(HUserRecord<HInstruction*>(input));
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100706 input->AddUseAt(this, inputs_.Size() - 1);
707}
708
David Brazdil2d7352b2015-04-20 14:52:42 +0100709void HPhi::RemoveInputAt(size_t index) {
710 RemoveAsUserOfInput(index);
711 inputs_.DeleteAt(index);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100712 for (size_t i = index, e = InputCount(); i < e; ++i) {
713 InputRecordAt(i).GetUseNode()->SetIndex(i);
714 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100715}
716
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100717#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000718void H##name::Accept(HGraphVisitor* visitor) { \
719 visitor->Visit##name(this); \
720}
721
722FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
723
724#undef DEFINE_ACCEPT
725
726void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100727 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
728 for (size_t i = 0 ; i < blocks.Size(); i++) {
David Brazdil46e2a392015-03-16 17:31:52 +0000729 HBasicBlock* block = blocks.Get(i);
730 if (block != nullptr) {
731 VisitBasicBlock(block);
732 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000733 }
734}
735
Roland Levillain633021e2014-10-01 14:12:25 +0100736void HGraphVisitor::VisitReversePostOrder() {
737 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
738 VisitBasicBlock(it.Current());
739 }
740}
741
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000742void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100743 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100744 it.Current()->Accept(this);
745 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100746 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000747 it.Current()->Accept(this);
748 }
749}
750
Roland Levillain9240d6a2014-10-20 16:47:04 +0100751HConstant* HUnaryOperation::TryStaticEvaluation() const {
752 if (GetInput()->IsIntConstant()) {
753 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000754 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain9240d6a2014-10-20 16:47:04 +0100755 } else if (GetInput()->IsLongConstant()) {
Roland Levillainb762d2e2014-10-22 10:11:06 +0100756 // TODO: Implement static evaluation of long unary operations.
757 //
758 // Do not exit with a fatal condition here. Instead, simply
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700759 // return `null' to notify the caller that this instruction
Roland Levillainb762d2e2014-10-22 10:11:06 +0100760 // cannot (yet) be statically evaluated.
Roland Levillain9240d6a2014-10-20 16:47:04 +0100761 return nullptr;
762 }
763 return nullptr;
764}
765
766HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +0100767 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
768 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
769 GetRight()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000770 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100771 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
772 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
773 GetRight()->AsLongConstant()->GetValue());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000774 if (GetResultType() == Primitive::kPrimLong) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000775 return GetBlock()->GetGraph()->GetLongConstant(value);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000776 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000777 DCHECK_EQ(GetResultType(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000778 return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000779 }
Roland Levillain556c3d12014-09-18 15:25:07 +0100780 }
781 return nullptr;
782}
Dave Allison20dfc792014-06-16 20:44:29 -0700783
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000784HConstant* HBinaryOperation::GetConstantRight() const {
785 if (GetRight()->IsConstant()) {
786 return GetRight()->AsConstant();
787 } else if (IsCommutative() && GetLeft()->IsConstant()) {
788 return GetLeft()->AsConstant();
789 } else {
790 return nullptr;
791 }
792}
793
794// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700795// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000796HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
797 HInstruction* most_constant_right = GetConstantRight();
798 if (most_constant_right == nullptr) {
799 return nullptr;
800 } else if (most_constant_right == GetLeft()) {
801 return GetRight();
802 } else {
803 return GetLeft();
804 }
805}
806
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700807bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
808 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100809}
810
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100811bool HInstruction::Equals(HInstruction* other) const {
812 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100813 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100814 if (!InstructionDataEquals(other)) return false;
815 if (GetType() != other->GetType()) return false;
816 if (InputCount() != other->InputCount()) return false;
817
818 for (size_t i = 0, e = InputCount(); i < e; ++i) {
819 if (InputAt(i) != other->InputAt(i)) return false;
820 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100821 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100822 return true;
823}
824
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700825std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
826#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
827 switch (rhs) {
828 FOR_EACH_INSTRUCTION(DECLARE_CASE)
829 default:
830 os << "Unknown instruction kind " << static_cast<int>(rhs);
831 break;
832 }
833#undef DECLARE_CASE
834 return os;
835}
836
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000837void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000838 next_->previous_ = previous_;
839 if (previous_ != nullptr) {
840 previous_->next_ = next_;
841 }
842 if (block_->instructions_.first_instruction_ == this) {
843 block_->instructions_.first_instruction_ = next_;
844 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000845 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000846
847 previous_ = cursor->previous_;
848 if (previous_ != nullptr) {
849 previous_->next_ = this;
850 }
851 next_ = cursor;
852 cursor->previous_ = this;
853 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000854
855 if (block_->instructions_.first_instruction_ == cursor) {
856 block_->instructions_.first_instruction_ = this;
857 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000858}
859
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000860HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
861 DCHECK(!cursor->IsControlFlow());
862 DCHECK_NE(instructions_.last_instruction_, cursor);
863 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000864
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000865 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
866 new_block->instructions_.first_instruction_ = cursor->GetNext();
867 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
868 cursor->next_->previous_ = nullptr;
869 cursor->next_ = nullptr;
870 instructions_.last_instruction_ = cursor;
871
872 new_block->instructions_.SetBlockOfInstructions(new_block);
873 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
874 HBasicBlock* successor = GetSuccessors().Get(i);
875 new_block->successors_.Add(successor);
876 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
877 }
878 successors_.Reset();
879
880 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
881 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
882 dominated->dominator_ = new_block;
883 new_block->dominated_blocks_.Add(dominated);
884 }
885 dominated_blocks_.Reset();
886 return new_block;
887}
888
David Brazdil46e2a392015-03-16 17:31:52 +0000889bool HBasicBlock::IsSingleGoto() const {
890 HLoopInformation* loop_info = GetLoopInformation();
891 // TODO: Remove the null check b/19084197.
892 return GetFirstInstruction() != nullptr
893 && GetPhis().IsEmpty()
894 && GetFirstInstruction() == GetLastInstruction()
895 && GetLastInstruction()->IsGoto()
896 // Back edges generate the suspend check.
897 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
898}
899
David Brazdil8d5b8b22015-03-24 10:51:52 +0000900bool HBasicBlock::EndsWithControlFlowInstruction() const {
901 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
902}
903
David Brazdilb2bd1c52015-03-25 11:17:37 +0000904bool HBasicBlock::EndsWithIf() const {
905 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
906}
907
908bool HBasicBlock::HasSinglePhi() const {
909 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
910}
911
David Brazdil2d7352b2015-04-20 14:52:42 +0100912size_t HInstructionList::CountSize() const {
913 size_t size = 0;
914 HInstruction* current = first_instruction_;
915 for (; current != nullptr; current = current->GetNext()) {
916 size++;
917 }
918 return size;
919}
920
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000921void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
922 for (HInstruction* current = first_instruction_;
923 current != nullptr;
924 current = current->GetNext()) {
925 current->SetBlock(block);
926 }
927}
928
929void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
930 DCHECK(Contains(cursor));
931 if (!instruction_list.IsEmpty()) {
932 if (cursor == last_instruction_) {
933 last_instruction_ = instruction_list.last_instruction_;
934 } else {
935 cursor->next_->previous_ = instruction_list.last_instruction_;
936 }
937 instruction_list.last_instruction_->next_ = cursor->next_;
938 cursor->next_ = instruction_list.first_instruction_;
939 instruction_list.first_instruction_->previous_ = cursor;
940 }
941}
942
943void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +0000944 if (IsEmpty()) {
945 first_instruction_ = instruction_list.first_instruction_;
946 last_instruction_ = instruction_list.last_instruction_;
947 } else {
948 AddAfter(last_instruction_, instruction_list);
949 }
950}
951
David Brazdil2d7352b2015-04-20 14:52:42 +0100952void HBasicBlock::DisconnectAndDelete() {
953 // Dominators must be removed after all the blocks they dominate. This way
954 // a loop header is removed last, a requirement for correct loop information
955 // iteration.
956 DCHECK(dominated_blocks_.IsEmpty());
David Brazdil46e2a392015-03-16 17:31:52 +0000957
David Brazdil2d7352b2015-04-20 14:52:42 +0100958 // Remove the block from all loops it is included in.
959 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
960 HLoopInformation* loop_info = it.Current();
961 loop_info->Remove(this);
962 if (loop_info->IsBackEdge(*this)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100963 // If this was the last back edge of the loop, we deliberately leave the
964 // loop in an inconsistent state and will fail SSAChecker unless the
965 // entire loop is removed during the pass.
David Brazdil2d7352b2015-04-20 14:52:42 +0100966 loop_info->RemoveBackEdge(this);
967 }
968 }
969
970 // Disconnect the block from its predecessors and update their control-flow
971 // instructions.
David Brazdil46e2a392015-03-16 17:31:52 +0000972 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
David Brazdil2d7352b2015-04-20 14:52:42 +0100973 HBasicBlock* predecessor = predecessors_.Get(i);
974 HInstruction* last_instruction = predecessor->GetLastInstruction();
975 predecessor->RemoveInstruction(last_instruction);
976 predecessor->RemoveSuccessor(this);
977 if (predecessor->GetSuccessors().Size() == 1u) {
978 DCHECK(last_instruction->IsIf());
979 predecessor->AddInstruction(new (graph_->GetArena()) HGoto());
980 } else {
981 // The predecessor has no remaining successors and therefore must be dead.
982 // We deliberately leave it without a control-flow instruction so that the
983 // SSAChecker fails unless it is not removed during the pass too.
984 DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u);
985 }
David Brazdil46e2a392015-03-16 17:31:52 +0000986 }
David Brazdil46e2a392015-03-16 17:31:52 +0000987 predecessors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +0100988
989 // Disconnect the block from its successors and update their dominators
990 // and phis.
991 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
992 HBasicBlock* successor = successors_.Get(i);
993 // Delete this block from the list of predecessors.
994 size_t this_index = successor->GetPredecessorIndexOf(this);
995 successor->predecessors_.DeleteAt(this_index);
996
997 // Check that `successor` has other predecessors, otherwise `this` is the
998 // dominator of `successor` which violates the order DCHECKed at the top.
999 DCHECK(!successor->predecessors_.IsEmpty());
1000
1001 // Recompute the successor's dominator.
1002 HBasicBlock* old_dominator = successor->GetDominator();
1003 HBasicBlock* new_dominator = successor->predecessors_.Get(0);
1004 for (size_t j = 1, f = successor->predecessors_.Size(); j < f; ++j) {
1005 new_dominator = graph_->FindCommonDominator(
1006 new_dominator, successor->predecessors_.Get(j));
1007 }
1008 if (old_dominator != new_dominator) {
1009 successor->SetDominator(new_dominator);
1010 old_dominator->RemoveDominatedBlock(successor);
1011 new_dominator->AddDominatedBlock(successor);
1012 }
1013
1014 // Remove this block's entries in the successor's phis.
1015 if (successor->predecessors_.Size() == 1u) {
1016 // The successor has just one predecessor left. Replace phis with the only
1017 // remaining input.
1018 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1019 HPhi* phi = phi_it.Current()->AsPhi();
1020 phi->ReplaceWith(phi->InputAt(1 - this_index));
1021 successor->RemovePhi(phi);
1022 }
1023 } else {
1024 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1025 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1026 }
1027 }
1028 }
David Brazdil46e2a392015-03-16 17:31:52 +00001029 successors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001030
1031 // Disconnect from the dominator.
1032 dominator_->RemoveDominatedBlock(this);
1033 SetDominator(nullptr);
1034
1035 // Delete from the graph. The function safely deletes remaining instructions
1036 // and updates the reverse post order.
1037 graph_->DeleteDeadBlock(this);
1038 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001039}
1040
David Brazdil69a28042015-04-29 17:16:07 +01001041void HBasicBlock::UpdateLoopInformation() {
1042 // Check if loop information points to a dismantled loop. If so, replace with
1043 // the loop information of a larger loop which contains this block, or nullptr
1044 // otherwise. We iterate in case the larger loop has been destroyed too.
1045 while (IsInLoop() && loop_information_->GetBackEdges().IsEmpty()) {
1046 if (IsLoopHeader()) {
1047 HSuspendCheck* suspend_check = loop_information_->GetSuspendCheck();
1048 DCHECK_EQ(suspend_check->GetBlock(), this);
1049 RemoveInstruction(suspend_check);
1050 }
1051 loop_information_ = loop_information_->GetPreHeader()->GetLoopInformation();
1052 }
1053}
1054
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001055void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001056 DCHECK_EQ(GetGraph(), other->GetGraph());
1057 DCHECK(GetDominatedBlocks().Contains(other));
1058 DCHECK_EQ(GetSuccessors().Size(), 1u);
1059 DCHECK_EQ(GetSuccessors().Get(0), other);
1060 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1061 DCHECK_EQ(other->GetPredecessors().Get(0), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001062 DCHECK(other->GetPhis().IsEmpty());
1063
David Brazdil2d7352b2015-04-20 14:52:42 +01001064 // Move instructions from `other` to `this`.
1065 DCHECK(EndsWithControlFlowInstruction());
1066 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001067 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001068 other->instructions_.SetBlockOfInstructions(this);
1069 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001070
David Brazdil2d7352b2015-04-20 14:52:42 +01001071 // Remove `other` from the loops it is included in.
1072 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1073 HLoopInformation* loop_info = it.Current();
1074 loop_info->Remove(other);
1075 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001076 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01001077 }
1078 }
1079
1080 // Update links to the successors of `other`.
1081 successors_.Reset();
1082 while (!other->successors_.IsEmpty()) {
1083 HBasicBlock* successor = other->successors_.Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001084 successor->ReplacePredecessor(other, this);
1085 }
1086
David Brazdil2d7352b2015-04-20 14:52:42 +01001087 // Update the dominator tree.
1088 dominated_blocks_.Delete(other);
1089 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1090 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1091 dominated_blocks_.Add(dominated);
1092 dominated->SetDominator(this);
1093 }
1094 other->dominated_blocks_.Reset();
1095 other->dominator_ = nullptr;
1096
1097 // Clear the list of predecessors of `other` in preparation of deleting it.
1098 other->predecessors_.Reset();
1099
1100 // Delete `other` from the graph. The function updates reverse post order.
1101 graph_->DeleteDeadBlock(other);
1102 other->SetGraph(nullptr);
1103}
1104
1105void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1106 DCHECK_NE(GetGraph(), other->GetGraph());
1107 DCHECK(GetDominatedBlocks().IsEmpty());
1108 DCHECK(GetSuccessors().IsEmpty());
1109 DCHECK(!EndsWithControlFlowInstruction());
1110 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1111 DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock());
1112 DCHECK(other->GetPhis().IsEmpty());
1113 DCHECK(!other->IsInLoop());
1114
1115 // Move instructions from `other` to `this`.
1116 instructions_.Add(other->GetInstructions());
1117 other->instructions_.SetBlockOfInstructions(this);
1118
1119 // Update links to the successors of `other`.
1120 successors_.Reset();
1121 while (!other->successors_.IsEmpty()) {
1122 HBasicBlock* successor = other->successors_.Get(0);
1123 successor->ReplacePredecessor(other, this);
1124 }
1125
1126 // Update the dominator tree.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001127 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1128 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1129 dominated_blocks_.Add(dominated);
1130 dominated->SetDominator(this);
1131 }
1132 other->dominated_blocks_.Reset();
1133 other->dominator_ = nullptr;
1134 other->graph_ = nullptr;
1135}
1136
1137void HBasicBlock::ReplaceWith(HBasicBlock* other) {
1138 while (!GetPredecessors().IsEmpty()) {
1139 HBasicBlock* predecessor = GetPredecessors().Get(0);
1140 predecessor->ReplaceSuccessor(this, other);
1141 }
1142 while (!GetSuccessors().IsEmpty()) {
1143 HBasicBlock* successor = GetSuccessors().Get(0);
1144 successor->ReplacePredecessor(this, other);
1145 }
1146 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
1147 other->AddDominatedBlock(dominated_blocks_.Get(i));
1148 }
1149 GetDominator()->ReplaceDominatedBlock(this, other);
1150 other->SetDominator(GetDominator());
1151 dominator_ = nullptr;
1152 graph_ = nullptr;
1153}
1154
1155// Create space in `blocks` for adding `number_of_new_blocks` entries
1156// starting at location `at`. Blocks after `at` are moved accordingly.
1157static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
1158 size_t number_of_new_blocks,
1159 size_t at) {
1160 size_t old_size = blocks->Size();
1161 size_t new_size = old_size + number_of_new_blocks;
1162 blocks->SetSize(new_size);
1163 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
1164 blocks->Put(j, blocks->Get(i));
1165 }
1166}
1167
David Brazdil2d7352b2015-04-20 14:52:42 +01001168void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1169 DCHECK_EQ(block->GetGraph(), this);
1170 DCHECK(block->GetSuccessors().IsEmpty());
1171 DCHECK(block->GetPredecessors().IsEmpty());
1172 DCHECK(block->GetDominatedBlocks().IsEmpty());
1173 DCHECK(block->GetDominator() == nullptr);
1174
1175 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1176 block->RemoveInstruction(it.Current());
1177 }
1178 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1179 block->RemovePhi(it.Current()->AsPhi());
1180 }
1181
1182 reverse_post_order_.Delete(block);
1183 blocks_.Put(block->GetBlockId(), nullptr);
1184}
1185
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001186void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001187 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001188 // Simple case of an entry block, a body block, and an exit block.
1189 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001190 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001191 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
1192 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001193 DCHECK(!body->IsExitBlock());
1194 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001195
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001196 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1197 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001198
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001199 // Replace the invoke with the return value of the inlined graph.
1200 if (last->IsReturn()) {
1201 invoke->ReplaceWith(last->InputAt(0));
1202 } else {
1203 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001204 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001205
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001206 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001207 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001208 // Need to inline multiple blocks. We split `invoke`'s block
1209 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001210 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001211 // with the second half.
1212 ArenaAllocator* allocator = outer_graph->GetArena();
1213 HBasicBlock* at = invoke->GetBlock();
1214 HBasicBlock* to = at->SplitAfter(invoke);
1215
1216 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
1217 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001218 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001219 exit_block_->ReplaceWith(to);
1220
1221 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001222 // to not `HReturn` but `HGoto` instead.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001223 HInstruction* return_value = nullptr;
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001224 bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
1225 if (to->GetPredecessors().Size() == 1) {
1226 HBasicBlock* predecessor = to->GetPredecessors().Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001227 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001228 if (!returns_void) {
1229 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001230 }
1231 predecessor->AddInstruction(new (allocator) HGoto());
1232 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001233 } else {
1234 if (!returns_void) {
1235 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001236 return_value = new (allocator) HPhi(
1237 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001238 to->AddPhi(return_value->AsPhi());
1239 }
1240 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
1241 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
1242 HInstruction* last = predecessor->GetLastInstruction();
1243 if (!returns_void) {
1244 return_value->AsPhi()->AddInput(last->InputAt(0));
1245 }
1246 predecessor->AddInstruction(new (allocator) HGoto());
1247 predecessor->RemoveInstruction(last);
1248 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001249 }
1250
1251 if (return_value != nullptr) {
1252 invoke->ReplaceWith(return_value);
1253 }
1254
1255 // Update the meta information surrounding blocks:
1256 // (1) the graph they are now in,
1257 // (2) the reverse post order of that graph,
1258 // (3) the potential loop information they are now in.
1259
1260 // We don't add the entry block, the exit block, and the first block, which
1261 // has been merged with `at`.
1262 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1263
1264 // We add the `to` block.
1265 static constexpr int kNumberOfNewBlocksInCaller = 1;
1266 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
1267 + kNumberOfNewBlocksInCaller;
1268
1269 // Find the location of `at` in the outer graph's reverse post order. The new
1270 // blocks will be added after it.
1271 size_t index_of_at = 0;
1272 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
1273 index_of_at++;
1274 }
1275 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1276
1277 // Do a reverse post order of the blocks in the callee and do (1), (2),
1278 // and (3) to the blocks that apply.
1279 HLoopInformation* info = at->GetLoopInformation();
1280 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1281 HBasicBlock* current = it.Current();
1282 if (current != exit_block_ && current != entry_block_ && current != first) {
1283 DCHECK(!current->IsInLoop());
1284 DCHECK(current->GetGraph() == this);
1285 current->SetGraph(outer_graph);
1286 outer_graph->AddBlock(current);
1287 outer_graph->reverse_post_order_.Put(++index_of_at, current);
1288 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001289 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001290 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1291 loop_it.Current()->Add(current);
1292 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001293 }
1294 }
1295 }
1296
1297 // Do (1), (2), and (3) to `to`.
1298 to->SetGraph(outer_graph);
1299 outer_graph->AddBlock(to);
1300 outer_graph->reverse_post_order_.Put(++index_of_at, to);
1301 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001302 to->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001303 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1304 loop_it.Current()->Add(to);
1305 }
David Brazdil46e2a392015-03-16 17:31:52 +00001306 if (info->IsBackEdge(*at)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001307 // Only `to` can become a back edge, as the inlined blocks
1308 // are predecessors of `to`.
1309 info->ReplaceBackEdge(at, to);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001310 }
1311 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001312 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001313
David Brazdil05144f42015-04-16 15:18:00 +01001314 // Update the next instruction id of the outer graph, so that instructions
1315 // added later get bigger ids than those in the inner graph.
1316 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1317
1318 // Walk over the entry block and:
1319 // - Move constants from the entry block to the outer_graph's entry block,
1320 // - Replace HParameterValue instructions with their real value.
1321 // - Remove suspend checks, that hold an environment.
1322 // We must do this after the other blocks have been inlined, otherwise ids of
1323 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001324 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001325 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1326 HInstruction* current = it.Current();
1327 if (current->IsNullConstant()) {
1328 current->ReplaceWith(outer_graph->GetNullConstant());
1329 } else if (current->IsIntConstant()) {
1330 current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue()));
1331 } else if (current->IsLongConstant()) {
1332 current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue()));
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001333 } else if (current->IsFloatConstant()) {
1334 current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue()));
1335 } else if (current->IsDoubleConstant()) {
1336 current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue()));
David Brazdil05144f42015-04-16 15:18:00 +01001337 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001338 if (kIsDebugBuild
1339 && invoke->IsInvokeStaticOrDirect()
1340 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1341 // Ensure we do not use the last input of `invoke`, as it
1342 // contains a clinit check which is not an actual argument.
1343 size_t last_input_index = invoke->InputCount() - 1;
1344 DCHECK(parameter_index != last_input_index);
1345 }
David Brazdil05144f42015-04-16 15:18:00 +01001346 current->ReplaceWith(invoke->InputAt(parameter_index++));
1347 } else {
1348 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1349 entry_block_->RemoveInstruction(current);
1350 }
1351 }
1352
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001353 // Finally remove the invoke from the caller.
1354 invoke->GetBlock()->RemoveInstruction(invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001355}
1356
Calin Juravleacf735c2015-02-12 15:25:22 +00001357std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1358 ScopedObjectAccess soa(Thread::Current());
1359 os << "["
1360 << " is_top=" << rhs.IsTop()
1361 << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
1362 << " is_exact=" << rhs.IsExact()
1363 << " ]";
1364 return os;
1365}
1366
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001367} // namespace art