blob: d3ee770941c5164b7d2a28858cf7a44ad34410eb [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "nodes.h"
Calin Juravle77520bc2015-01-12 18:45:46 +000018
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010019#include "ssa_builder.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000020#include "utils/growable_array.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000021#include "scoped_thread_state_change.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000022
23namespace art {
24
25void HGraph::AddBlock(HBasicBlock* block) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000026 block->SetBlockId(blocks_.Size());
Nicolas Geoffray818f2102014-02-18 16:43:35 +000027 blocks_.Add(block);
28}
29
Nicolas Geoffray804d0932014-05-02 08:46:00 +010030void HGraph::FindBackEdges(ArenaBitVector* visited) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000031 ArenaBitVector visiting(arena_, blocks_.Size(), false);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032 VisitBlockForBackEdges(entry_block_, visited, &visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000033}
34
Roland Levillainfc600dc2014-12-02 17:16:31 +000035static void RemoveAsUser(HInstruction* instruction) {
36 for (size_t i = 0; i < instruction->InputCount(); i++) {
David Brazdil1abb4192015-02-17 18:33:36 +000037 instruction->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000038 }
39
40 HEnvironment* environment = instruction->GetEnvironment();
41 if (environment != nullptr) {
42 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
David Brazdil1abb4192015-02-17 18:33:36 +000043 if (environment->GetInstructionAt(i) != nullptr) {
44 environment->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000045 }
46 }
47 }
48}
49
50void HGraph::RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const {
51 for (size_t i = 0; i < blocks_.Size(); ++i) {
52 if (!visited.IsBitSet(i)) {
53 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010054 DCHECK(block->GetPhis().IsEmpty()) << "Phis are not inserted at this stage";
Roland Levillainfc600dc2014-12-02 17:16:31 +000055 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
56 RemoveAsUser(it.Current());
57 }
58 }
59 }
60}
61
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010062void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010063 for (size_t i = 0; i < blocks_.Size(); ++i) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000064 if (!visited.IsBitSet(i)) {
David Brazdil1abb4192015-02-17 18:33:36 +000065 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010066 // We only need to update the successor, which might be live.
David Brazdil1abb4192015-02-17 18:33:36 +000067 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
68 block->GetSuccessors().Get(j)->RemovePredecessor(block);
69 }
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010070 // Remove the block from the list of blocks, so that further analyses
71 // never see it.
72 blocks_.Put(i, nullptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073 }
74 }
75}
76
77void HGraph::VisitBlockForBackEdges(HBasicBlock* block,
78 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +010079 ArenaBitVector* visiting) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000080 int id = block->GetBlockId();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081 if (visited->IsBitSet(id)) return;
82
83 visited->SetBit(id);
84 visiting->SetBit(id);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010085 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
86 HBasicBlock* successor = block->GetSuccessors().Get(i);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000087 if (visiting->IsBitSet(successor->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000088 successor->AddBackEdge(block);
89 } else {
90 VisitBlockForBackEdges(successor, visited, visiting);
91 }
92 }
93 visiting->ClearBit(id);
94}
95
96void HGraph::BuildDominatorTree() {
97 ArenaBitVector visited(arena_, blocks_.Size(), false);
98
99 // (1) Find the back edges in the graph doing a DFS traversal.
100 FindBackEdges(&visited);
101
Roland Levillainfc600dc2014-12-02 17:16:31 +0000102 // (2) Remove instructions and phis from blocks not visited during
103 // the initial DFS as users from other instructions, so that
104 // users can be safely removed before uses later.
105 RemoveInstructionsAsUsersFromDeadBlocks(visited);
106
107 // (3) Remove blocks not visited during the initial DFS.
108 // Step (4) requires dead blocks to be removed from the
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000109 // predecessors list of live blocks.
110 RemoveDeadBlocks(visited);
111
Roland Levillainfc600dc2014-12-02 17:16:31 +0000112 // (4) Simplify the CFG now, so that we don't need to recompute
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100113 // dominators and the reverse post order.
114 SimplifyCFG();
115
Roland Levillainfc600dc2014-12-02 17:16:31 +0000116 // (5) Compute the immediate dominator of each block. We visit
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000117 // the successors of a block only when all its forward branches
118 // have been processed.
119 GrowableArray<size_t> visits(arena_, blocks_.Size());
120 visits.SetSize(blocks_.Size());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100121 reverse_post_order_.Add(entry_block_);
122 for (size_t i = 0; i < entry_block_->GetSuccessors().Size(); i++) {
123 VisitBlockForDominatorTree(entry_block_->GetSuccessors().Get(i), entry_block_, &visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000124 }
125}
126
127HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
128 ArenaBitVector visited(arena_, blocks_.Size(), false);
129 // Walk the dominator tree of the first block and mark the visited blocks.
130 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000131 visited.SetBit(first->GetBlockId());
132 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000133 }
134 // Walk the dominator tree of the second block until a marked block is found.
135 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000136 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000137 return second;
138 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000139 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000140 }
141 LOG(ERROR) << "Could not find common dominator";
142 return nullptr;
143}
144
145void HGraph::VisitBlockForDominatorTree(HBasicBlock* block,
146 HBasicBlock* predecessor,
147 GrowableArray<size_t>* visits) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000148 if (block->GetDominator() == nullptr) {
149 block->SetDominator(predecessor);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000150 } else {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000151 block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000152 }
153
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000154 visits->Increment(block->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000155 // Once all the forward edges have been visited, we know the immediate
156 // dominator of the block. We can then start visiting its successors.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000157 if (visits->Get(block->GetBlockId()) ==
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100158 block->GetPredecessors().Size() - block->NumberOfBackEdges()) {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100159 block->GetDominator()->AddDominatedBlock(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100160 reverse_post_order_.Add(block);
161 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
162 VisitBlockForDominatorTree(block->GetSuccessors().Get(i), block, visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000163 }
164 }
165}
166
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000167void HGraph::TransformToSsa() {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100168 DCHECK(!reverse_post_order_.IsEmpty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100169 SsaBuilder ssa_builder(this);
170 ssa_builder.BuildSsa();
171}
172
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100173void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) {
174 // Insert a new node between `block` and `successor` to split the
175 // critical edge.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100176 HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100177 AddBlock(new_block);
178 new_block->AddInstruction(new (arena_) HGoto());
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100179 block->ReplaceSuccessor(successor, new_block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100180 new_block->AddSuccessor(successor);
181 if (successor->IsLoopHeader()) {
182 // If we split at a back edge boundary, make the new block the back edge.
183 HLoopInformation* info = successor->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000184 if (info->IsBackEdge(*block)) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100185 info->RemoveBackEdge(block);
186 info->AddBackEdge(new_block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100187 }
188 }
189}
190
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100191void HGraph::SimplifyLoop(HBasicBlock* header) {
192 HLoopInformation* info = header->GetLoopInformation();
193
194 // If there are more than one back edge, make them branch to the same block that
195 // will become the only back edge. This simplifies finding natural loops in the
196 // graph.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100197 // Also, if the loop is a do/while (that is the back edge is an if), change the
198 // back edge to be a goto. This simplifies code generation of suspend cheks.
199 if (info->NumberOfBackEdges() > 1 || info->GetBackEdges().Get(0)->GetLastInstruction()->IsIf()) {
200 HBasicBlock* new_back_edge = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100201 AddBlock(new_back_edge);
202 new_back_edge->AddInstruction(new (arena_) HGoto());
203 for (size_t pred = 0, e = info->GetBackEdges().Size(); pred < e; ++pred) {
204 HBasicBlock* back_edge = info->GetBackEdges().Get(pred);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100205 back_edge->ReplaceSuccessor(header, new_back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100206 }
207 info->ClearBackEdges();
208 info->AddBackEdge(new_back_edge);
209 new_back_edge->AddSuccessor(header);
210 }
211
212 // Make sure the loop has only one pre header. This simplifies SSA building by having
213 // to just look at the pre header to know which locals are initialized at entry of the
214 // loop.
215 size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges();
216 if (number_of_incomings != 1) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100217 HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100218 AddBlock(pre_header);
219 pre_header->AddInstruction(new (arena_) HGoto());
220
221 ArenaBitVector back_edges(arena_, GetBlocks().Size(), false);
222 HBasicBlock* back_edge = info->GetBackEdges().Get(0);
223 for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) {
224 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
225 if (predecessor != back_edge) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100226 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100227 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100228 }
229 }
230 pre_header->AddSuccessor(header);
231 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100232
233 // Make sure the second predecessor of a loop header is the back edge.
234 if (header->GetPredecessors().Get(1) != info->GetBackEdges().Get(0)) {
235 header->SwapPredecessors();
236 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100237
238 // Place the suspend check at the beginning of the header, so that live registers
239 // will be known when allocating registers. Note that code generation can still
240 // generate the suspend check at the back edge, but needs to be careful with
241 // loop phi spill slots (which are not written to at back edge).
242 HInstruction* first_instruction = header->GetFirstInstruction();
243 if (!first_instruction->IsSuspendCheck()) {
244 HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc());
245 header->InsertInstructionBefore(check, first_instruction);
246 first_instruction = check;
247 }
248 info->SetSuspendCheck(first_instruction->AsSuspendCheck());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100249}
250
251void HGraph::SimplifyCFG() {
252 // Simplify the CFG for future analysis, and code generation:
253 // (1): Split critical edges.
254 // (2): Simplify loops by having only one back edge, and one preheader.
255 for (size_t i = 0; i < blocks_.Size(); ++i) {
256 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100257 if (block == nullptr) continue;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100258 if (block->GetSuccessors().Size() > 1) {
259 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
260 HBasicBlock* successor = block->GetSuccessors().Get(j);
261 if (successor->GetPredecessors().Size() > 1) {
262 SplitCriticalEdge(block, successor);
263 --j;
264 }
265 }
266 }
267 if (block->IsLoopHeader()) {
268 SimplifyLoop(block);
269 }
270 }
271}
272
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000273bool HGraph::AnalyzeNaturalLoops() const {
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100274 // Order does not matter.
275 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
276 HBasicBlock* block = it.Current();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100277 if (block->IsLoopHeader()) {
278 HLoopInformation* info = block->GetLoopInformation();
279 if (!info->Populate()) {
280 // Abort if the loop is non natural. We currently bailout in such cases.
281 return false;
282 }
283 }
284 }
285 return true;
286}
287
David Brazdil8d5b8b22015-03-24 10:51:52 +0000288void HGraph::InsertConstant(HConstant* constant) {
289 // New constants are inserted before the final control-flow instruction
290 // of the graph, or at its end if called from the graph builder.
291 if (entry_block_->EndsWithControlFlowInstruction()) {
292 entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction());
David Brazdil46e2a392015-03-16 17:31:52 +0000293 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000294 entry_block_->AddInstruction(constant);
David Brazdil46e2a392015-03-16 17:31:52 +0000295 }
296}
297
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000298HNullConstant* HGraph::GetNullConstant() {
299 if (cached_null_constant_ == nullptr) {
300 cached_null_constant_ = new (arena_) HNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000301 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000302 }
303 return cached_null_constant_;
304}
305
David Brazdil8d5b8b22015-03-24 10:51:52 +0000306HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value) {
307 switch (type) {
308 case Primitive::Type::kPrimBoolean:
309 DCHECK(IsUint<1>(value));
310 FALLTHROUGH_INTENDED;
311 case Primitive::Type::kPrimByte:
312 case Primitive::Type::kPrimChar:
313 case Primitive::Type::kPrimShort:
314 case Primitive::Type::kPrimInt:
315 DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value));
316 return GetIntConstant(static_cast<int32_t>(value));
317
318 case Primitive::Type::kPrimLong:
319 return GetLongConstant(value);
320
321 default:
322 LOG(FATAL) << "Unsupported constant type";
323 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000324 }
David Brazdil46e2a392015-03-16 17:31:52 +0000325}
326
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000327void HGraph::CacheFloatConstant(HFloatConstant* constant) {
328 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
329 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
330 cached_float_constants_.Overwrite(value, constant);
331}
332
333void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
334 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
335 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
336 cached_double_constants_.Overwrite(value, constant);
337}
338
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000339void HLoopInformation::Add(HBasicBlock* block) {
340 blocks_.SetBit(block->GetBlockId());
341}
342
David Brazdil46e2a392015-03-16 17:31:52 +0000343void HLoopInformation::Remove(HBasicBlock* block) {
344 blocks_.ClearBit(block->GetBlockId());
345}
346
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100347void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
348 if (blocks_.IsBitSet(block->GetBlockId())) {
349 return;
350 }
351
352 blocks_.SetBit(block->GetBlockId());
353 block->SetInLoop(this);
354 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
355 PopulateRecursive(block->GetPredecessors().Get(i));
356 }
357}
358
359bool HLoopInformation::Populate() {
360 DCHECK_EQ(GetBackEdges().Size(), 1u);
361 HBasicBlock* back_edge = GetBackEdges().Get(0);
362 DCHECK(back_edge->GetDominator() != nullptr);
363 if (!header_->Dominates(back_edge)) {
364 // This loop is not natural. Do not bother going further.
365 return false;
366 }
367
368 // Populate this loop: starting with the back edge, recursively add predecessors
369 // that are not already part of that loop. Set the header as part of the loop
370 // to end the recursion.
371 // This is a recursive implementation of the algorithm described in
372 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
373 blocks_.SetBit(header_->GetBlockId());
374 PopulateRecursive(back_edge);
375 return true;
376}
377
378HBasicBlock* HLoopInformation::GetPreHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100379 return header_->GetDominator();
380}
381
382bool HLoopInformation::Contains(const HBasicBlock& block) const {
383 return blocks_.IsBitSet(block.GetBlockId());
384}
385
386bool HLoopInformation::IsIn(const HLoopInformation& other) const {
387 return other.blocks_.IsBitSet(header_->GetBlockId());
388}
389
390bool HBasicBlock::Dominates(HBasicBlock* other) const {
391 // Walk up the dominator tree from `other`, to find out if `this`
392 // is an ancestor.
393 HBasicBlock* current = other;
394 while (current != nullptr) {
395 if (current == this) {
396 return true;
397 }
398 current = current->GetDominator();
399 }
400 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100401}
402
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100403static void UpdateInputsUsers(HInstruction* instruction) {
404 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
405 instruction->InputAt(i)->AddUseAt(instruction, i);
406 }
407 // Environment should be created later.
408 DCHECK(!instruction->HasEnvironment());
409}
410
Roland Levillainccc07a92014-09-16 14:48:16 +0100411void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
412 HInstruction* replacement) {
413 DCHECK(initial->GetBlock() == this);
414 InsertInstructionBefore(replacement, initial);
415 initial->ReplaceWith(replacement);
416 RemoveInstruction(initial);
417}
418
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100419static void Add(HInstructionList* instruction_list,
420 HBasicBlock* block,
421 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000422 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000423 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100424 instruction->SetBlock(block);
425 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100426 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100427 instruction_list->AddInstruction(instruction);
428}
429
430void HBasicBlock::AddInstruction(HInstruction* instruction) {
431 Add(&instructions_, this, instruction);
432}
433
434void HBasicBlock::AddPhi(HPhi* phi) {
435 Add(&phis_, this, phi);
436}
437
David Brazdilc3d743f2015-04-22 13:40:50 +0100438void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
439 DCHECK(!cursor->IsPhi());
440 DCHECK(!instruction->IsPhi());
441 DCHECK_EQ(instruction->GetId(), -1);
442 DCHECK_NE(cursor->GetId(), -1);
443 DCHECK_EQ(cursor->GetBlock(), this);
444 DCHECK(!instruction->IsControlFlow());
445 instruction->SetBlock(this);
446 instruction->SetId(GetGraph()->GetNextInstructionId());
447 UpdateInputsUsers(instruction);
448 instructions_.InsertInstructionBefore(instruction, cursor);
449}
450
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100451void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
452 DCHECK(!cursor->IsPhi());
453 DCHECK(!instruction->IsPhi());
454 DCHECK_EQ(instruction->GetId(), -1);
455 DCHECK_NE(cursor->GetId(), -1);
456 DCHECK_EQ(cursor->GetBlock(), this);
457 DCHECK(!instruction->IsControlFlow());
458 DCHECK(!cursor->IsControlFlow());
459 instruction->SetBlock(this);
460 instruction->SetId(GetGraph()->GetNextInstructionId());
461 UpdateInputsUsers(instruction);
462 instructions_.InsertInstructionAfter(instruction, cursor);
463}
464
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100465void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
466 DCHECK_EQ(phi->GetId(), -1);
467 DCHECK_NE(cursor->GetId(), -1);
468 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100469 phi->SetBlock(this);
470 phi->SetId(GetGraph()->GetNextInstructionId());
471 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100472 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100473}
474
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100475static void Remove(HInstructionList* instruction_list,
476 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000477 HInstruction* instruction,
478 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100479 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100480 instruction->SetBlock(nullptr);
481 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000482 if (ensure_safety) {
483 DCHECK(instruction->GetUses().IsEmpty());
484 DCHECK(instruction->GetEnvUses().IsEmpty());
485 RemoveAsUser(instruction);
486 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100487}
488
David Brazdil1abb4192015-02-17 18:33:36 +0000489void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100490 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000491 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100492}
493
David Brazdil1abb4192015-02-17 18:33:36 +0000494void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
495 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100496}
497
David Brazdilc7508e92015-04-27 13:28:57 +0100498void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
499 if (instruction->IsPhi()) {
500 RemovePhi(instruction->AsPhi(), ensure_safety);
501 } else {
502 RemoveInstruction(instruction, ensure_safety);
503 }
504}
505
David Brazdiled596192015-01-23 10:39:45 +0000506void HEnvironment::CopyFrom(HEnvironment* env) {
507 for (size_t i = 0; i < env->Size(); i++) {
508 HInstruction* instruction = env->GetInstructionAt(i);
509 SetRawEnvAt(i, instruction);
510 if (instruction != nullptr) {
511 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100512 }
David Brazdiled596192015-01-23 10:39:45 +0000513 }
514}
515
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700516void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
517 HBasicBlock* loop_header) {
518 DCHECK(loop_header->IsLoopHeader());
519 for (size_t i = 0; i < env->Size(); i++) {
520 HInstruction* instruction = env->GetInstructionAt(i);
521 SetRawEnvAt(i, instruction);
522 if (instruction == nullptr) {
523 continue;
524 }
525 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
526 // At the end of the loop pre-header, the corresponding value for instruction
527 // is the first input of the phi.
528 HInstruction* initial = instruction->AsPhi()->InputAt(0);
529 DCHECK(initial->GetBlock()->Dominates(loop_header));
530 SetRawEnvAt(i, initial);
531 initial->AddEnvUseAt(this, i);
532 } else {
533 instruction->AddEnvUseAt(this, i);
534 }
535 }
536}
537
David Brazdil1abb4192015-02-17 18:33:36 +0000538void HEnvironment::RemoveAsUserOfInput(size_t index) const {
539 const HUserRecord<HEnvironment*> user_record = vregs_.Get(index);
540 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100541}
542
Calin Juravle77520bc2015-01-12 18:45:46 +0000543HInstruction* HInstruction::GetNextDisregardingMoves() const {
544 HInstruction* next = GetNext();
545 while (next != nullptr && next->IsParallelMove()) {
546 next = next->GetNext();
547 }
548 return next;
549}
550
551HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
552 HInstruction* previous = GetPrevious();
553 while (previous != nullptr && previous->IsParallelMove()) {
554 previous = previous->GetPrevious();
555 }
556 return previous;
557}
558
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100559void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000560 if (first_instruction_ == nullptr) {
561 DCHECK(last_instruction_ == nullptr);
562 first_instruction_ = last_instruction_ = instruction;
563 } else {
564 last_instruction_->next_ = instruction;
565 instruction->previous_ = last_instruction_;
566 last_instruction_ = instruction;
567 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000568}
569
David Brazdilc3d743f2015-04-22 13:40:50 +0100570void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
571 DCHECK(Contains(cursor));
572 if (cursor == first_instruction_) {
573 cursor->previous_ = instruction;
574 instruction->next_ = cursor;
575 first_instruction_ = instruction;
576 } else {
577 instruction->previous_ = cursor->previous_;
578 instruction->next_ = cursor;
579 cursor->previous_ = instruction;
580 instruction->previous_->next_ = instruction;
581 }
582}
583
584void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
585 DCHECK(Contains(cursor));
586 if (cursor == last_instruction_) {
587 cursor->next_ = instruction;
588 instruction->previous_ = cursor;
589 last_instruction_ = instruction;
590 } else {
591 instruction->next_ = cursor->next_;
592 instruction->previous_ = cursor;
593 cursor->next_ = instruction;
594 instruction->next_->previous_ = instruction;
595 }
596}
597
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100598void HInstructionList::RemoveInstruction(HInstruction* instruction) {
599 if (instruction->previous_ != nullptr) {
600 instruction->previous_->next_ = instruction->next_;
601 }
602 if (instruction->next_ != nullptr) {
603 instruction->next_->previous_ = instruction->previous_;
604 }
605 if (instruction == first_instruction_) {
606 first_instruction_ = instruction->next_;
607 }
608 if (instruction == last_instruction_) {
609 last_instruction_ = instruction->previous_;
610 }
611}
612
Roland Levillain6b469232014-09-25 10:10:38 +0100613bool HInstructionList::Contains(HInstruction* instruction) const {
614 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
615 if (it.Current() == instruction) {
616 return true;
617 }
618 }
619 return false;
620}
621
Roland Levillainccc07a92014-09-16 14:48:16 +0100622bool HInstructionList::FoundBefore(const HInstruction* instruction1,
623 const HInstruction* instruction2) const {
624 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
625 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
626 if (it.Current() == instruction1) {
627 return true;
628 }
629 if (it.Current() == instruction2) {
630 return false;
631 }
632 }
633 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
634 return true;
635}
636
Roland Levillain6c82d402014-10-13 16:10:27 +0100637bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
638 if (other_instruction == this) {
639 // An instruction does not strictly dominate itself.
640 return false;
641 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100642 HBasicBlock* block = GetBlock();
643 HBasicBlock* other_block = other_instruction->GetBlock();
644 if (block != other_block) {
645 return GetBlock()->Dominates(other_instruction->GetBlock());
646 } else {
647 // If both instructions are in the same block, ensure this
648 // instruction comes before `other_instruction`.
649 if (IsPhi()) {
650 if (!other_instruction->IsPhi()) {
651 // Phis appear before non phi-instructions so this instruction
652 // dominates `other_instruction`.
653 return true;
654 } else {
655 // There is no order among phis.
656 LOG(FATAL) << "There is no dominance between phis of a same block.";
657 return false;
658 }
659 } else {
660 // `this` is not a phi.
661 if (other_instruction->IsPhi()) {
662 // Phis appear before non phi-instructions so this instruction
663 // does not dominate `other_instruction`.
664 return false;
665 } else {
666 // Check whether this instruction comes before
667 // `other_instruction` in the instruction list.
668 return block->GetInstructions().FoundBefore(this, other_instruction);
669 }
670 }
671 }
672}
673
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100675 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000676 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
677 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100678 HInstruction* user = current->GetUser();
679 size_t input_index = current->GetIndex();
680 user->SetRawInputAt(input_index, other);
681 other->AddUseAt(user, input_index);
682 }
683
David Brazdiled596192015-01-23 10:39:45 +0000684 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
685 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100686 HEnvironment* user = current->GetUser();
687 size_t input_index = current->GetIndex();
688 user->SetRawEnvAt(input_index, other);
689 other->AddEnvUseAt(user, input_index);
690 }
691
David Brazdiled596192015-01-23 10:39:45 +0000692 uses_.Clear();
693 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100694}
695
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100696void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000697 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100698 SetRawInputAt(index, replacement);
699 replacement->AddUseAt(this, index);
700}
701
Nicolas Geoffray39468442014-09-02 15:17:15 +0100702size_t HInstruction::EnvironmentSize() const {
703 return HasEnvironment() ? environment_->Size() : 0;
704}
705
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100706void HPhi::AddInput(HInstruction* input) {
707 DCHECK(input->GetBlock() != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +0000708 inputs_.Add(HUserRecord<HInstruction*>(input));
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100709 input->AddUseAt(this, inputs_.Size() - 1);
710}
711
David Brazdil2d7352b2015-04-20 14:52:42 +0100712void HPhi::RemoveInputAt(size_t index) {
713 RemoveAsUserOfInput(index);
714 inputs_.DeleteAt(index);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100715 for (size_t i = index, e = InputCount(); i < e; ++i) {
716 InputRecordAt(i).GetUseNode()->SetIndex(i);
717 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100718}
719
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100720#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000721void H##name::Accept(HGraphVisitor* visitor) { \
722 visitor->Visit##name(this); \
723}
724
725FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
726
727#undef DEFINE_ACCEPT
728
729void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100730 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
731 for (size_t i = 0 ; i < blocks.Size(); i++) {
David Brazdil46e2a392015-03-16 17:31:52 +0000732 HBasicBlock* block = blocks.Get(i);
733 if (block != nullptr) {
734 VisitBasicBlock(block);
735 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000736 }
737}
738
Roland Levillain633021e2014-10-01 14:12:25 +0100739void HGraphVisitor::VisitReversePostOrder() {
740 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
741 VisitBasicBlock(it.Current());
742 }
743}
744
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000745void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100746 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100747 it.Current()->Accept(this);
748 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100749 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000750 it.Current()->Accept(this);
751 }
752}
753
Roland Levillain9240d6a2014-10-20 16:47:04 +0100754HConstant* HUnaryOperation::TryStaticEvaluation() const {
755 if (GetInput()->IsIntConstant()) {
756 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000757 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain9240d6a2014-10-20 16:47:04 +0100758 } else if (GetInput()->IsLongConstant()) {
Roland Levillainb762d2e2014-10-22 10:11:06 +0100759 // TODO: Implement static evaluation of long unary operations.
760 //
761 // Do not exit with a fatal condition here. Instead, simply
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700762 // return `null' to notify the caller that this instruction
Roland Levillainb762d2e2014-10-22 10:11:06 +0100763 // cannot (yet) be statically evaluated.
Roland Levillain9240d6a2014-10-20 16:47:04 +0100764 return nullptr;
765 }
766 return nullptr;
767}
768
769HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +0100770 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
771 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
772 GetRight()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000773 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100774 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
775 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
776 GetRight()->AsLongConstant()->GetValue());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000777 if (GetResultType() == Primitive::kPrimLong) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000778 return GetBlock()->GetGraph()->GetLongConstant(value);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000779 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000780 DCHECK_EQ(GetResultType(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000781 return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000782 }
Roland Levillain556c3d12014-09-18 15:25:07 +0100783 }
784 return nullptr;
785}
Dave Allison20dfc792014-06-16 20:44:29 -0700786
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000787HConstant* HBinaryOperation::GetConstantRight() const {
788 if (GetRight()->IsConstant()) {
789 return GetRight()->AsConstant();
790 } else if (IsCommutative() && GetLeft()->IsConstant()) {
791 return GetLeft()->AsConstant();
792 } else {
793 return nullptr;
794 }
795}
796
797// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700798// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000799HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
800 HInstruction* most_constant_right = GetConstantRight();
801 if (most_constant_right == nullptr) {
802 return nullptr;
803 } else if (most_constant_right == GetLeft()) {
804 return GetRight();
805 } else {
806 return GetLeft();
807 }
808}
809
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700810bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
811 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100812}
813
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100814bool HInstruction::Equals(HInstruction* other) const {
815 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100816 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100817 if (!InstructionDataEquals(other)) return false;
818 if (GetType() != other->GetType()) return false;
819 if (InputCount() != other->InputCount()) return false;
820
821 for (size_t i = 0, e = InputCount(); i < e; ++i) {
822 if (InputAt(i) != other->InputAt(i)) return false;
823 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100824 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100825 return true;
826}
827
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700828std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
829#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
830 switch (rhs) {
831 FOR_EACH_INSTRUCTION(DECLARE_CASE)
832 default:
833 os << "Unknown instruction kind " << static_cast<int>(rhs);
834 break;
835 }
836#undef DECLARE_CASE
837 return os;
838}
839
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000840void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000841 next_->previous_ = previous_;
842 if (previous_ != nullptr) {
843 previous_->next_ = next_;
844 }
845 if (block_->instructions_.first_instruction_ == this) {
846 block_->instructions_.first_instruction_ = next_;
847 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000848 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000849
850 previous_ = cursor->previous_;
851 if (previous_ != nullptr) {
852 previous_->next_ = this;
853 }
854 next_ = cursor;
855 cursor->previous_ = this;
856 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000857
858 if (block_->instructions_.first_instruction_ == cursor) {
859 block_->instructions_.first_instruction_ = this;
860 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000861}
862
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000863HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
864 DCHECK(!cursor->IsControlFlow());
865 DCHECK_NE(instructions_.last_instruction_, cursor);
866 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000867
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000868 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
869 new_block->instructions_.first_instruction_ = cursor->GetNext();
870 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
871 cursor->next_->previous_ = nullptr;
872 cursor->next_ = nullptr;
873 instructions_.last_instruction_ = cursor;
874
875 new_block->instructions_.SetBlockOfInstructions(new_block);
876 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
877 HBasicBlock* successor = GetSuccessors().Get(i);
878 new_block->successors_.Add(successor);
879 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
880 }
881 successors_.Reset();
882
883 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
884 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
885 dominated->dominator_ = new_block;
886 new_block->dominated_blocks_.Add(dominated);
887 }
888 dominated_blocks_.Reset();
889 return new_block;
890}
891
David Brazdil46e2a392015-03-16 17:31:52 +0000892bool HBasicBlock::IsSingleGoto() const {
893 HLoopInformation* loop_info = GetLoopInformation();
894 // TODO: Remove the null check b/19084197.
895 return GetFirstInstruction() != nullptr
896 && GetPhis().IsEmpty()
897 && GetFirstInstruction() == GetLastInstruction()
898 && GetLastInstruction()->IsGoto()
899 // Back edges generate the suspend check.
900 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
901}
902
David Brazdil8d5b8b22015-03-24 10:51:52 +0000903bool HBasicBlock::EndsWithControlFlowInstruction() const {
904 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
905}
906
David Brazdilb2bd1c52015-03-25 11:17:37 +0000907bool HBasicBlock::EndsWithIf() const {
908 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
909}
910
911bool HBasicBlock::HasSinglePhi() const {
912 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
913}
914
David Brazdil2d7352b2015-04-20 14:52:42 +0100915size_t HInstructionList::CountSize() const {
916 size_t size = 0;
917 HInstruction* current = first_instruction_;
918 for (; current != nullptr; current = current->GetNext()) {
919 size++;
920 }
921 return size;
922}
923
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000924void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
925 for (HInstruction* current = first_instruction_;
926 current != nullptr;
927 current = current->GetNext()) {
928 current->SetBlock(block);
929 }
930}
931
932void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
933 DCHECK(Contains(cursor));
934 if (!instruction_list.IsEmpty()) {
935 if (cursor == last_instruction_) {
936 last_instruction_ = instruction_list.last_instruction_;
937 } else {
938 cursor->next_->previous_ = instruction_list.last_instruction_;
939 }
940 instruction_list.last_instruction_->next_ = cursor->next_;
941 cursor->next_ = instruction_list.first_instruction_;
942 instruction_list.first_instruction_->previous_ = cursor;
943 }
944}
945
946void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +0000947 if (IsEmpty()) {
948 first_instruction_ = instruction_list.first_instruction_;
949 last_instruction_ = instruction_list.last_instruction_;
950 } else {
951 AddAfter(last_instruction_, instruction_list);
952 }
953}
954
David Brazdil2d7352b2015-04-20 14:52:42 +0100955void HBasicBlock::DisconnectAndDelete() {
956 // Dominators must be removed after all the blocks they dominate. This way
957 // a loop header is removed last, a requirement for correct loop information
958 // iteration.
959 DCHECK(dominated_blocks_.IsEmpty());
David Brazdil46e2a392015-03-16 17:31:52 +0000960
David Brazdil2d7352b2015-04-20 14:52:42 +0100961 // Remove the block from all loops it is included in.
962 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
963 HLoopInformation* loop_info = it.Current();
964 loop_info->Remove(this);
965 if (loop_info->IsBackEdge(*this)) {
966 // This deliberately leaves the loop in an inconsistent state and will
967 // fail SSAChecker unless the entire loop is removed during the pass.
968 loop_info->RemoveBackEdge(this);
969 }
970 }
971
972 // Disconnect the block from its predecessors and update their control-flow
973 // instructions.
David Brazdil46e2a392015-03-16 17:31:52 +0000974 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
David Brazdil2d7352b2015-04-20 14:52:42 +0100975 HBasicBlock* predecessor = predecessors_.Get(i);
976 HInstruction* last_instruction = predecessor->GetLastInstruction();
977 predecessor->RemoveInstruction(last_instruction);
978 predecessor->RemoveSuccessor(this);
979 if (predecessor->GetSuccessors().Size() == 1u) {
980 DCHECK(last_instruction->IsIf());
981 predecessor->AddInstruction(new (graph_->GetArena()) HGoto());
982 } else {
983 // The predecessor has no remaining successors and therefore must be dead.
984 // We deliberately leave it without a control-flow instruction so that the
985 // SSAChecker fails unless it is not removed during the pass too.
986 DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u);
987 }
David Brazdil46e2a392015-03-16 17:31:52 +0000988 }
David Brazdil46e2a392015-03-16 17:31:52 +0000989 predecessors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +0100990
991 // Disconnect the block from its successors and update their dominators
992 // and phis.
993 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
994 HBasicBlock* successor = successors_.Get(i);
995 // Delete this block from the list of predecessors.
996 size_t this_index = successor->GetPredecessorIndexOf(this);
997 successor->predecessors_.DeleteAt(this_index);
998
999 // Check that `successor` has other predecessors, otherwise `this` is the
1000 // dominator of `successor` which violates the order DCHECKed at the top.
1001 DCHECK(!successor->predecessors_.IsEmpty());
1002
1003 // Recompute the successor's dominator.
1004 HBasicBlock* old_dominator = successor->GetDominator();
1005 HBasicBlock* new_dominator = successor->predecessors_.Get(0);
1006 for (size_t j = 1, f = successor->predecessors_.Size(); j < f; ++j) {
1007 new_dominator = graph_->FindCommonDominator(
1008 new_dominator, successor->predecessors_.Get(j));
1009 }
1010 if (old_dominator != new_dominator) {
1011 successor->SetDominator(new_dominator);
1012 old_dominator->RemoveDominatedBlock(successor);
1013 new_dominator->AddDominatedBlock(successor);
1014 }
1015
1016 // Remove this block's entries in the successor's phis.
1017 if (successor->predecessors_.Size() == 1u) {
1018 // The successor has just one predecessor left. Replace phis with the only
1019 // remaining input.
1020 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1021 HPhi* phi = phi_it.Current()->AsPhi();
1022 phi->ReplaceWith(phi->InputAt(1 - this_index));
1023 successor->RemovePhi(phi);
1024 }
1025 } else {
1026 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1027 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1028 }
1029 }
1030 }
David Brazdil46e2a392015-03-16 17:31:52 +00001031 successors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001032
1033 // Disconnect from the dominator.
1034 dominator_->RemoveDominatedBlock(this);
1035 SetDominator(nullptr);
1036
1037 // Delete from the graph. The function safely deletes remaining instructions
1038 // and updates the reverse post order.
1039 graph_->DeleteDeadBlock(this);
1040 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001041}
1042
David Brazdil69a28042015-04-29 17:16:07 +01001043void HBasicBlock::UpdateLoopInformation() {
1044 // Check if loop information points to a dismantled loop. If so, replace with
1045 // the loop information of a larger loop which contains this block, or nullptr
1046 // otherwise. We iterate in case the larger loop has been destroyed too.
1047 while (IsInLoop() && loop_information_->GetBackEdges().IsEmpty()) {
1048 if (IsLoopHeader()) {
1049 HSuspendCheck* suspend_check = loop_information_->GetSuspendCheck();
1050 DCHECK_EQ(suspend_check->GetBlock(), this);
1051 RemoveInstruction(suspend_check);
1052 }
1053 loop_information_ = loop_information_->GetPreHeader()->GetLoopInformation();
1054 }
1055}
1056
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001057void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001058 DCHECK_EQ(GetGraph(), other->GetGraph());
1059 DCHECK(GetDominatedBlocks().Contains(other));
1060 DCHECK_EQ(GetSuccessors().Size(), 1u);
1061 DCHECK_EQ(GetSuccessors().Get(0), other);
1062 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1063 DCHECK_EQ(other->GetPredecessors().Get(0), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001064 DCHECK(other->GetPhis().IsEmpty());
1065
David Brazdil2d7352b2015-04-20 14:52:42 +01001066 // Move instructions from `other` to `this`.
1067 DCHECK(EndsWithControlFlowInstruction());
1068 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001069 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001070 other->instructions_.SetBlockOfInstructions(this);
1071 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072
David Brazdil2d7352b2015-04-20 14:52:42 +01001073 // Remove `other` from the loops it is included in.
1074 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1075 HLoopInformation* loop_info = it.Current();
1076 loop_info->Remove(other);
1077 if (loop_info->IsBackEdge(*other)) {
1078 loop_info->ClearBackEdges();
1079 loop_info->AddBackEdge(this);
1080 }
1081 }
1082
1083 // Update links to the successors of `other`.
1084 successors_.Reset();
1085 while (!other->successors_.IsEmpty()) {
1086 HBasicBlock* successor = other->successors_.Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001087 successor->ReplacePredecessor(other, this);
1088 }
1089
David Brazdil2d7352b2015-04-20 14:52:42 +01001090 // Update the dominator tree.
1091 dominated_blocks_.Delete(other);
1092 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1093 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1094 dominated_blocks_.Add(dominated);
1095 dominated->SetDominator(this);
1096 }
1097 other->dominated_blocks_.Reset();
1098 other->dominator_ = nullptr;
1099
1100 // Clear the list of predecessors of `other` in preparation of deleting it.
1101 other->predecessors_.Reset();
1102
1103 // Delete `other` from the graph. The function updates reverse post order.
1104 graph_->DeleteDeadBlock(other);
1105 other->SetGraph(nullptr);
1106}
1107
1108void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1109 DCHECK_NE(GetGraph(), other->GetGraph());
1110 DCHECK(GetDominatedBlocks().IsEmpty());
1111 DCHECK(GetSuccessors().IsEmpty());
1112 DCHECK(!EndsWithControlFlowInstruction());
1113 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1114 DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock());
1115 DCHECK(other->GetPhis().IsEmpty());
1116 DCHECK(!other->IsInLoop());
1117
1118 // Move instructions from `other` to `this`.
1119 instructions_.Add(other->GetInstructions());
1120 other->instructions_.SetBlockOfInstructions(this);
1121
1122 // Update links to the successors of `other`.
1123 successors_.Reset();
1124 while (!other->successors_.IsEmpty()) {
1125 HBasicBlock* successor = other->successors_.Get(0);
1126 successor->ReplacePredecessor(other, this);
1127 }
1128
1129 // Update the dominator tree.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001130 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1131 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1132 dominated_blocks_.Add(dominated);
1133 dominated->SetDominator(this);
1134 }
1135 other->dominated_blocks_.Reset();
1136 other->dominator_ = nullptr;
1137 other->graph_ = nullptr;
1138}
1139
1140void HBasicBlock::ReplaceWith(HBasicBlock* other) {
1141 while (!GetPredecessors().IsEmpty()) {
1142 HBasicBlock* predecessor = GetPredecessors().Get(0);
1143 predecessor->ReplaceSuccessor(this, other);
1144 }
1145 while (!GetSuccessors().IsEmpty()) {
1146 HBasicBlock* successor = GetSuccessors().Get(0);
1147 successor->ReplacePredecessor(this, other);
1148 }
1149 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
1150 other->AddDominatedBlock(dominated_blocks_.Get(i));
1151 }
1152 GetDominator()->ReplaceDominatedBlock(this, other);
1153 other->SetDominator(GetDominator());
1154 dominator_ = nullptr;
1155 graph_ = nullptr;
1156}
1157
1158// Create space in `blocks` for adding `number_of_new_blocks` entries
1159// starting at location `at`. Blocks after `at` are moved accordingly.
1160static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
1161 size_t number_of_new_blocks,
1162 size_t at) {
1163 size_t old_size = blocks->Size();
1164 size_t new_size = old_size + number_of_new_blocks;
1165 blocks->SetSize(new_size);
1166 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
1167 blocks->Put(j, blocks->Get(i));
1168 }
1169}
1170
David Brazdil2d7352b2015-04-20 14:52:42 +01001171void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1172 DCHECK_EQ(block->GetGraph(), this);
1173 DCHECK(block->GetSuccessors().IsEmpty());
1174 DCHECK(block->GetPredecessors().IsEmpty());
1175 DCHECK(block->GetDominatedBlocks().IsEmpty());
1176 DCHECK(block->GetDominator() == nullptr);
1177
1178 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1179 block->RemoveInstruction(it.Current());
1180 }
1181 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1182 block->RemovePhi(it.Current()->AsPhi());
1183 }
1184
1185 reverse_post_order_.Delete(block);
1186 blocks_.Put(block->GetBlockId(), nullptr);
1187}
1188
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001189void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001190 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001191 // Simple case of an entry block, a body block, and an exit block.
1192 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001193 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001194 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
1195 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001196 DCHECK(!body->IsExitBlock());
1197 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001198
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001199 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1200 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001201
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001202 // Replace the invoke with the return value of the inlined graph.
1203 if (last->IsReturn()) {
1204 invoke->ReplaceWith(last->InputAt(0));
1205 } else {
1206 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001207 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001208
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001209 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001210 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001211 // Need to inline multiple blocks. We split `invoke`'s block
1212 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001213 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001214 // with the second half.
1215 ArenaAllocator* allocator = outer_graph->GetArena();
1216 HBasicBlock* at = invoke->GetBlock();
1217 HBasicBlock* to = at->SplitAfter(invoke);
1218
1219 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
1220 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001221 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001222 exit_block_->ReplaceWith(to);
1223
1224 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001225 // to not `HReturn` but `HGoto` instead.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001226 HInstruction* return_value = nullptr;
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001227 bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
1228 if (to->GetPredecessors().Size() == 1) {
1229 HBasicBlock* predecessor = to->GetPredecessors().Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001230 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001231 if (!returns_void) {
1232 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001233 }
1234 predecessor->AddInstruction(new (allocator) HGoto());
1235 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001236 } else {
1237 if (!returns_void) {
1238 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001239 return_value = new (allocator) HPhi(
1240 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001241 to->AddPhi(return_value->AsPhi());
1242 }
1243 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
1244 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
1245 HInstruction* last = predecessor->GetLastInstruction();
1246 if (!returns_void) {
1247 return_value->AsPhi()->AddInput(last->InputAt(0));
1248 }
1249 predecessor->AddInstruction(new (allocator) HGoto());
1250 predecessor->RemoveInstruction(last);
1251 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001252 }
1253
1254 if (return_value != nullptr) {
1255 invoke->ReplaceWith(return_value);
1256 }
1257
1258 // Update the meta information surrounding blocks:
1259 // (1) the graph they are now in,
1260 // (2) the reverse post order of that graph,
1261 // (3) the potential loop information they are now in.
1262
1263 // We don't add the entry block, the exit block, and the first block, which
1264 // has been merged with `at`.
1265 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1266
1267 // We add the `to` block.
1268 static constexpr int kNumberOfNewBlocksInCaller = 1;
1269 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
1270 + kNumberOfNewBlocksInCaller;
1271
1272 // Find the location of `at` in the outer graph's reverse post order. The new
1273 // blocks will be added after it.
1274 size_t index_of_at = 0;
1275 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
1276 index_of_at++;
1277 }
1278 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1279
1280 // Do a reverse post order of the blocks in the callee and do (1), (2),
1281 // and (3) to the blocks that apply.
1282 HLoopInformation* info = at->GetLoopInformation();
1283 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1284 HBasicBlock* current = it.Current();
1285 if (current != exit_block_ && current != entry_block_ && current != first) {
1286 DCHECK(!current->IsInLoop());
1287 DCHECK(current->GetGraph() == this);
1288 current->SetGraph(outer_graph);
1289 outer_graph->AddBlock(current);
1290 outer_graph->reverse_post_order_.Put(++index_of_at, current);
1291 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001292 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001293 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1294 loop_it.Current()->Add(current);
1295 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001296 }
1297 }
1298 }
1299
1300 // Do (1), (2), and (3) to `to`.
1301 to->SetGraph(outer_graph);
1302 outer_graph->AddBlock(to);
1303 outer_graph->reverse_post_order_.Put(++index_of_at, to);
1304 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001305 to->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001306 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1307 loop_it.Current()->Add(to);
1308 }
David Brazdil46e2a392015-03-16 17:31:52 +00001309 if (info->IsBackEdge(*at)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001310 // Only `at` can become a back edge, as the inlined blocks
1311 // are predecessors of `at`.
1312 DCHECK_EQ(1u, info->NumberOfBackEdges());
1313 info->ClearBackEdges();
1314 info->AddBackEdge(to);
1315 }
1316 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001317 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001318
David Brazdil05144f42015-04-16 15:18:00 +01001319 // Update the next instruction id of the outer graph, so that instructions
1320 // added later get bigger ids than those in the inner graph.
1321 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1322
1323 // Walk over the entry block and:
1324 // - Move constants from the entry block to the outer_graph's entry block,
1325 // - Replace HParameterValue instructions with their real value.
1326 // - Remove suspend checks, that hold an environment.
1327 // We must do this after the other blocks have been inlined, otherwise ids of
1328 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001329 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001330 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1331 HInstruction* current = it.Current();
1332 if (current->IsNullConstant()) {
1333 current->ReplaceWith(outer_graph->GetNullConstant());
1334 } else if (current->IsIntConstant()) {
1335 current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue()));
1336 } else if (current->IsLongConstant()) {
1337 current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue()));
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001338 } else if (current->IsFloatConstant()) {
1339 current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue()));
1340 } else if (current->IsDoubleConstant()) {
1341 current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue()));
David Brazdil05144f42015-04-16 15:18:00 +01001342 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001343 if (kIsDebugBuild
1344 && invoke->IsInvokeStaticOrDirect()
1345 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1346 // Ensure we do not use the last input of `invoke`, as it
1347 // contains a clinit check which is not an actual argument.
1348 size_t last_input_index = invoke->InputCount() - 1;
1349 DCHECK(parameter_index != last_input_index);
1350 }
David Brazdil05144f42015-04-16 15:18:00 +01001351 current->ReplaceWith(invoke->InputAt(parameter_index++));
1352 } else {
1353 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1354 entry_block_->RemoveInstruction(current);
1355 }
1356 }
1357
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001358 // Finally remove the invoke from the caller.
1359 invoke->GetBlock()->RemoveInstruction(invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001360}
1361
Calin Juravleacf735c2015-02-12 15:25:22 +00001362std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1363 ScopedObjectAccess soa(Thread::Current());
1364 os << "["
1365 << " is_top=" << rhs.IsTop()
1366 << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
1367 << " is_exact=" << rhs.IsExact()
1368 << " ]";
1369 return os;
1370}
1371
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001372} // namespace art