blob: 41adc7223e7453d5d31f49969dca613344ede373 [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"
David Brazdila4b8c212015-05-07 09:59:30 +010020#include "base/bit_vector-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000021#include "utils/growable_array.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000022#include "scoped_thread_state_change.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023
24namespace art {
25
26void HGraph::AddBlock(HBasicBlock* block) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000027 block->SetBlockId(blocks_.Size());
Nicolas Geoffray818f2102014-02-18 16:43:35 +000028 blocks_.Add(block);
29}
30
Nicolas Geoffray804d0932014-05-02 08:46:00 +010031void HGraph::FindBackEdges(ArenaBitVector* visited) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000032 ArenaBitVector visiting(arena_, blocks_.Size(), false);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033 VisitBlockForBackEdges(entry_block_, visited, &visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000034}
35
Roland Levillainfc600dc2014-12-02 17:16:31 +000036static void RemoveAsUser(HInstruction* instruction) {
37 for (size_t i = 0; i < instruction->InputCount(); i++) {
David Brazdil1abb4192015-02-17 18:33:36 +000038 instruction->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000039 }
40
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010041 for (HEnvironment* environment = instruction->GetEnvironment();
42 environment != nullptr;
43 environment = environment->GetParent()) {
Roland Levillainfc600dc2014-12-02 17:16:31 +000044 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
David Brazdil1abb4192015-02-17 18:33:36 +000045 if (environment->GetInstructionAt(i) != nullptr) {
46 environment->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000047 }
48 }
49 }
50}
51
52void HGraph::RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const {
53 for (size_t i = 0; i < blocks_.Size(); ++i) {
54 if (!visited.IsBitSet(i)) {
55 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010056 DCHECK(block->GetPhis().IsEmpty()) << "Phis are not inserted at this stage";
Roland Levillainfc600dc2014-12-02 17:16:31 +000057 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
58 RemoveAsUser(it.Current());
59 }
60 }
61 }
62}
63
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010064void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010065 for (size_t i = 0; i < blocks_.Size(); ++i) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000066 if (!visited.IsBitSet(i)) {
David Brazdil1abb4192015-02-17 18:33:36 +000067 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010068 // We only need to update the successor, which might be live.
David Brazdil1abb4192015-02-17 18:33:36 +000069 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
70 block->GetSuccessors().Get(j)->RemovePredecessor(block);
71 }
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010072 // Remove the block from the list of blocks, so that further analyses
73 // never see it.
74 blocks_.Put(i, nullptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000075 }
76 }
77}
78
79void HGraph::VisitBlockForBackEdges(HBasicBlock* block,
80 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +010081 ArenaBitVector* visiting) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000082 int id = block->GetBlockId();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000083 if (visited->IsBitSet(id)) return;
84
85 visited->SetBit(id);
86 visiting->SetBit(id);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010087 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
88 HBasicBlock* successor = block->GetSuccessors().Get(i);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000089 if (visiting->IsBitSet(successor->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000090 successor->AddBackEdge(block);
91 } else {
92 VisitBlockForBackEdges(successor, visited, visiting);
93 }
94 }
95 visiting->ClearBit(id);
96}
97
98void HGraph::BuildDominatorTree() {
99 ArenaBitVector visited(arena_, blocks_.Size(), false);
100
101 // (1) Find the back edges in the graph doing a DFS traversal.
102 FindBackEdges(&visited);
103
Roland Levillainfc600dc2014-12-02 17:16:31 +0000104 // (2) Remove instructions and phis from blocks not visited during
105 // the initial DFS as users from other instructions, so that
106 // users can be safely removed before uses later.
107 RemoveInstructionsAsUsersFromDeadBlocks(visited);
108
109 // (3) Remove blocks not visited during the initial DFS.
110 // Step (4) requires dead blocks to be removed from the
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000111 // predecessors list of live blocks.
112 RemoveDeadBlocks(visited);
113
Roland Levillainfc600dc2014-12-02 17:16:31 +0000114 // (4) Simplify the CFG now, so that we don't need to recompute
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100115 // dominators and the reverse post order.
116 SimplifyCFG();
117
Roland Levillainfc600dc2014-12-02 17:16:31 +0000118 // (5) Compute the immediate dominator of each block. We visit
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000119 // the successors of a block only when all its forward branches
120 // have been processed.
121 GrowableArray<size_t> visits(arena_, blocks_.Size());
122 visits.SetSize(blocks_.Size());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100123 reverse_post_order_.Add(entry_block_);
124 for (size_t i = 0; i < entry_block_->GetSuccessors().Size(); i++) {
125 VisitBlockForDominatorTree(entry_block_->GetSuccessors().Get(i), entry_block_, &visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000126 }
127}
128
129HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
130 ArenaBitVector visited(arena_, blocks_.Size(), false);
131 // Walk the dominator tree of the first block and mark the visited blocks.
132 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000133 visited.SetBit(first->GetBlockId());
134 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000135 }
136 // Walk the dominator tree of the second block until a marked block is found.
137 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000138 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000139 return second;
140 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000141 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000142 }
143 LOG(ERROR) << "Could not find common dominator";
144 return nullptr;
145}
146
147void HGraph::VisitBlockForDominatorTree(HBasicBlock* block,
148 HBasicBlock* predecessor,
149 GrowableArray<size_t>* visits) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000150 if (block->GetDominator() == nullptr) {
151 block->SetDominator(predecessor);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000152 } else {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000153 block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000154 }
155
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000156 visits->Increment(block->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000157 // Once all the forward edges have been visited, we know the immediate
158 // dominator of the block. We can then start visiting its successors.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000159 if (visits->Get(block->GetBlockId()) ==
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100160 block->GetPredecessors().Size() - block->NumberOfBackEdges()) {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100161 block->GetDominator()->AddDominatedBlock(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100162 reverse_post_order_.Add(block);
163 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
164 VisitBlockForDominatorTree(block->GetSuccessors().Get(i), block, visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000165 }
166 }
167}
168
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000169void HGraph::TransformToSsa() {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100170 DCHECK(!reverse_post_order_.IsEmpty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100171 SsaBuilder ssa_builder(this);
172 ssa_builder.BuildSsa();
173}
174
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100175void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) {
176 // Insert a new node between `block` and `successor` to split the
177 // critical edge.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100178 HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100179 AddBlock(new_block);
180 new_block->AddInstruction(new (arena_) HGoto());
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100181 block->ReplaceSuccessor(successor, new_block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100182 new_block->AddSuccessor(successor);
183 if (successor->IsLoopHeader()) {
184 // If we split at a back edge boundary, make the new block the back edge.
185 HLoopInformation* info = successor->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000186 if (info->IsBackEdge(*block)) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100187 info->RemoveBackEdge(block);
188 info->AddBackEdge(new_block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100189 }
190 }
191}
192
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100193void HGraph::SimplifyLoop(HBasicBlock* header) {
194 HLoopInformation* info = header->GetLoopInformation();
195
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100196 // Make sure the loop has only one pre header. This simplifies SSA building by having
197 // to just look at the pre header to know which locals are initialized at entry of the
198 // loop.
199 size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges();
200 if (number_of_incomings != 1) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100201 HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100202 AddBlock(pre_header);
203 pre_header->AddInstruction(new (arena_) HGoto());
204
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100205 for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) {
206 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100207 if (!info->IsBackEdge(*predecessor)) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100208 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100209 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100210 }
211 }
212 pre_header->AddSuccessor(header);
213 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100214
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100215 // Make sure the first predecessor of a loop header is the incoming block.
216 if (info->IsBackEdge(*header->GetPredecessors().Get(0))) {
217 HBasicBlock* to_swap = header->GetPredecessors().Get(0);
218 for (size_t pred = 1, e = header->GetPredecessors().Size(); pred < e; ++pred) {
219 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
220 if (!info->IsBackEdge(*predecessor)) {
221 header->predecessors_.Put(pred, to_swap);
222 header->predecessors_.Put(0, predecessor);
223 break;
224 }
225 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100226 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100227
228 // Place the suspend check at the beginning of the header, so that live registers
229 // will be known when allocating registers. Note that code generation can still
230 // generate the suspend check at the back edge, but needs to be careful with
231 // loop phi spill slots (which are not written to at back edge).
232 HInstruction* first_instruction = header->GetFirstInstruction();
233 if (!first_instruction->IsSuspendCheck()) {
234 HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc());
235 header->InsertInstructionBefore(check, first_instruction);
236 first_instruction = check;
237 }
238 info->SetSuspendCheck(first_instruction->AsSuspendCheck());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100239}
240
241void HGraph::SimplifyCFG() {
242 // Simplify the CFG for future analysis, and code generation:
243 // (1): Split critical edges.
244 // (2): Simplify loops by having only one back edge, and one preheader.
245 for (size_t i = 0; i < blocks_.Size(); ++i) {
246 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100247 if (block == nullptr) continue;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100248 if (block->GetSuccessors().Size() > 1) {
249 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
250 HBasicBlock* successor = block->GetSuccessors().Get(j);
251 if (successor->GetPredecessors().Size() > 1) {
252 SplitCriticalEdge(block, successor);
253 --j;
254 }
255 }
256 }
257 if (block->IsLoopHeader()) {
258 SimplifyLoop(block);
259 }
260 }
261}
262
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000263bool HGraph::AnalyzeNaturalLoops() const {
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100264 // Order does not matter.
265 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
266 HBasicBlock* block = it.Current();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100267 if (block->IsLoopHeader()) {
268 HLoopInformation* info = block->GetLoopInformation();
269 if (!info->Populate()) {
270 // Abort if the loop is non natural. We currently bailout in such cases.
271 return false;
272 }
273 }
274 }
275 return true;
276}
277
David Brazdil8d5b8b22015-03-24 10:51:52 +0000278void HGraph::InsertConstant(HConstant* constant) {
279 // New constants are inserted before the final control-flow instruction
280 // of the graph, or at its end if called from the graph builder.
281 if (entry_block_->EndsWithControlFlowInstruction()) {
282 entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction());
David Brazdil46e2a392015-03-16 17:31:52 +0000283 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000284 entry_block_->AddInstruction(constant);
David Brazdil46e2a392015-03-16 17:31:52 +0000285 }
286}
287
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000288HNullConstant* HGraph::GetNullConstant() {
289 if (cached_null_constant_ == nullptr) {
290 cached_null_constant_ = new (arena_) HNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000291 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000292 }
293 return cached_null_constant_;
294}
295
David Brazdil8d5b8b22015-03-24 10:51:52 +0000296HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value) {
297 switch (type) {
298 case Primitive::Type::kPrimBoolean:
299 DCHECK(IsUint<1>(value));
300 FALLTHROUGH_INTENDED;
301 case Primitive::Type::kPrimByte:
302 case Primitive::Type::kPrimChar:
303 case Primitive::Type::kPrimShort:
304 case Primitive::Type::kPrimInt:
305 DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value));
306 return GetIntConstant(static_cast<int32_t>(value));
307
308 case Primitive::Type::kPrimLong:
309 return GetLongConstant(value);
310
311 default:
312 LOG(FATAL) << "Unsupported constant type";
313 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000314 }
David Brazdil46e2a392015-03-16 17:31:52 +0000315}
316
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000317void HGraph::CacheFloatConstant(HFloatConstant* constant) {
318 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
319 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
320 cached_float_constants_.Overwrite(value, constant);
321}
322
323void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
324 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
325 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
326 cached_double_constants_.Overwrite(value, constant);
327}
328
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000329void HLoopInformation::Add(HBasicBlock* block) {
330 blocks_.SetBit(block->GetBlockId());
331}
332
David Brazdil46e2a392015-03-16 17:31:52 +0000333void HLoopInformation::Remove(HBasicBlock* block) {
334 blocks_.ClearBit(block->GetBlockId());
335}
336
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100337void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
338 if (blocks_.IsBitSet(block->GetBlockId())) {
339 return;
340 }
341
342 blocks_.SetBit(block->GetBlockId());
343 block->SetInLoop(this);
344 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
345 PopulateRecursive(block->GetPredecessors().Get(i));
346 }
347}
348
349bool HLoopInformation::Populate() {
David Brazdila4b8c212015-05-07 09:59:30 +0100350 DCHECK_EQ(blocks_.NumSetBits(), 0u) << "Loop information has already been populated";
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100351 for (size_t i = 0, e = GetBackEdges().Size(); i < e; ++i) {
352 HBasicBlock* back_edge = GetBackEdges().Get(i);
353 DCHECK(back_edge->GetDominator() != nullptr);
354 if (!header_->Dominates(back_edge)) {
355 // This loop is not natural. Do not bother going further.
356 return false;
357 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100358
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100359 // Populate this loop: starting with the back edge, recursively add predecessors
360 // that are not already part of that loop. Set the header as part of the loop
361 // to end the recursion.
362 // This is a recursive implementation of the algorithm described in
363 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
364 blocks_.SetBit(header_->GetBlockId());
365 PopulateRecursive(back_edge);
366 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100367 return true;
368}
369
David Brazdila4b8c212015-05-07 09:59:30 +0100370void HLoopInformation::Update() {
371 HGraph* graph = header_->GetGraph();
372 for (uint32_t id : blocks_.Indexes()) {
373 HBasicBlock* block = graph->GetBlocks().Get(id);
374 // Reset loop information of non-header blocks inside the loop, except
375 // members of inner nested loops because those should already have been
376 // updated by their own LoopInformation.
377 if (block->GetLoopInformation() == this && block != header_) {
378 block->SetLoopInformation(nullptr);
379 }
380 }
381 blocks_.ClearAllBits();
382
383 if (back_edges_.IsEmpty()) {
384 // The loop has been dismantled, delete its suspend check and remove info
385 // from the header.
386 DCHECK(HasSuspendCheck());
387 header_->RemoveInstruction(suspend_check_);
388 header_->SetLoopInformation(nullptr);
389 header_ = nullptr;
390 suspend_check_ = nullptr;
391 } else {
392 if (kIsDebugBuild) {
393 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
394 DCHECK(header_->Dominates(back_edges_.Get(i)));
395 }
396 }
397 // This loop still has reachable back edges. Repopulate the list of blocks.
398 bool populate_successful = Populate();
399 DCHECK(populate_successful);
400 }
401}
402
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100403HBasicBlock* HLoopInformation::GetPreHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100404 return header_->GetDominator();
405}
406
407bool HLoopInformation::Contains(const HBasicBlock& block) const {
408 return blocks_.IsBitSet(block.GetBlockId());
409}
410
411bool HLoopInformation::IsIn(const HLoopInformation& other) const {
412 return other.blocks_.IsBitSet(header_->GetBlockId());
413}
414
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100415size_t HLoopInformation::GetLifetimeEnd() const {
416 size_t last_position = 0;
417 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
418 last_position = std::max(back_edges_.Get(i)->GetLifetimeEnd(), last_position);
419 }
420 return last_position;
421}
422
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100423bool HBasicBlock::Dominates(HBasicBlock* other) const {
424 // Walk up the dominator tree from `other`, to find out if `this`
425 // is an ancestor.
426 HBasicBlock* current = other;
427 while (current != nullptr) {
428 if (current == this) {
429 return true;
430 }
431 current = current->GetDominator();
432 }
433 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100434}
435
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100436static void UpdateInputsUsers(HInstruction* instruction) {
437 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
438 instruction->InputAt(i)->AddUseAt(instruction, i);
439 }
440 // Environment should be created later.
441 DCHECK(!instruction->HasEnvironment());
442}
443
Roland Levillainccc07a92014-09-16 14:48:16 +0100444void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
445 HInstruction* replacement) {
446 DCHECK(initial->GetBlock() == this);
447 InsertInstructionBefore(replacement, initial);
448 initial->ReplaceWith(replacement);
449 RemoveInstruction(initial);
450}
451
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100452static void Add(HInstructionList* instruction_list,
453 HBasicBlock* block,
454 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000455 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000456 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100457 instruction->SetBlock(block);
458 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100459 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100460 instruction_list->AddInstruction(instruction);
461}
462
463void HBasicBlock::AddInstruction(HInstruction* instruction) {
464 Add(&instructions_, this, instruction);
465}
466
467void HBasicBlock::AddPhi(HPhi* phi) {
468 Add(&phis_, this, phi);
469}
470
David Brazdilc3d743f2015-04-22 13:40:50 +0100471void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
472 DCHECK(!cursor->IsPhi());
473 DCHECK(!instruction->IsPhi());
474 DCHECK_EQ(instruction->GetId(), -1);
475 DCHECK_NE(cursor->GetId(), -1);
476 DCHECK_EQ(cursor->GetBlock(), this);
477 DCHECK(!instruction->IsControlFlow());
478 instruction->SetBlock(this);
479 instruction->SetId(GetGraph()->GetNextInstructionId());
480 UpdateInputsUsers(instruction);
481 instructions_.InsertInstructionBefore(instruction, cursor);
482}
483
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100484void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
485 DCHECK(!cursor->IsPhi());
486 DCHECK(!instruction->IsPhi());
487 DCHECK_EQ(instruction->GetId(), -1);
488 DCHECK_NE(cursor->GetId(), -1);
489 DCHECK_EQ(cursor->GetBlock(), this);
490 DCHECK(!instruction->IsControlFlow());
491 DCHECK(!cursor->IsControlFlow());
492 instruction->SetBlock(this);
493 instruction->SetId(GetGraph()->GetNextInstructionId());
494 UpdateInputsUsers(instruction);
495 instructions_.InsertInstructionAfter(instruction, cursor);
496}
497
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100498void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
499 DCHECK_EQ(phi->GetId(), -1);
500 DCHECK_NE(cursor->GetId(), -1);
501 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100502 phi->SetBlock(this);
503 phi->SetId(GetGraph()->GetNextInstructionId());
504 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100505 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100506}
507
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100508static void Remove(HInstructionList* instruction_list,
509 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000510 HInstruction* instruction,
511 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100512 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100513 instruction->SetBlock(nullptr);
514 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000515 if (ensure_safety) {
516 DCHECK(instruction->GetUses().IsEmpty());
517 DCHECK(instruction->GetEnvUses().IsEmpty());
518 RemoveAsUser(instruction);
519 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100520}
521
David Brazdil1abb4192015-02-17 18:33:36 +0000522void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100523 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000524 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100525}
526
David Brazdil1abb4192015-02-17 18:33:36 +0000527void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
528 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100529}
530
David Brazdilc7508e92015-04-27 13:28:57 +0100531void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
532 if (instruction->IsPhi()) {
533 RemovePhi(instruction->AsPhi(), ensure_safety);
534 } else {
535 RemoveInstruction(instruction, ensure_safety);
536 }
537}
538
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +0100539void HEnvironment::CopyFrom(const GrowableArray<HInstruction*>& locals) {
540 for (size_t i = 0; i < locals.Size(); i++) {
541 HInstruction* instruction = locals.Get(i);
542 SetRawEnvAt(i, instruction);
543 if (instruction != nullptr) {
544 instruction->AddEnvUseAt(this, i);
545 }
546 }
547}
548
David Brazdiled596192015-01-23 10:39:45 +0000549void HEnvironment::CopyFrom(HEnvironment* env) {
550 for (size_t i = 0; i < env->Size(); i++) {
551 HInstruction* instruction = env->GetInstructionAt(i);
552 SetRawEnvAt(i, instruction);
553 if (instruction != nullptr) {
554 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100555 }
David Brazdiled596192015-01-23 10:39:45 +0000556 }
557}
558
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700559void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
560 HBasicBlock* loop_header) {
561 DCHECK(loop_header->IsLoopHeader());
562 for (size_t i = 0; i < env->Size(); i++) {
563 HInstruction* instruction = env->GetInstructionAt(i);
564 SetRawEnvAt(i, instruction);
565 if (instruction == nullptr) {
566 continue;
567 }
568 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
569 // At the end of the loop pre-header, the corresponding value for instruction
570 // is the first input of the phi.
571 HInstruction* initial = instruction->AsPhi()->InputAt(0);
572 DCHECK(initial->GetBlock()->Dominates(loop_header));
573 SetRawEnvAt(i, initial);
574 initial->AddEnvUseAt(this, i);
575 } else {
576 instruction->AddEnvUseAt(this, i);
577 }
578 }
579}
580
David Brazdil1abb4192015-02-17 18:33:36 +0000581void HEnvironment::RemoveAsUserOfInput(size_t index) const {
582 const HUserRecord<HEnvironment*> user_record = vregs_.Get(index);
583 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100584}
585
Calin Juravle77520bc2015-01-12 18:45:46 +0000586HInstruction* HInstruction::GetNextDisregardingMoves() const {
587 HInstruction* next = GetNext();
588 while (next != nullptr && next->IsParallelMove()) {
589 next = next->GetNext();
590 }
591 return next;
592}
593
594HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
595 HInstruction* previous = GetPrevious();
596 while (previous != nullptr && previous->IsParallelMove()) {
597 previous = previous->GetPrevious();
598 }
599 return previous;
600}
601
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100602void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000603 if (first_instruction_ == nullptr) {
604 DCHECK(last_instruction_ == nullptr);
605 first_instruction_ = last_instruction_ = instruction;
606 } else {
607 last_instruction_->next_ = instruction;
608 instruction->previous_ = last_instruction_;
609 last_instruction_ = instruction;
610 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000611}
612
David Brazdilc3d743f2015-04-22 13:40:50 +0100613void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
614 DCHECK(Contains(cursor));
615 if (cursor == first_instruction_) {
616 cursor->previous_ = instruction;
617 instruction->next_ = cursor;
618 first_instruction_ = instruction;
619 } else {
620 instruction->previous_ = cursor->previous_;
621 instruction->next_ = cursor;
622 cursor->previous_ = instruction;
623 instruction->previous_->next_ = instruction;
624 }
625}
626
627void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
628 DCHECK(Contains(cursor));
629 if (cursor == last_instruction_) {
630 cursor->next_ = instruction;
631 instruction->previous_ = cursor;
632 last_instruction_ = instruction;
633 } else {
634 instruction->next_ = cursor->next_;
635 instruction->previous_ = cursor;
636 cursor->next_ = instruction;
637 instruction->next_->previous_ = instruction;
638 }
639}
640
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100641void HInstructionList::RemoveInstruction(HInstruction* instruction) {
642 if (instruction->previous_ != nullptr) {
643 instruction->previous_->next_ = instruction->next_;
644 }
645 if (instruction->next_ != nullptr) {
646 instruction->next_->previous_ = instruction->previous_;
647 }
648 if (instruction == first_instruction_) {
649 first_instruction_ = instruction->next_;
650 }
651 if (instruction == last_instruction_) {
652 last_instruction_ = instruction->previous_;
653 }
654}
655
Roland Levillain6b469232014-09-25 10:10:38 +0100656bool HInstructionList::Contains(HInstruction* instruction) const {
657 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
658 if (it.Current() == instruction) {
659 return true;
660 }
661 }
662 return false;
663}
664
Roland Levillainccc07a92014-09-16 14:48:16 +0100665bool HInstructionList::FoundBefore(const HInstruction* instruction1,
666 const HInstruction* instruction2) const {
667 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
668 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
669 if (it.Current() == instruction1) {
670 return true;
671 }
672 if (it.Current() == instruction2) {
673 return false;
674 }
675 }
676 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
677 return true;
678}
679
Roland Levillain6c82d402014-10-13 16:10:27 +0100680bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
681 if (other_instruction == this) {
682 // An instruction does not strictly dominate itself.
683 return false;
684 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100685 HBasicBlock* block = GetBlock();
686 HBasicBlock* other_block = other_instruction->GetBlock();
687 if (block != other_block) {
688 return GetBlock()->Dominates(other_instruction->GetBlock());
689 } else {
690 // If both instructions are in the same block, ensure this
691 // instruction comes before `other_instruction`.
692 if (IsPhi()) {
693 if (!other_instruction->IsPhi()) {
694 // Phis appear before non phi-instructions so this instruction
695 // dominates `other_instruction`.
696 return true;
697 } else {
698 // There is no order among phis.
699 LOG(FATAL) << "There is no dominance between phis of a same block.";
700 return false;
701 }
702 } else {
703 // `this` is not a phi.
704 if (other_instruction->IsPhi()) {
705 // Phis appear before non phi-instructions so this instruction
706 // does not dominate `other_instruction`.
707 return false;
708 } else {
709 // Check whether this instruction comes before
710 // `other_instruction` in the instruction list.
711 return block->GetInstructions().FoundBefore(this, other_instruction);
712 }
713 }
714 }
715}
716
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100717void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100718 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000719 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
720 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100721 HInstruction* user = current->GetUser();
722 size_t input_index = current->GetIndex();
723 user->SetRawInputAt(input_index, other);
724 other->AddUseAt(user, input_index);
725 }
726
David Brazdiled596192015-01-23 10:39:45 +0000727 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
728 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100729 HEnvironment* user = current->GetUser();
730 size_t input_index = current->GetIndex();
731 user->SetRawEnvAt(input_index, other);
732 other->AddEnvUseAt(user, input_index);
733 }
734
David Brazdiled596192015-01-23 10:39:45 +0000735 uses_.Clear();
736 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100737}
738
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100739void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000740 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100741 SetRawInputAt(index, replacement);
742 replacement->AddUseAt(this, index);
743}
744
Nicolas Geoffray39468442014-09-02 15:17:15 +0100745size_t HInstruction::EnvironmentSize() const {
746 return HasEnvironment() ? environment_->Size() : 0;
747}
748
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100749void HPhi::AddInput(HInstruction* input) {
750 DCHECK(input->GetBlock() != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +0000751 inputs_.Add(HUserRecord<HInstruction*>(input));
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100752 input->AddUseAt(this, inputs_.Size() - 1);
753}
754
David Brazdil2d7352b2015-04-20 14:52:42 +0100755void HPhi::RemoveInputAt(size_t index) {
756 RemoveAsUserOfInput(index);
757 inputs_.DeleteAt(index);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100758 for (size_t i = index, e = InputCount(); i < e; ++i) {
759 InputRecordAt(i).GetUseNode()->SetIndex(i);
760 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100761}
762
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100763#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000764void H##name::Accept(HGraphVisitor* visitor) { \
765 visitor->Visit##name(this); \
766}
767
768FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
769
770#undef DEFINE_ACCEPT
771
772void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100773 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
774 for (size_t i = 0 ; i < blocks.Size(); i++) {
David Brazdil46e2a392015-03-16 17:31:52 +0000775 HBasicBlock* block = blocks.Get(i);
776 if (block != nullptr) {
777 VisitBasicBlock(block);
778 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000779 }
780}
781
Roland Levillain633021e2014-10-01 14:12:25 +0100782void HGraphVisitor::VisitReversePostOrder() {
783 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
784 VisitBasicBlock(it.Current());
785 }
786}
787
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000788void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100789 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100790 it.Current()->Accept(this);
791 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100792 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000793 it.Current()->Accept(this);
794 }
795}
796
Roland Levillain9240d6a2014-10-20 16:47:04 +0100797HConstant* HUnaryOperation::TryStaticEvaluation() const {
798 if (GetInput()->IsIntConstant()) {
799 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000800 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain9240d6a2014-10-20 16:47:04 +0100801 } else if (GetInput()->IsLongConstant()) {
Roland Levillainb762d2e2014-10-22 10:11:06 +0100802 // TODO: Implement static evaluation of long unary operations.
803 //
804 // Do not exit with a fatal condition here. Instead, simply
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700805 // return `null' to notify the caller that this instruction
Roland Levillainb762d2e2014-10-22 10:11:06 +0100806 // cannot (yet) be statically evaluated.
Roland Levillain9240d6a2014-10-20 16:47:04 +0100807 return nullptr;
808 }
809 return nullptr;
810}
811
812HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +0100813 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
814 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
815 GetRight()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000816 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100817 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
818 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
819 GetRight()->AsLongConstant()->GetValue());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000820 if (GetResultType() == Primitive::kPrimLong) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000821 return GetBlock()->GetGraph()->GetLongConstant(value);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000822 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000823 DCHECK_EQ(GetResultType(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000824 return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000825 }
Roland Levillain556c3d12014-09-18 15:25:07 +0100826 }
827 return nullptr;
828}
Dave Allison20dfc792014-06-16 20:44:29 -0700829
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000830HConstant* HBinaryOperation::GetConstantRight() const {
831 if (GetRight()->IsConstant()) {
832 return GetRight()->AsConstant();
833 } else if (IsCommutative() && GetLeft()->IsConstant()) {
834 return GetLeft()->AsConstant();
835 } else {
836 return nullptr;
837 }
838}
839
840// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700841// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000842HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
843 HInstruction* most_constant_right = GetConstantRight();
844 if (most_constant_right == nullptr) {
845 return nullptr;
846 } else if (most_constant_right == GetLeft()) {
847 return GetRight();
848 } else {
849 return GetLeft();
850 }
851}
852
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700853bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
854 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100855}
856
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100857bool HInstruction::Equals(HInstruction* other) const {
858 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100859 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100860 if (!InstructionDataEquals(other)) return false;
861 if (GetType() != other->GetType()) return false;
862 if (InputCount() != other->InputCount()) return false;
863
864 for (size_t i = 0, e = InputCount(); i < e; ++i) {
865 if (InputAt(i) != other->InputAt(i)) return false;
866 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100867 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100868 return true;
869}
870
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700871std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
872#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
873 switch (rhs) {
874 FOR_EACH_INSTRUCTION(DECLARE_CASE)
875 default:
876 os << "Unknown instruction kind " << static_cast<int>(rhs);
877 break;
878 }
879#undef DECLARE_CASE
880 return os;
881}
882
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000883void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000884 next_->previous_ = previous_;
885 if (previous_ != nullptr) {
886 previous_->next_ = next_;
887 }
888 if (block_->instructions_.first_instruction_ == this) {
889 block_->instructions_.first_instruction_ = next_;
890 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000891 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000892
893 previous_ = cursor->previous_;
894 if (previous_ != nullptr) {
895 previous_->next_ = this;
896 }
897 next_ = cursor;
898 cursor->previous_ = this;
899 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000900
901 if (block_->instructions_.first_instruction_ == cursor) {
902 block_->instructions_.first_instruction_ = this;
903 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000904}
905
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000906HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
907 DCHECK(!cursor->IsControlFlow());
908 DCHECK_NE(instructions_.last_instruction_, cursor);
909 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000910
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000911 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
912 new_block->instructions_.first_instruction_ = cursor->GetNext();
913 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
914 cursor->next_->previous_ = nullptr;
915 cursor->next_ = nullptr;
916 instructions_.last_instruction_ = cursor;
917
918 new_block->instructions_.SetBlockOfInstructions(new_block);
919 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
920 HBasicBlock* successor = GetSuccessors().Get(i);
921 new_block->successors_.Add(successor);
922 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
923 }
924 successors_.Reset();
925
926 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
927 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
928 dominated->dominator_ = new_block;
929 new_block->dominated_blocks_.Add(dominated);
930 }
931 dominated_blocks_.Reset();
932 return new_block;
933}
934
David Brazdil46e2a392015-03-16 17:31:52 +0000935bool HBasicBlock::IsSingleGoto() const {
936 HLoopInformation* loop_info = GetLoopInformation();
937 // TODO: Remove the null check b/19084197.
938 return GetFirstInstruction() != nullptr
939 && GetPhis().IsEmpty()
940 && GetFirstInstruction() == GetLastInstruction()
941 && GetLastInstruction()->IsGoto()
942 // Back edges generate the suspend check.
943 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
944}
945
David Brazdil8d5b8b22015-03-24 10:51:52 +0000946bool HBasicBlock::EndsWithControlFlowInstruction() const {
947 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
948}
949
David Brazdilb2bd1c52015-03-25 11:17:37 +0000950bool HBasicBlock::EndsWithIf() const {
951 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
952}
953
954bool HBasicBlock::HasSinglePhi() const {
955 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
956}
957
David Brazdil2d7352b2015-04-20 14:52:42 +0100958size_t HInstructionList::CountSize() const {
959 size_t size = 0;
960 HInstruction* current = first_instruction_;
961 for (; current != nullptr; current = current->GetNext()) {
962 size++;
963 }
964 return size;
965}
966
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000967void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
968 for (HInstruction* current = first_instruction_;
969 current != nullptr;
970 current = current->GetNext()) {
971 current->SetBlock(block);
972 }
973}
974
975void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
976 DCHECK(Contains(cursor));
977 if (!instruction_list.IsEmpty()) {
978 if (cursor == last_instruction_) {
979 last_instruction_ = instruction_list.last_instruction_;
980 } else {
981 cursor->next_->previous_ = instruction_list.last_instruction_;
982 }
983 instruction_list.last_instruction_->next_ = cursor->next_;
984 cursor->next_ = instruction_list.first_instruction_;
985 instruction_list.first_instruction_->previous_ = cursor;
986 }
987}
988
989void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +0000990 if (IsEmpty()) {
991 first_instruction_ = instruction_list.first_instruction_;
992 last_instruction_ = instruction_list.last_instruction_;
993 } else {
994 AddAfter(last_instruction_, instruction_list);
995 }
996}
997
David Brazdil2d7352b2015-04-20 14:52:42 +0100998void HBasicBlock::DisconnectAndDelete() {
999 // Dominators must be removed after all the blocks they dominate. This way
1000 // a loop header is removed last, a requirement for correct loop information
1001 // iteration.
1002 DCHECK(dominated_blocks_.IsEmpty());
David Brazdil46e2a392015-03-16 17:31:52 +00001003
David Brazdil2d7352b2015-04-20 14:52:42 +01001004 // Remove the block from all loops it is included in.
1005 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
1006 HLoopInformation* loop_info = it.Current();
1007 loop_info->Remove(this);
1008 if (loop_info->IsBackEdge(*this)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001009 // If this was the last back edge of the loop, we deliberately leave the
1010 // loop in an inconsistent state and will fail SSAChecker unless the
1011 // entire loop is removed during the pass.
David Brazdil2d7352b2015-04-20 14:52:42 +01001012 loop_info->RemoveBackEdge(this);
1013 }
1014 }
1015
1016 // Disconnect the block from its predecessors and update their control-flow
1017 // instructions.
David Brazdil46e2a392015-03-16 17:31:52 +00001018 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001019 HBasicBlock* predecessor = predecessors_.Get(i);
1020 HInstruction* last_instruction = predecessor->GetLastInstruction();
1021 predecessor->RemoveInstruction(last_instruction);
1022 predecessor->RemoveSuccessor(this);
1023 if (predecessor->GetSuccessors().Size() == 1u) {
1024 DCHECK(last_instruction->IsIf());
1025 predecessor->AddInstruction(new (graph_->GetArena()) HGoto());
1026 } else {
1027 // The predecessor has no remaining successors and therefore must be dead.
1028 // We deliberately leave it without a control-flow instruction so that the
1029 // SSAChecker fails unless it is not removed during the pass too.
1030 DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u);
1031 }
David Brazdil46e2a392015-03-16 17:31:52 +00001032 }
David Brazdil46e2a392015-03-16 17:31:52 +00001033 predecessors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001034
1035 // Disconnect the block from its successors and update their dominators
1036 // and phis.
1037 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
1038 HBasicBlock* successor = successors_.Get(i);
1039 // Delete this block from the list of predecessors.
1040 size_t this_index = successor->GetPredecessorIndexOf(this);
1041 successor->predecessors_.DeleteAt(this_index);
1042
1043 // Check that `successor` has other predecessors, otherwise `this` is the
1044 // dominator of `successor` which violates the order DCHECKed at the top.
1045 DCHECK(!successor->predecessors_.IsEmpty());
1046
1047 // Recompute the successor's dominator.
1048 HBasicBlock* old_dominator = successor->GetDominator();
1049 HBasicBlock* new_dominator = successor->predecessors_.Get(0);
1050 for (size_t j = 1, f = successor->predecessors_.Size(); j < f; ++j) {
1051 new_dominator = graph_->FindCommonDominator(
1052 new_dominator, successor->predecessors_.Get(j));
1053 }
1054 if (old_dominator != new_dominator) {
1055 successor->SetDominator(new_dominator);
1056 old_dominator->RemoveDominatedBlock(successor);
1057 new_dominator->AddDominatedBlock(successor);
1058 }
1059
1060 // Remove this block's entries in the successor's phis.
1061 if (successor->predecessors_.Size() == 1u) {
1062 // The successor has just one predecessor left. Replace phis with the only
1063 // remaining input.
1064 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1065 HPhi* phi = phi_it.Current()->AsPhi();
1066 phi->ReplaceWith(phi->InputAt(1 - this_index));
1067 successor->RemovePhi(phi);
1068 }
1069 } else {
1070 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1071 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1072 }
1073 }
1074 }
David Brazdil46e2a392015-03-16 17:31:52 +00001075 successors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001076
1077 // Disconnect from the dominator.
1078 dominator_->RemoveDominatedBlock(this);
1079 SetDominator(nullptr);
1080
1081 // Delete from the graph. The function safely deletes remaining instructions
1082 // and updates the reverse post order.
1083 graph_->DeleteDeadBlock(this);
1084 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001085}
1086
1087void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001088 DCHECK_EQ(GetGraph(), other->GetGraph());
1089 DCHECK(GetDominatedBlocks().Contains(other));
1090 DCHECK_EQ(GetSuccessors().Size(), 1u);
1091 DCHECK_EQ(GetSuccessors().Get(0), other);
1092 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1093 DCHECK_EQ(other->GetPredecessors().Get(0), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001094 DCHECK(other->GetPhis().IsEmpty());
1095
David Brazdil2d7352b2015-04-20 14:52:42 +01001096 // Move instructions from `other` to `this`.
1097 DCHECK(EndsWithControlFlowInstruction());
1098 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001099 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001100 other->instructions_.SetBlockOfInstructions(this);
1101 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001102
David Brazdil2d7352b2015-04-20 14:52:42 +01001103 // Remove `other` from the loops it is included in.
1104 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1105 HLoopInformation* loop_info = it.Current();
1106 loop_info->Remove(other);
1107 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001108 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01001109 }
1110 }
1111
1112 // Update links to the successors of `other`.
1113 successors_.Reset();
1114 while (!other->successors_.IsEmpty()) {
1115 HBasicBlock* successor = other->successors_.Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001116 successor->ReplacePredecessor(other, this);
1117 }
1118
David Brazdil2d7352b2015-04-20 14:52:42 +01001119 // Update the dominator tree.
1120 dominated_blocks_.Delete(other);
1121 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1122 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1123 dominated_blocks_.Add(dominated);
1124 dominated->SetDominator(this);
1125 }
1126 other->dominated_blocks_.Reset();
1127 other->dominator_ = nullptr;
1128
1129 // Clear the list of predecessors of `other` in preparation of deleting it.
1130 other->predecessors_.Reset();
1131
1132 // Delete `other` from the graph. The function updates reverse post order.
1133 graph_->DeleteDeadBlock(other);
1134 other->SetGraph(nullptr);
1135}
1136
1137void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1138 DCHECK_NE(GetGraph(), other->GetGraph());
1139 DCHECK(GetDominatedBlocks().IsEmpty());
1140 DCHECK(GetSuccessors().IsEmpty());
1141 DCHECK(!EndsWithControlFlowInstruction());
1142 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1143 DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock());
1144 DCHECK(other->GetPhis().IsEmpty());
1145 DCHECK(!other->IsInLoop());
1146
1147 // Move instructions from `other` to `this`.
1148 instructions_.Add(other->GetInstructions());
1149 other->instructions_.SetBlockOfInstructions(this);
1150
1151 // Update links to the successors of `other`.
1152 successors_.Reset();
1153 while (!other->successors_.IsEmpty()) {
1154 HBasicBlock* successor = other->successors_.Get(0);
1155 successor->ReplacePredecessor(other, this);
1156 }
1157
1158 // Update the dominator tree.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001159 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1160 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1161 dominated_blocks_.Add(dominated);
1162 dominated->SetDominator(this);
1163 }
1164 other->dominated_blocks_.Reset();
1165 other->dominator_ = nullptr;
1166 other->graph_ = nullptr;
1167}
1168
1169void HBasicBlock::ReplaceWith(HBasicBlock* other) {
1170 while (!GetPredecessors().IsEmpty()) {
1171 HBasicBlock* predecessor = GetPredecessors().Get(0);
1172 predecessor->ReplaceSuccessor(this, other);
1173 }
1174 while (!GetSuccessors().IsEmpty()) {
1175 HBasicBlock* successor = GetSuccessors().Get(0);
1176 successor->ReplacePredecessor(this, other);
1177 }
1178 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
1179 other->AddDominatedBlock(dominated_blocks_.Get(i));
1180 }
1181 GetDominator()->ReplaceDominatedBlock(this, other);
1182 other->SetDominator(GetDominator());
1183 dominator_ = nullptr;
1184 graph_ = nullptr;
1185}
1186
1187// Create space in `blocks` for adding `number_of_new_blocks` entries
1188// starting at location `at`. Blocks after `at` are moved accordingly.
1189static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
1190 size_t number_of_new_blocks,
1191 size_t at) {
1192 size_t old_size = blocks->Size();
1193 size_t new_size = old_size + number_of_new_blocks;
1194 blocks->SetSize(new_size);
1195 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
1196 blocks->Put(j, blocks->Get(i));
1197 }
1198}
1199
David Brazdil2d7352b2015-04-20 14:52:42 +01001200void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1201 DCHECK_EQ(block->GetGraph(), this);
1202 DCHECK(block->GetSuccessors().IsEmpty());
1203 DCHECK(block->GetPredecessors().IsEmpty());
1204 DCHECK(block->GetDominatedBlocks().IsEmpty());
1205 DCHECK(block->GetDominator() == nullptr);
1206
1207 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1208 block->RemoveInstruction(it.Current());
1209 }
1210 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1211 block->RemovePhi(it.Current()->AsPhi());
1212 }
1213
1214 reverse_post_order_.Delete(block);
1215 blocks_.Put(block->GetBlockId(), nullptr);
1216}
1217
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001218void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001219 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001220 // Simple case of an entry block, a body block, and an exit block.
1221 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001222 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001223 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
1224 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001225 DCHECK(!body->IsExitBlock());
1226 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001227
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001228 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1229 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001230
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001231 // Replace the invoke with the return value of the inlined graph.
1232 if (last->IsReturn()) {
1233 invoke->ReplaceWith(last->InputAt(0));
1234 } else {
1235 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001236 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001237
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001238 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001239 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001240 // Need to inline multiple blocks. We split `invoke`'s block
1241 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001242 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001243 // with the second half.
1244 ArenaAllocator* allocator = outer_graph->GetArena();
1245 HBasicBlock* at = invoke->GetBlock();
1246 HBasicBlock* to = at->SplitAfter(invoke);
1247
1248 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
1249 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001250 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001251 exit_block_->ReplaceWith(to);
1252
1253 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001254 // to not `HReturn` but `HGoto` instead.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001255 HInstruction* return_value = nullptr;
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001256 bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
1257 if (to->GetPredecessors().Size() == 1) {
1258 HBasicBlock* predecessor = to->GetPredecessors().Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001259 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001260 if (!returns_void) {
1261 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001262 }
1263 predecessor->AddInstruction(new (allocator) HGoto());
1264 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001265 } else {
1266 if (!returns_void) {
1267 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001268 return_value = new (allocator) HPhi(
1269 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001270 to->AddPhi(return_value->AsPhi());
1271 }
1272 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
1273 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
1274 HInstruction* last = predecessor->GetLastInstruction();
1275 if (!returns_void) {
1276 return_value->AsPhi()->AddInput(last->InputAt(0));
1277 }
1278 predecessor->AddInstruction(new (allocator) HGoto());
1279 predecessor->RemoveInstruction(last);
1280 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001281 }
1282
1283 if (return_value != nullptr) {
1284 invoke->ReplaceWith(return_value);
1285 }
1286
1287 // Update the meta information surrounding blocks:
1288 // (1) the graph they are now in,
1289 // (2) the reverse post order of that graph,
1290 // (3) the potential loop information they are now in.
1291
1292 // We don't add the entry block, the exit block, and the first block, which
1293 // has been merged with `at`.
1294 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1295
1296 // We add the `to` block.
1297 static constexpr int kNumberOfNewBlocksInCaller = 1;
1298 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
1299 + kNumberOfNewBlocksInCaller;
1300
1301 // Find the location of `at` in the outer graph's reverse post order. The new
1302 // blocks will be added after it.
1303 size_t index_of_at = 0;
1304 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
1305 index_of_at++;
1306 }
1307 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1308
1309 // Do a reverse post order of the blocks in the callee and do (1), (2),
1310 // and (3) to the blocks that apply.
1311 HLoopInformation* info = at->GetLoopInformation();
1312 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1313 HBasicBlock* current = it.Current();
1314 if (current != exit_block_ && current != entry_block_ && current != first) {
1315 DCHECK(!current->IsInLoop());
1316 DCHECK(current->GetGraph() == this);
1317 current->SetGraph(outer_graph);
1318 outer_graph->AddBlock(current);
1319 outer_graph->reverse_post_order_.Put(++index_of_at, current);
1320 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001321 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001322 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1323 loop_it.Current()->Add(current);
1324 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001325 }
1326 }
1327 }
1328
1329 // Do (1), (2), and (3) to `to`.
1330 to->SetGraph(outer_graph);
1331 outer_graph->AddBlock(to);
1332 outer_graph->reverse_post_order_.Put(++index_of_at, to);
1333 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001334 to->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001335 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1336 loop_it.Current()->Add(to);
1337 }
David Brazdil46e2a392015-03-16 17:31:52 +00001338 if (info->IsBackEdge(*at)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001339 // Only `to` can become a back edge, as the inlined blocks
1340 // are predecessors of `to`.
1341 info->ReplaceBackEdge(at, to);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001342 }
1343 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001344 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001345
David Brazdil05144f42015-04-16 15:18:00 +01001346 // Update the next instruction id of the outer graph, so that instructions
1347 // added later get bigger ids than those in the inner graph.
1348 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1349
1350 // Walk over the entry block and:
1351 // - Move constants from the entry block to the outer_graph's entry block,
1352 // - Replace HParameterValue instructions with their real value.
1353 // - Remove suspend checks, that hold an environment.
1354 // We must do this after the other blocks have been inlined, otherwise ids of
1355 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001356 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001357 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1358 HInstruction* current = it.Current();
1359 if (current->IsNullConstant()) {
1360 current->ReplaceWith(outer_graph->GetNullConstant());
1361 } else if (current->IsIntConstant()) {
1362 current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue()));
1363 } else if (current->IsLongConstant()) {
1364 current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue()));
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001365 } else if (current->IsFloatConstant()) {
1366 current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue()));
1367 } else if (current->IsDoubleConstant()) {
1368 current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue()));
David Brazdil05144f42015-04-16 15:18:00 +01001369 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001370 if (kIsDebugBuild
1371 && invoke->IsInvokeStaticOrDirect()
1372 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1373 // Ensure we do not use the last input of `invoke`, as it
1374 // contains a clinit check which is not an actual argument.
1375 size_t last_input_index = invoke->InputCount() - 1;
1376 DCHECK(parameter_index != last_input_index);
1377 }
David Brazdil05144f42015-04-16 15:18:00 +01001378 current->ReplaceWith(invoke->InputAt(parameter_index++));
1379 } else {
1380 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1381 entry_block_->RemoveInstruction(current);
1382 }
1383 }
1384
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001385 // Finally remove the invoke from the caller.
1386 invoke->GetBlock()->RemoveInstruction(invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001387}
1388
Calin Juravleacf735c2015-02-12 15:25:22 +00001389std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1390 ScopedObjectAccess soa(Thread::Current());
1391 os << "["
1392 << " is_top=" << rhs.IsTop()
1393 << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
1394 << " is_exact=" << rhs.IsExact()
1395 << " ]";
1396 return os;
1397}
1398
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001399} // namespace art