blob: 699987c05e3f64b1ad554522f946fb42373c73e5 [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 {
379 DCHECK_EQ(header_->GetPredecessors().Size(), 2u);
380 return header_->GetDominator();
381}
382
383bool HLoopInformation::Contains(const HBasicBlock& block) const {
384 return blocks_.IsBitSet(block.GetBlockId());
385}
386
387bool HLoopInformation::IsIn(const HLoopInformation& other) const {
388 return other.blocks_.IsBitSet(header_->GetBlockId());
389}
390
391bool HBasicBlock::Dominates(HBasicBlock* other) const {
392 // Walk up the dominator tree from `other`, to find out if `this`
393 // is an ancestor.
394 HBasicBlock* current = other;
395 while (current != nullptr) {
396 if (current == this) {
397 return true;
398 }
399 current = current->GetDominator();
400 }
401 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100402}
403
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100404static void UpdateInputsUsers(HInstruction* instruction) {
405 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
406 instruction->InputAt(i)->AddUseAt(instruction, i);
407 }
408 // Environment should be created later.
409 DCHECK(!instruction->HasEnvironment());
410}
411
Roland Levillainccc07a92014-09-16 14:48:16 +0100412void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
413 HInstruction* replacement) {
414 DCHECK(initial->GetBlock() == this);
415 InsertInstructionBefore(replacement, initial);
416 initial->ReplaceWith(replacement);
417 RemoveInstruction(initial);
418}
419
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100420static void Add(HInstructionList* instruction_list,
421 HBasicBlock* block,
422 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000423 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000424 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100425 instruction->SetBlock(block);
426 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100427 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100428 instruction_list->AddInstruction(instruction);
429}
430
431void HBasicBlock::AddInstruction(HInstruction* instruction) {
432 Add(&instructions_, this, instruction);
433}
434
435void HBasicBlock::AddPhi(HPhi* phi) {
436 Add(&phis_, this, phi);
437}
438
David Brazdilc3d743f2015-04-22 13:40:50 +0100439void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
440 DCHECK(!cursor->IsPhi());
441 DCHECK(!instruction->IsPhi());
442 DCHECK_EQ(instruction->GetId(), -1);
443 DCHECK_NE(cursor->GetId(), -1);
444 DCHECK_EQ(cursor->GetBlock(), this);
445 DCHECK(!instruction->IsControlFlow());
446 instruction->SetBlock(this);
447 instruction->SetId(GetGraph()->GetNextInstructionId());
448 UpdateInputsUsers(instruction);
449 instructions_.InsertInstructionBefore(instruction, cursor);
450}
451
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100452void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
453 DCHECK(!cursor->IsPhi());
454 DCHECK(!instruction->IsPhi());
455 DCHECK_EQ(instruction->GetId(), -1);
456 DCHECK_NE(cursor->GetId(), -1);
457 DCHECK_EQ(cursor->GetBlock(), this);
458 DCHECK(!instruction->IsControlFlow());
459 DCHECK(!cursor->IsControlFlow());
460 instruction->SetBlock(this);
461 instruction->SetId(GetGraph()->GetNextInstructionId());
462 UpdateInputsUsers(instruction);
463 instructions_.InsertInstructionAfter(instruction, cursor);
464}
465
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100466void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
467 DCHECK_EQ(phi->GetId(), -1);
468 DCHECK_NE(cursor->GetId(), -1);
469 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100470 phi->SetBlock(this);
471 phi->SetId(GetGraph()->GetNextInstructionId());
472 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100473 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100474}
475
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100476static void Remove(HInstructionList* instruction_list,
477 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000478 HInstruction* instruction,
479 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100480 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100481 instruction->SetBlock(nullptr);
482 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000483 if (ensure_safety) {
484 DCHECK(instruction->GetUses().IsEmpty());
485 DCHECK(instruction->GetEnvUses().IsEmpty());
486 RemoveAsUser(instruction);
487 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100488}
489
David Brazdil1abb4192015-02-17 18:33:36 +0000490void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100491 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000492 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100493}
494
David Brazdil1abb4192015-02-17 18:33:36 +0000495void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
496 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100497}
498
David Brazdilc7508e92015-04-27 13:28:57 +0100499void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
500 if (instruction->IsPhi()) {
501 RemovePhi(instruction->AsPhi(), ensure_safety);
502 } else {
503 RemoveInstruction(instruction, ensure_safety);
504 }
505}
506
David Brazdiled596192015-01-23 10:39:45 +0000507void HEnvironment::CopyFrom(HEnvironment* env) {
508 for (size_t i = 0; i < env->Size(); i++) {
509 HInstruction* instruction = env->GetInstructionAt(i);
510 SetRawEnvAt(i, instruction);
511 if (instruction != nullptr) {
512 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100513 }
David Brazdiled596192015-01-23 10:39:45 +0000514 }
515}
516
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700517void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
518 HBasicBlock* loop_header) {
519 DCHECK(loop_header->IsLoopHeader());
520 for (size_t i = 0; i < env->Size(); i++) {
521 HInstruction* instruction = env->GetInstructionAt(i);
522 SetRawEnvAt(i, instruction);
523 if (instruction == nullptr) {
524 continue;
525 }
526 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
527 // At the end of the loop pre-header, the corresponding value for instruction
528 // is the first input of the phi.
529 HInstruction* initial = instruction->AsPhi()->InputAt(0);
530 DCHECK(initial->GetBlock()->Dominates(loop_header));
531 SetRawEnvAt(i, initial);
532 initial->AddEnvUseAt(this, i);
533 } else {
534 instruction->AddEnvUseAt(this, i);
535 }
536 }
537}
538
David Brazdil1abb4192015-02-17 18:33:36 +0000539void HEnvironment::RemoveAsUserOfInput(size_t index) const {
540 const HUserRecord<HEnvironment*> user_record = vregs_.Get(index);
541 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100542}
543
Calin Juravle77520bc2015-01-12 18:45:46 +0000544HInstruction* HInstruction::GetNextDisregardingMoves() const {
545 HInstruction* next = GetNext();
546 while (next != nullptr && next->IsParallelMove()) {
547 next = next->GetNext();
548 }
549 return next;
550}
551
552HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
553 HInstruction* previous = GetPrevious();
554 while (previous != nullptr && previous->IsParallelMove()) {
555 previous = previous->GetPrevious();
556 }
557 return previous;
558}
559
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100560void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000561 if (first_instruction_ == nullptr) {
562 DCHECK(last_instruction_ == nullptr);
563 first_instruction_ = last_instruction_ = instruction;
564 } else {
565 last_instruction_->next_ = instruction;
566 instruction->previous_ = last_instruction_;
567 last_instruction_ = instruction;
568 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000569}
570
David Brazdilc3d743f2015-04-22 13:40:50 +0100571void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
572 DCHECK(Contains(cursor));
573 if (cursor == first_instruction_) {
574 cursor->previous_ = instruction;
575 instruction->next_ = cursor;
576 first_instruction_ = instruction;
577 } else {
578 instruction->previous_ = cursor->previous_;
579 instruction->next_ = cursor;
580 cursor->previous_ = instruction;
581 instruction->previous_->next_ = instruction;
582 }
583}
584
585void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
586 DCHECK(Contains(cursor));
587 if (cursor == last_instruction_) {
588 cursor->next_ = instruction;
589 instruction->previous_ = cursor;
590 last_instruction_ = instruction;
591 } else {
592 instruction->next_ = cursor->next_;
593 instruction->previous_ = cursor;
594 cursor->next_ = instruction;
595 instruction->next_->previous_ = instruction;
596 }
597}
598
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100599void HInstructionList::RemoveInstruction(HInstruction* instruction) {
600 if (instruction->previous_ != nullptr) {
601 instruction->previous_->next_ = instruction->next_;
602 }
603 if (instruction->next_ != nullptr) {
604 instruction->next_->previous_ = instruction->previous_;
605 }
606 if (instruction == first_instruction_) {
607 first_instruction_ = instruction->next_;
608 }
609 if (instruction == last_instruction_) {
610 last_instruction_ = instruction->previous_;
611 }
612}
613
Roland Levillain6b469232014-09-25 10:10:38 +0100614bool HInstructionList::Contains(HInstruction* instruction) const {
615 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
616 if (it.Current() == instruction) {
617 return true;
618 }
619 }
620 return false;
621}
622
Roland Levillainccc07a92014-09-16 14:48:16 +0100623bool HInstructionList::FoundBefore(const HInstruction* instruction1,
624 const HInstruction* instruction2) const {
625 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
626 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
627 if (it.Current() == instruction1) {
628 return true;
629 }
630 if (it.Current() == instruction2) {
631 return false;
632 }
633 }
634 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
635 return true;
636}
637
Roland Levillain6c82d402014-10-13 16:10:27 +0100638bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
639 if (other_instruction == this) {
640 // An instruction does not strictly dominate itself.
641 return false;
642 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100643 HBasicBlock* block = GetBlock();
644 HBasicBlock* other_block = other_instruction->GetBlock();
645 if (block != other_block) {
646 return GetBlock()->Dominates(other_instruction->GetBlock());
647 } else {
648 // If both instructions are in the same block, ensure this
649 // instruction comes before `other_instruction`.
650 if (IsPhi()) {
651 if (!other_instruction->IsPhi()) {
652 // Phis appear before non phi-instructions so this instruction
653 // dominates `other_instruction`.
654 return true;
655 } else {
656 // There is no order among phis.
657 LOG(FATAL) << "There is no dominance between phis of a same block.";
658 return false;
659 }
660 } else {
661 // `this` is not a phi.
662 if (other_instruction->IsPhi()) {
663 // Phis appear before non phi-instructions so this instruction
664 // does not dominate `other_instruction`.
665 return false;
666 } else {
667 // Check whether this instruction comes before
668 // `other_instruction` in the instruction list.
669 return block->GetInstructions().FoundBefore(this, other_instruction);
670 }
671 }
672 }
673}
674
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100675void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100676 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000677 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
678 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100679 HInstruction* user = current->GetUser();
680 size_t input_index = current->GetIndex();
681 user->SetRawInputAt(input_index, other);
682 other->AddUseAt(user, input_index);
683 }
684
David Brazdiled596192015-01-23 10:39:45 +0000685 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
686 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100687 HEnvironment* user = current->GetUser();
688 size_t input_index = current->GetIndex();
689 user->SetRawEnvAt(input_index, other);
690 other->AddEnvUseAt(user, input_index);
691 }
692
David Brazdiled596192015-01-23 10:39:45 +0000693 uses_.Clear();
694 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100695}
696
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100697void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000698 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100699 SetRawInputAt(index, replacement);
700 replacement->AddUseAt(this, index);
701}
702
Nicolas Geoffray39468442014-09-02 15:17:15 +0100703size_t HInstruction::EnvironmentSize() const {
704 return HasEnvironment() ? environment_->Size() : 0;
705}
706
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100707void HPhi::AddInput(HInstruction* input) {
708 DCHECK(input->GetBlock() != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +0000709 inputs_.Add(HUserRecord<HInstruction*>(input));
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100710 input->AddUseAt(this, inputs_.Size() - 1);
711}
712
David Brazdil2d7352b2015-04-20 14:52:42 +0100713void HPhi::RemoveInputAt(size_t index) {
714 RemoveAsUserOfInput(index);
715 inputs_.DeleteAt(index);
716}
717
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100718#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000719void H##name::Accept(HGraphVisitor* visitor) { \
720 visitor->Visit##name(this); \
721}
722
723FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
724
725#undef DEFINE_ACCEPT
726
727void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100728 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
729 for (size_t i = 0 ; i < blocks.Size(); i++) {
David Brazdil46e2a392015-03-16 17:31:52 +0000730 HBasicBlock* block = blocks.Get(i);
731 if (block != nullptr) {
732 VisitBasicBlock(block);
733 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000734 }
735}
736
Roland Levillain633021e2014-10-01 14:12:25 +0100737void HGraphVisitor::VisitReversePostOrder() {
738 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
739 VisitBasicBlock(it.Current());
740 }
741}
742
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000743void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100744 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100745 it.Current()->Accept(this);
746 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100747 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000748 it.Current()->Accept(this);
749 }
750}
751
Roland Levillain9240d6a2014-10-20 16:47:04 +0100752HConstant* HUnaryOperation::TryStaticEvaluation() const {
753 if (GetInput()->IsIntConstant()) {
754 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000755 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain9240d6a2014-10-20 16:47:04 +0100756 } else if (GetInput()->IsLongConstant()) {
Roland Levillainb762d2e2014-10-22 10:11:06 +0100757 // TODO: Implement static evaluation of long unary operations.
758 //
759 // Do not exit with a fatal condition here. Instead, simply
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700760 // return `null' to notify the caller that this instruction
Roland Levillainb762d2e2014-10-22 10:11:06 +0100761 // cannot (yet) be statically evaluated.
Roland Levillain9240d6a2014-10-20 16:47:04 +0100762 return nullptr;
763 }
764 return nullptr;
765}
766
767HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +0100768 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
769 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
770 GetRight()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000771 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100772 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
773 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
774 GetRight()->AsLongConstant()->GetValue());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000775 if (GetResultType() == Primitive::kPrimLong) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000776 return GetBlock()->GetGraph()->GetLongConstant(value);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000777 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000778 DCHECK_EQ(GetResultType(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000779 return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000780 }
Roland Levillain556c3d12014-09-18 15:25:07 +0100781 }
782 return nullptr;
783}
Dave Allison20dfc792014-06-16 20:44:29 -0700784
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000785HConstant* HBinaryOperation::GetConstantRight() const {
786 if (GetRight()->IsConstant()) {
787 return GetRight()->AsConstant();
788 } else if (IsCommutative() && GetLeft()->IsConstant()) {
789 return GetLeft()->AsConstant();
790 } else {
791 return nullptr;
792 }
793}
794
795// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700796// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000797HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
798 HInstruction* most_constant_right = GetConstantRight();
799 if (most_constant_right == nullptr) {
800 return nullptr;
801 } else if (most_constant_right == GetLeft()) {
802 return GetRight();
803 } else {
804 return GetLeft();
805 }
806}
807
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700808bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
809 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100810}
811
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100812bool HInstruction::Equals(HInstruction* other) const {
813 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100814 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100815 if (!InstructionDataEquals(other)) return false;
816 if (GetType() != other->GetType()) return false;
817 if (InputCount() != other->InputCount()) return false;
818
819 for (size_t i = 0, e = InputCount(); i < e; ++i) {
820 if (InputAt(i) != other->InputAt(i)) return false;
821 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100822 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100823 return true;
824}
825
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700826std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
827#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
828 switch (rhs) {
829 FOR_EACH_INSTRUCTION(DECLARE_CASE)
830 default:
831 os << "Unknown instruction kind " << static_cast<int>(rhs);
832 break;
833 }
834#undef DECLARE_CASE
835 return os;
836}
837
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000838void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000839 next_->previous_ = previous_;
840 if (previous_ != nullptr) {
841 previous_->next_ = next_;
842 }
843 if (block_->instructions_.first_instruction_ == this) {
844 block_->instructions_.first_instruction_ = next_;
845 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000846 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000847
848 previous_ = cursor->previous_;
849 if (previous_ != nullptr) {
850 previous_->next_ = this;
851 }
852 next_ = cursor;
853 cursor->previous_ = this;
854 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000855
856 if (block_->instructions_.first_instruction_ == cursor) {
857 block_->instructions_.first_instruction_ = this;
858 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000859}
860
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000861HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
862 DCHECK(!cursor->IsControlFlow());
863 DCHECK_NE(instructions_.last_instruction_, cursor);
864 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000865
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000866 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
867 new_block->instructions_.first_instruction_ = cursor->GetNext();
868 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
869 cursor->next_->previous_ = nullptr;
870 cursor->next_ = nullptr;
871 instructions_.last_instruction_ = cursor;
872
873 new_block->instructions_.SetBlockOfInstructions(new_block);
874 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
875 HBasicBlock* successor = GetSuccessors().Get(i);
876 new_block->successors_.Add(successor);
877 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
878 }
879 successors_.Reset();
880
881 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
882 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
883 dominated->dominator_ = new_block;
884 new_block->dominated_blocks_.Add(dominated);
885 }
886 dominated_blocks_.Reset();
887 return new_block;
888}
889
David Brazdil46e2a392015-03-16 17:31:52 +0000890bool HBasicBlock::IsSingleGoto() const {
891 HLoopInformation* loop_info = GetLoopInformation();
892 // TODO: Remove the null check b/19084197.
893 return GetFirstInstruction() != nullptr
894 && GetPhis().IsEmpty()
895 && GetFirstInstruction() == GetLastInstruction()
896 && GetLastInstruction()->IsGoto()
897 // Back edges generate the suspend check.
898 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
899}
900
David Brazdil8d5b8b22015-03-24 10:51:52 +0000901bool HBasicBlock::EndsWithControlFlowInstruction() const {
902 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
903}
904
David Brazdilb2bd1c52015-03-25 11:17:37 +0000905bool HBasicBlock::EndsWithIf() const {
906 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
907}
908
909bool HBasicBlock::HasSinglePhi() const {
910 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
911}
912
David Brazdil2d7352b2015-04-20 14:52:42 +0100913size_t HInstructionList::CountSize() const {
914 size_t size = 0;
915 HInstruction* current = first_instruction_;
916 for (; current != nullptr; current = current->GetNext()) {
917 size++;
918 }
919 return size;
920}
921
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000922void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
923 for (HInstruction* current = first_instruction_;
924 current != nullptr;
925 current = current->GetNext()) {
926 current->SetBlock(block);
927 }
928}
929
930void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
931 DCHECK(Contains(cursor));
932 if (!instruction_list.IsEmpty()) {
933 if (cursor == last_instruction_) {
934 last_instruction_ = instruction_list.last_instruction_;
935 } else {
936 cursor->next_->previous_ = instruction_list.last_instruction_;
937 }
938 instruction_list.last_instruction_->next_ = cursor->next_;
939 cursor->next_ = instruction_list.first_instruction_;
940 instruction_list.first_instruction_->previous_ = cursor;
941 }
942}
943
944void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +0000945 if (IsEmpty()) {
946 first_instruction_ = instruction_list.first_instruction_;
947 last_instruction_ = instruction_list.last_instruction_;
948 } else {
949 AddAfter(last_instruction_, instruction_list);
950 }
951}
952
David Brazdil2d7352b2015-04-20 14:52:42 +0100953void HBasicBlock::DisconnectAndDelete() {
954 // Dominators must be removed after all the blocks they dominate. This way
955 // a loop header is removed last, a requirement for correct loop information
956 // iteration.
957 DCHECK(dominated_blocks_.IsEmpty());
David Brazdil46e2a392015-03-16 17:31:52 +0000958
David Brazdil2d7352b2015-04-20 14:52:42 +0100959 // Remove the block from all loops it is included in.
960 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
961 HLoopInformation* loop_info = it.Current();
962 loop_info->Remove(this);
963 if (loop_info->IsBackEdge(*this)) {
964 // This deliberately leaves the loop in an inconsistent state and will
965 // fail SSAChecker unless the entire loop is removed during the pass.
966 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
1041void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001042 DCHECK_EQ(GetGraph(), other->GetGraph());
1043 DCHECK(GetDominatedBlocks().Contains(other));
1044 DCHECK_EQ(GetSuccessors().Size(), 1u);
1045 DCHECK_EQ(GetSuccessors().Get(0), other);
1046 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1047 DCHECK_EQ(other->GetPredecessors().Get(0), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001048 DCHECK(other->GetPhis().IsEmpty());
1049
David Brazdil2d7352b2015-04-20 14:52:42 +01001050 // Move instructions from `other` to `this`.
1051 DCHECK(EndsWithControlFlowInstruction());
1052 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001053 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001054 other->instructions_.SetBlockOfInstructions(this);
1055 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001056
David Brazdil2d7352b2015-04-20 14:52:42 +01001057 // Remove `other` from the loops it is included in.
1058 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1059 HLoopInformation* loop_info = it.Current();
1060 loop_info->Remove(other);
1061 if (loop_info->IsBackEdge(*other)) {
1062 loop_info->ClearBackEdges();
1063 loop_info->AddBackEdge(this);
1064 }
1065 }
1066
1067 // Update links to the successors of `other`.
1068 successors_.Reset();
1069 while (!other->successors_.IsEmpty()) {
1070 HBasicBlock* successor = other->successors_.Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001071 successor->ReplacePredecessor(other, this);
1072 }
1073
David Brazdil2d7352b2015-04-20 14:52:42 +01001074 // Update the dominator tree.
1075 dominated_blocks_.Delete(other);
1076 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1077 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1078 dominated_blocks_.Add(dominated);
1079 dominated->SetDominator(this);
1080 }
1081 other->dominated_blocks_.Reset();
1082 other->dominator_ = nullptr;
1083
1084 // Clear the list of predecessors of `other` in preparation of deleting it.
1085 other->predecessors_.Reset();
1086
1087 // Delete `other` from the graph. The function updates reverse post order.
1088 graph_->DeleteDeadBlock(other);
1089 other->SetGraph(nullptr);
1090}
1091
1092void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1093 DCHECK_NE(GetGraph(), other->GetGraph());
1094 DCHECK(GetDominatedBlocks().IsEmpty());
1095 DCHECK(GetSuccessors().IsEmpty());
1096 DCHECK(!EndsWithControlFlowInstruction());
1097 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1098 DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock());
1099 DCHECK(other->GetPhis().IsEmpty());
1100 DCHECK(!other->IsInLoop());
1101
1102 // Move instructions from `other` to `this`.
1103 instructions_.Add(other->GetInstructions());
1104 other->instructions_.SetBlockOfInstructions(this);
1105
1106 // Update links to the successors of `other`.
1107 successors_.Reset();
1108 while (!other->successors_.IsEmpty()) {
1109 HBasicBlock* successor = other->successors_.Get(0);
1110 successor->ReplacePredecessor(other, this);
1111 }
1112
1113 // Update the dominator tree.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001114 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1115 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1116 dominated_blocks_.Add(dominated);
1117 dominated->SetDominator(this);
1118 }
1119 other->dominated_blocks_.Reset();
1120 other->dominator_ = nullptr;
1121 other->graph_ = nullptr;
1122}
1123
1124void HBasicBlock::ReplaceWith(HBasicBlock* other) {
1125 while (!GetPredecessors().IsEmpty()) {
1126 HBasicBlock* predecessor = GetPredecessors().Get(0);
1127 predecessor->ReplaceSuccessor(this, other);
1128 }
1129 while (!GetSuccessors().IsEmpty()) {
1130 HBasicBlock* successor = GetSuccessors().Get(0);
1131 successor->ReplacePredecessor(this, other);
1132 }
1133 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
1134 other->AddDominatedBlock(dominated_blocks_.Get(i));
1135 }
1136 GetDominator()->ReplaceDominatedBlock(this, other);
1137 other->SetDominator(GetDominator());
1138 dominator_ = nullptr;
1139 graph_ = nullptr;
1140}
1141
1142// Create space in `blocks` for adding `number_of_new_blocks` entries
1143// starting at location `at`. Blocks after `at` are moved accordingly.
1144static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
1145 size_t number_of_new_blocks,
1146 size_t at) {
1147 size_t old_size = blocks->Size();
1148 size_t new_size = old_size + number_of_new_blocks;
1149 blocks->SetSize(new_size);
1150 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
1151 blocks->Put(j, blocks->Get(i));
1152 }
1153}
1154
David Brazdil2d7352b2015-04-20 14:52:42 +01001155void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1156 DCHECK_EQ(block->GetGraph(), this);
1157 DCHECK(block->GetSuccessors().IsEmpty());
1158 DCHECK(block->GetPredecessors().IsEmpty());
1159 DCHECK(block->GetDominatedBlocks().IsEmpty());
1160 DCHECK(block->GetDominator() == nullptr);
1161
1162 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1163 block->RemoveInstruction(it.Current());
1164 }
1165 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1166 block->RemovePhi(it.Current()->AsPhi());
1167 }
1168
1169 reverse_post_order_.Delete(block);
1170 blocks_.Put(block->GetBlockId(), nullptr);
1171}
1172
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001173void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001174 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001175 // Simple case of an entry block, a body block, and an exit block.
1176 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001177 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001178 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
1179 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001180 DCHECK(!body->IsExitBlock());
1181 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001182
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001183 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1184 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001185
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001186 // Replace the invoke with the return value of the inlined graph.
1187 if (last->IsReturn()) {
1188 invoke->ReplaceWith(last->InputAt(0));
1189 } else {
1190 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001191 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001192
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001193 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001194 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001195 // Need to inline multiple blocks. We split `invoke`'s block
1196 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001197 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001198 // with the second half.
1199 ArenaAllocator* allocator = outer_graph->GetArena();
1200 HBasicBlock* at = invoke->GetBlock();
1201 HBasicBlock* to = at->SplitAfter(invoke);
1202
1203 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
1204 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001205 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001206 exit_block_->ReplaceWith(to);
1207
1208 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001209 // to not `HReturn` but `HGoto` instead.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001210 HInstruction* return_value = nullptr;
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001211 bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
1212 if (to->GetPredecessors().Size() == 1) {
1213 HBasicBlock* predecessor = to->GetPredecessors().Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001214 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001215 if (!returns_void) {
1216 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001217 }
1218 predecessor->AddInstruction(new (allocator) HGoto());
1219 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001220 } else {
1221 if (!returns_void) {
1222 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001223 return_value = new (allocator) HPhi(
1224 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001225 to->AddPhi(return_value->AsPhi());
1226 }
1227 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
1228 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
1229 HInstruction* last = predecessor->GetLastInstruction();
1230 if (!returns_void) {
1231 return_value->AsPhi()->AddInput(last->InputAt(0));
1232 }
1233 predecessor->AddInstruction(new (allocator) HGoto());
1234 predecessor->RemoveInstruction(last);
1235 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001236 }
1237
1238 if (return_value != nullptr) {
1239 invoke->ReplaceWith(return_value);
1240 }
1241
1242 // Update the meta information surrounding blocks:
1243 // (1) the graph they are now in,
1244 // (2) the reverse post order of that graph,
1245 // (3) the potential loop information they are now in.
1246
1247 // We don't add the entry block, the exit block, and the first block, which
1248 // has been merged with `at`.
1249 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1250
1251 // We add the `to` block.
1252 static constexpr int kNumberOfNewBlocksInCaller = 1;
1253 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
1254 + kNumberOfNewBlocksInCaller;
1255
1256 // Find the location of `at` in the outer graph's reverse post order. The new
1257 // blocks will be added after it.
1258 size_t index_of_at = 0;
1259 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
1260 index_of_at++;
1261 }
1262 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1263
1264 // Do a reverse post order of the blocks in the callee and do (1), (2),
1265 // and (3) to the blocks that apply.
1266 HLoopInformation* info = at->GetLoopInformation();
1267 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1268 HBasicBlock* current = it.Current();
1269 if (current != exit_block_ && current != entry_block_ && current != first) {
1270 DCHECK(!current->IsInLoop());
1271 DCHECK(current->GetGraph() == this);
1272 current->SetGraph(outer_graph);
1273 outer_graph->AddBlock(current);
1274 outer_graph->reverse_post_order_.Put(++index_of_at, current);
1275 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001276 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001277 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1278 loop_it.Current()->Add(current);
1279 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001280 }
1281 }
1282 }
1283
1284 // Do (1), (2), and (3) to `to`.
1285 to->SetGraph(outer_graph);
1286 outer_graph->AddBlock(to);
1287 outer_graph->reverse_post_order_.Put(++index_of_at, to);
1288 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001289 to->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(to);
1292 }
David Brazdil46e2a392015-03-16 17:31:52 +00001293 if (info->IsBackEdge(*at)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001294 // Only `at` can become a back edge, as the inlined blocks
1295 // are predecessors of `at`.
1296 DCHECK_EQ(1u, info->NumberOfBackEdges());
1297 info->ClearBackEdges();
1298 info->AddBackEdge(to);
1299 }
1300 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001301 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001302
David Brazdil05144f42015-04-16 15:18:00 +01001303 // Update the next instruction id of the outer graph, so that instructions
1304 // added later get bigger ids than those in the inner graph.
1305 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1306
1307 // Walk over the entry block and:
1308 // - Move constants from the entry block to the outer_graph's entry block,
1309 // - Replace HParameterValue instructions with their real value.
1310 // - Remove suspend checks, that hold an environment.
1311 // We must do this after the other blocks have been inlined, otherwise ids of
1312 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001313 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001314 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1315 HInstruction* current = it.Current();
1316 if (current->IsNullConstant()) {
1317 current->ReplaceWith(outer_graph->GetNullConstant());
1318 } else if (current->IsIntConstant()) {
1319 current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue()));
1320 } else if (current->IsLongConstant()) {
1321 current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue()));
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001322 } else if (current->IsFloatConstant()) {
1323 current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue()));
1324 } else if (current->IsDoubleConstant()) {
1325 current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue()));
David Brazdil05144f42015-04-16 15:18:00 +01001326 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001327 if (kIsDebugBuild
1328 && invoke->IsInvokeStaticOrDirect()
1329 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1330 // Ensure we do not use the last input of `invoke`, as it
1331 // contains a clinit check which is not an actual argument.
1332 size_t last_input_index = invoke->InputCount() - 1;
1333 DCHECK(parameter_index != last_input_index);
1334 }
David Brazdil05144f42015-04-16 15:18:00 +01001335 current->ReplaceWith(invoke->InputAt(parameter_index++));
1336 } else {
1337 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1338 entry_block_->RemoveInstruction(current);
1339 }
1340 }
1341
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001342 // Finally remove the invoke from the caller.
1343 invoke->GetBlock()->RemoveInstruction(invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001344}
1345
Calin Juravleacf735c2015-02-12 15:25:22 +00001346std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1347 ScopedObjectAccess soa(Thread::Current());
1348 os << "["
1349 << " is_top=" << rhs.IsTop()
1350 << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
1351 << " is_exact=" << rhs.IsExact()
1352 << " ]";
1353 return os;
1354}
1355
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001356} // namespace art