blob: 2bad68217b83437fac079e2a06f97edd7b75d7df [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 Brazdile8ff50d2015-05-07 09:59:30 +010020#include "base/bit_vector-inl.h"
Vladimir Marko41b175a2015-05-19 18:08:00 +010021#include "base/bit_utils.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000022#include "utils/growable_array.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000023#include "scoped_thread_state_change.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000024
25namespace art {
26
27void HGraph::AddBlock(HBasicBlock* block) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000028 block->SetBlockId(blocks_.Size());
Nicolas Geoffray818f2102014-02-18 16:43:35 +000029 blocks_.Add(block);
30}
31
Nicolas Geoffray804d0932014-05-02 08:46:00 +010032void HGraph::FindBackEdges(ArenaBitVector* visited) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000033 ArenaBitVector visiting(arena_, blocks_.Size(), false);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034 VisitBlockForBackEdges(entry_block_, visited, &visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000035}
36
Roland Levillainfc600dc2014-12-02 17:16:31 +000037static void RemoveAsUser(HInstruction* instruction) {
38 for (size_t i = 0; i < instruction->InputCount(); i++) {
David Brazdil1abb4192015-02-17 18:33:36 +000039 instruction->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000040 }
41
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010042 for (HEnvironment* environment = instruction->GetEnvironment();
43 environment != nullptr;
44 environment = environment->GetParent()) {
Roland Levillainfc600dc2014-12-02 17:16:31 +000045 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
David Brazdil1abb4192015-02-17 18:33:36 +000046 if (environment->GetInstructionAt(i) != nullptr) {
47 environment->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000048 }
49 }
50 }
51}
52
53void HGraph::RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const {
54 for (size_t i = 0; i < blocks_.Size(); ++i) {
55 if (!visited.IsBitSet(i)) {
56 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010057 DCHECK(block->GetPhis().IsEmpty()) << "Phis are not inserted at this stage";
Roland Levillainfc600dc2014-12-02 17:16:31 +000058 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
59 RemoveAsUser(it.Current());
60 }
61 }
62 }
63}
64
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010065void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010066 for (size_t i = 0; i < blocks_.Size(); ++i) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000067 if (!visited.IsBitSet(i)) {
David Brazdil1abb4192015-02-17 18:33:36 +000068 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010069 // We only need to update the successor, which might be live.
David Brazdil1abb4192015-02-17 18:33:36 +000070 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
71 block->GetSuccessors().Get(j)->RemovePredecessor(block);
72 }
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010073 // Remove the block from the list of blocks, so that further analyses
74 // never see it.
75 blocks_.Put(i, nullptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000076 }
77 }
78}
79
80void HGraph::VisitBlockForBackEdges(HBasicBlock* block,
81 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +010082 ArenaBitVector* visiting) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000083 int id = block->GetBlockId();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000084 if (visited->IsBitSet(id)) return;
85
86 visited->SetBit(id);
87 visiting->SetBit(id);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010088 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
89 HBasicBlock* successor = block->GetSuccessors().Get(i);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000090 if (visiting->IsBitSet(successor->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000091 successor->AddBackEdge(block);
92 } else {
93 VisitBlockForBackEdges(successor, visited, visiting);
94 }
95 }
96 visiting->ClearBit(id);
97}
98
99void HGraph::BuildDominatorTree() {
100 ArenaBitVector visited(arena_, blocks_.Size(), false);
101
102 // (1) Find the back edges in the graph doing a DFS traversal.
103 FindBackEdges(&visited);
104
Roland Levillainfc600dc2014-12-02 17:16:31 +0000105 // (2) Remove instructions and phis from blocks not visited during
106 // the initial DFS as users from other instructions, so that
107 // users can be safely removed before uses later.
108 RemoveInstructionsAsUsersFromDeadBlocks(visited);
109
110 // (3) Remove blocks not visited during the initial DFS.
111 // Step (4) requires dead blocks to be removed from the
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000112 // predecessors list of live blocks.
113 RemoveDeadBlocks(visited);
114
Roland Levillainfc600dc2014-12-02 17:16:31 +0000115 // (4) Simplify the CFG now, so that we don't need to recompute
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100116 // dominators and the reverse post order.
117 SimplifyCFG();
118
Roland Levillainfc600dc2014-12-02 17:16:31 +0000119 // (5) Compute the immediate dominator of each block. We visit
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000120 // the successors of a block only when all its forward branches
121 // have been processed.
122 GrowableArray<size_t> visits(arena_, blocks_.Size());
123 visits.SetSize(blocks_.Size());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100124 reverse_post_order_.Add(entry_block_);
125 for (size_t i = 0; i < entry_block_->GetSuccessors().Size(); i++) {
126 VisitBlockForDominatorTree(entry_block_->GetSuccessors().Get(i), entry_block_, &visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000127 }
128}
129
130HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
131 ArenaBitVector visited(arena_, blocks_.Size(), false);
132 // Walk the dominator tree of the first block and mark the visited blocks.
133 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000134 visited.SetBit(first->GetBlockId());
135 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000136 }
137 // Walk the dominator tree of the second block until a marked block is found.
138 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000139 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000140 return second;
141 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000142 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000143 }
144 LOG(ERROR) << "Could not find common dominator";
145 return nullptr;
146}
147
148void HGraph::VisitBlockForDominatorTree(HBasicBlock* block,
149 HBasicBlock* predecessor,
150 GrowableArray<size_t>* visits) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000151 if (block->GetDominator() == nullptr) {
152 block->SetDominator(predecessor);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000153 } else {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000154 block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000155 }
156
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000157 visits->Increment(block->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000158 // Once all the forward edges have been visited, we know the immediate
159 // dominator of the block. We can then start visiting its successors.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000160 if (visits->Get(block->GetBlockId()) ==
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100161 block->GetPredecessors().Size() - block->NumberOfBackEdges()) {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100162 block->GetDominator()->AddDominatedBlock(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100163 reverse_post_order_.Add(block);
164 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
165 VisitBlockForDominatorTree(block->GetSuccessors().Get(i), block, visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000166 }
167 }
168}
169
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000170void HGraph::TransformToSsa() {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100171 DCHECK(!reverse_post_order_.IsEmpty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100172 SsaBuilder ssa_builder(this);
173 ssa_builder.BuildSsa();
174}
175
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100176void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) {
177 // Insert a new node between `block` and `successor` to split the
178 // critical edge.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100179 HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100180 AddBlock(new_block);
181 new_block->AddInstruction(new (arena_) HGoto());
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100182 block->ReplaceSuccessor(successor, new_block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100183 new_block->AddSuccessor(successor);
184 if (successor->IsLoopHeader()) {
185 // If we split at a back edge boundary, make the new block the back edge.
186 HLoopInformation* info = successor->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000187 if (info->IsBackEdge(*block)) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100188 info->RemoveBackEdge(block);
189 info->AddBackEdge(new_block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100190 }
191 }
192}
193
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100194void HGraph::SimplifyLoop(HBasicBlock* header) {
195 HLoopInformation* info = header->GetLoopInformation();
196
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100197 // Make sure the loop has only one pre header. This simplifies SSA building by having
198 // to just look at the pre header to know which locals are initialized at entry of the
199 // loop.
200 size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges();
201 if (number_of_incomings != 1) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100202 HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100203 AddBlock(pre_header);
204 pre_header->AddInstruction(new (arena_) HGoto());
205
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100206 for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) {
207 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100208 if (!info->IsBackEdge(*predecessor)) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100209 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100210 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100211 }
212 }
213 pre_header->AddSuccessor(header);
214 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100215
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100216 // Make sure the first predecessor of a loop header is the incoming block.
217 if (info->IsBackEdge(*header->GetPredecessors().Get(0))) {
218 HBasicBlock* to_swap = header->GetPredecessors().Get(0);
219 for (size_t pred = 1, e = header->GetPredecessors().Size(); pred < e; ++pred) {
220 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
221 if (!info->IsBackEdge(*predecessor)) {
222 header->predecessors_.Put(pred, to_swap);
223 header->predecessors_.Put(0, predecessor);
224 break;
225 }
226 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100227 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100228
229 // Place the suspend check at the beginning of the header, so that live registers
230 // will be known when allocating registers. Note that code generation can still
231 // generate the suspend check at the back edge, but needs to be careful with
232 // loop phi spill slots (which are not written to at back edge).
233 HInstruction* first_instruction = header->GetFirstInstruction();
234 if (!first_instruction->IsSuspendCheck()) {
235 HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc());
236 header->InsertInstructionBefore(check, first_instruction);
237 first_instruction = check;
238 }
239 info->SetSuspendCheck(first_instruction->AsSuspendCheck());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100240}
241
242void HGraph::SimplifyCFG() {
243 // Simplify the CFG for future analysis, and code generation:
244 // (1): Split critical edges.
245 // (2): Simplify loops by having only one back edge, and one preheader.
246 for (size_t i = 0; i < blocks_.Size(); ++i) {
247 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100248 if (block == nullptr) continue;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100249 if (block->GetSuccessors().Size() > 1) {
250 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
251 HBasicBlock* successor = block->GetSuccessors().Get(j);
252 if (successor->GetPredecessors().Size() > 1) {
253 SplitCriticalEdge(block, successor);
254 --j;
255 }
256 }
257 }
258 if (block->IsLoopHeader()) {
259 SimplifyLoop(block);
260 }
261 }
262}
263
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000264bool HGraph::AnalyzeNaturalLoops() const {
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100265 // Order does not matter.
266 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
267 HBasicBlock* block = it.Current();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100268 if (block->IsLoopHeader()) {
269 HLoopInformation* info = block->GetLoopInformation();
270 if (!info->Populate()) {
271 // Abort if the loop is non natural. We currently bailout in such cases.
272 return false;
273 }
274 }
275 }
276 return true;
277}
278
David Brazdil8d5b8b22015-03-24 10:51:52 +0000279void HGraph::InsertConstant(HConstant* constant) {
280 // New constants are inserted before the final control-flow instruction
281 // of the graph, or at its end if called from the graph builder.
282 if (entry_block_->EndsWithControlFlowInstruction()) {
283 entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction());
David Brazdil46e2a392015-03-16 17:31:52 +0000284 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000285 entry_block_->AddInstruction(constant);
David Brazdil46e2a392015-03-16 17:31:52 +0000286 }
287}
288
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000289HNullConstant* HGraph::GetNullConstant() {
290 if (cached_null_constant_ == nullptr) {
291 cached_null_constant_ = new (arena_) HNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000292 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000293 }
294 return cached_null_constant_;
295}
296
David Brazdil8d5b8b22015-03-24 10:51:52 +0000297HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value) {
298 switch (type) {
299 case Primitive::Type::kPrimBoolean:
300 DCHECK(IsUint<1>(value));
301 FALLTHROUGH_INTENDED;
302 case Primitive::Type::kPrimByte:
303 case Primitive::Type::kPrimChar:
304 case Primitive::Type::kPrimShort:
305 case Primitive::Type::kPrimInt:
306 DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value));
307 return GetIntConstant(static_cast<int32_t>(value));
308
309 case Primitive::Type::kPrimLong:
310 return GetLongConstant(value);
311
312 default:
313 LOG(FATAL) << "Unsupported constant type";
314 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000315 }
David Brazdil46e2a392015-03-16 17:31:52 +0000316}
317
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000318void HGraph::CacheFloatConstant(HFloatConstant* constant) {
319 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
320 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
321 cached_float_constants_.Overwrite(value, constant);
322}
323
324void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
325 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
326 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
327 cached_double_constants_.Overwrite(value, constant);
328}
329
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000330void HLoopInformation::Add(HBasicBlock* block) {
331 blocks_.SetBit(block->GetBlockId());
332}
333
David Brazdil46e2a392015-03-16 17:31:52 +0000334void HLoopInformation::Remove(HBasicBlock* block) {
335 blocks_.ClearBit(block->GetBlockId());
336}
337
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100338void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
339 if (blocks_.IsBitSet(block->GetBlockId())) {
340 return;
341 }
342
343 blocks_.SetBit(block->GetBlockId());
344 block->SetInLoop(this);
345 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
346 PopulateRecursive(block->GetPredecessors().Get(i));
347 }
348}
349
350bool HLoopInformation::Populate() {
David Brazdile8ff50d2015-05-07 09:59:30 +0100351 DCHECK_EQ(blocks_.NumSetBits(), 0u) << "Loop information has already been populated";
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100352 for (size_t i = 0, e = GetBackEdges().Size(); i < e; ++i) {
353 HBasicBlock* back_edge = GetBackEdges().Get(i);
354 DCHECK(back_edge->GetDominator() != nullptr);
355 if (!header_->Dominates(back_edge)) {
356 // This loop is not natural. Do not bother going further.
357 return false;
358 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100360 // Populate this loop: starting with the back edge, recursively add predecessors
361 // that are not already part of that loop. Set the header as part of the loop
362 // to end the recursion.
363 // This is a recursive implementation of the algorithm described in
364 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
365 blocks_.SetBit(header_->GetBlockId());
366 PopulateRecursive(back_edge);
367 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100368 return true;
369}
370
David Brazdile8ff50d2015-05-07 09:59:30 +0100371void HLoopInformation::Update() {
372 HGraph* graph = header_->GetGraph();
373 for (uint32_t id : blocks_.Indexes()) {
374 HBasicBlock* block = graph->GetBlocks().Get(id);
375 // Reset loop information of non-header blocks inside the loop, except
376 // members of inner nested loops because those should already have been
377 // updated by their own LoopInformation.
378 if (block->GetLoopInformation() == this && block != header_) {
379 block->SetLoopInformation(nullptr);
380 }
381 }
382 blocks_.ClearAllBits();
383
384 if (back_edges_.IsEmpty()) {
385 // The loop has been dismantled, delete its suspend check and remove info
386 // from the header.
387 DCHECK(HasSuspendCheck());
388 header_->RemoveInstruction(suspend_check_);
389 header_->SetLoopInformation(nullptr);
390 header_ = nullptr;
391 suspend_check_ = nullptr;
392 } else {
393 if (kIsDebugBuild) {
394 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
395 DCHECK(header_->Dominates(back_edges_.Get(i)));
396 }
397 }
398 // This loop still has reachable back edges. Repopulate the list of blocks.
399 bool populate_successful = Populate();
400 DCHECK(populate_successful);
401 }
402}
403
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100404HBasicBlock* HLoopInformation::GetPreHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100405 return header_->GetDominator();
406}
407
408bool HLoopInformation::Contains(const HBasicBlock& block) const {
409 return blocks_.IsBitSet(block.GetBlockId());
410}
411
412bool HLoopInformation::IsIn(const HLoopInformation& other) const {
413 return other.blocks_.IsBitSet(header_->GetBlockId());
414}
415
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100416size_t HLoopInformation::GetLifetimeEnd() const {
417 size_t last_position = 0;
418 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
419 last_position = std::max(back_edges_.Get(i)->GetLifetimeEnd(), last_position);
420 }
421 return last_position;
422}
423
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100424bool HBasicBlock::Dominates(HBasicBlock* other) const {
425 // Walk up the dominator tree from `other`, to find out if `this`
426 // is an ancestor.
427 HBasicBlock* current = other;
428 while (current != nullptr) {
429 if (current == this) {
430 return true;
431 }
432 current = current->GetDominator();
433 }
434 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100435}
436
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100437static void UpdateInputsUsers(HInstruction* instruction) {
438 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
439 instruction->InputAt(i)->AddUseAt(instruction, i);
440 }
441 // Environment should be created later.
442 DCHECK(!instruction->HasEnvironment());
443}
444
Roland Levillainccc07a92014-09-16 14:48:16 +0100445void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
446 HInstruction* replacement) {
447 DCHECK(initial->GetBlock() == this);
448 InsertInstructionBefore(replacement, initial);
449 initial->ReplaceWith(replacement);
450 RemoveInstruction(initial);
451}
452
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100453static void Add(HInstructionList* instruction_list,
454 HBasicBlock* block,
455 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000456 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000457 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100458 instruction->SetBlock(block);
459 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100460 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100461 instruction_list->AddInstruction(instruction);
462}
463
464void HBasicBlock::AddInstruction(HInstruction* instruction) {
465 Add(&instructions_, this, instruction);
466}
467
468void HBasicBlock::AddPhi(HPhi* phi) {
469 Add(&phis_, this, phi);
470}
471
David Brazdilc3d743f2015-04-22 13:40:50 +0100472void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
473 DCHECK(!cursor->IsPhi());
474 DCHECK(!instruction->IsPhi());
475 DCHECK_EQ(instruction->GetId(), -1);
476 DCHECK_NE(cursor->GetId(), -1);
477 DCHECK_EQ(cursor->GetBlock(), this);
478 DCHECK(!instruction->IsControlFlow());
479 instruction->SetBlock(this);
480 instruction->SetId(GetGraph()->GetNextInstructionId());
481 UpdateInputsUsers(instruction);
482 instructions_.InsertInstructionBefore(instruction, cursor);
483}
484
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100485void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
486 DCHECK(!cursor->IsPhi());
487 DCHECK(!instruction->IsPhi());
488 DCHECK_EQ(instruction->GetId(), -1);
489 DCHECK_NE(cursor->GetId(), -1);
490 DCHECK_EQ(cursor->GetBlock(), this);
491 DCHECK(!instruction->IsControlFlow());
492 DCHECK(!cursor->IsControlFlow());
493 instruction->SetBlock(this);
494 instruction->SetId(GetGraph()->GetNextInstructionId());
495 UpdateInputsUsers(instruction);
496 instructions_.InsertInstructionAfter(instruction, cursor);
497}
498
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100499void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
500 DCHECK_EQ(phi->GetId(), -1);
501 DCHECK_NE(cursor->GetId(), -1);
502 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100503 phi->SetBlock(this);
504 phi->SetId(GetGraph()->GetNextInstructionId());
505 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100506 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100507}
508
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100509static void Remove(HInstructionList* instruction_list,
510 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000511 HInstruction* instruction,
512 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100513 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100514 instruction->SetBlock(nullptr);
515 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000516 if (ensure_safety) {
517 DCHECK(instruction->GetUses().IsEmpty());
518 DCHECK(instruction->GetEnvUses().IsEmpty());
519 RemoveAsUser(instruction);
520 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100521}
522
David Brazdil1abb4192015-02-17 18:33:36 +0000523void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100524 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000525 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100526}
527
David Brazdil1abb4192015-02-17 18:33:36 +0000528void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
529 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100530}
531
David Brazdilc7508e92015-04-27 13:28:57 +0100532void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
533 if (instruction->IsPhi()) {
534 RemovePhi(instruction->AsPhi(), ensure_safety);
535 } else {
536 RemoveInstruction(instruction, ensure_safety);
537 }
538}
539
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +0100540void HEnvironment::CopyFrom(const GrowableArray<HInstruction*>& locals) {
541 for (size_t i = 0; i < locals.Size(); i++) {
542 HInstruction* instruction = locals.Get(i);
543 SetRawEnvAt(i, instruction);
544 if (instruction != nullptr) {
545 instruction->AddEnvUseAt(this, i);
546 }
547 }
548}
549
David Brazdiled596192015-01-23 10:39:45 +0000550void HEnvironment::CopyFrom(HEnvironment* env) {
551 for (size_t i = 0; i < env->Size(); i++) {
552 HInstruction* instruction = env->GetInstructionAt(i);
553 SetRawEnvAt(i, instruction);
554 if (instruction != nullptr) {
555 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100556 }
David Brazdiled596192015-01-23 10:39:45 +0000557 }
558}
559
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700560void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
561 HBasicBlock* loop_header) {
562 DCHECK(loop_header->IsLoopHeader());
563 for (size_t i = 0; i < env->Size(); i++) {
564 HInstruction* instruction = env->GetInstructionAt(i);
565 SetRawEnvAt(i, instruction);
566 if (instruction == nullptr) {
567 continue;
568 }
569 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
570 // At the end of the loop pre-header, the corresponding value for instruction
571 // is the first input of the phi.
572 HInstruction* initial = instruction->AsPhi()->InputAt(0);
573 DCHECK(initial->GetBlock()->Dominates(loop_header));
574 SetRawEnvAt(i, initial);
575 initial->AddEnvUseAt(this, i);
576 } else {
577 instruction->AddEnvUseAt(this, i);
578 }
579 }
580}
581
David Brazdil1abb4192015-02-17 18:33:36 +0000582void HEnvironment::RemoveAsUserOfInput(size_t index) const {
583 const HUserRecord<HEnvironment*> user_record = vregs_.Get(index);
584 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100585}
586
Calin Juravle77520bc2015-01-12 18:45:46 +0000587HInstruction* HInstruction::GetNextDisregardingMoves() const {
588 HInstruction* next = GetNext();
589 while (next != nullptr && next->IsParallelMove()) {
590 next = next->GetNext();
591 }
592 return next;
593}
594
595HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
596 HInstruction* previous = GetPrevious();
597 while (previous != nullptr && previous->IsParallelMove()) {
598 previous = previous->GetPrevious();
599 }
600 return previous;
601}
602
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100603void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000604 if (first_instruction_ == nullptr) {
605 DCHECK(last_instruction_ == nullptr);
606 first_instruction_ = last_instruction_ = instruction;
607 } else {
608 last_instruction_->next_ = instruction;
609 instruction->previous_ = last_instruction_;
610 last_instruction_ = instruction;
611 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000612}
613
David Brazdilc3d743f2015-04-22 13:40:50 +0100614void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
615 DCHECK(Contains(cursor));
616 if (cursor == first_instruction_) {
617 cursor->previous_ = instruction;
618 instruction->next_ = cursor;
619 first_instruction_ = instruction;
620 } else {
621 instruction->previous_ = cursor->previous_;
622 instruction->next_ = cursor;
623 cursor->previous_ = instruction;
624 instruction->previous_->next_ = instruction;
625 }
626}
627
628void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
629 DCHECK(Contains(cursor));
630 if (cursor == last_instruction_) {
631 cursor->next_ = instruction;
632 instruction->previous_ = cursor;
633 last_instruction_ = instruction;
634 } else {
635 instruction->next_ = cursor->next_;
636 instruction->previous_ = cursor;
637 cursor->next_ = instruction;
638 instruction->next_->previous_ = instruction;
639 }
640}
641
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100642void HInstructionList::RemoveInstruction(HInstruction* instruction) {
643 if (instruction->previous_ != nullptr) {
644 instruction->previous_->next_ = instruction->next_;
645 }
646 if (instruction->next_ != nullptr) {
647 instruction->next_->previous_ = instruction->previous_;
648 }
649 if (instruction == first_instruction_) {
650 first_instruction_ = instruction->next_;
651 }
652 if (instruction == last_instruction_) {
653 last_instruction_ = instruction->previous_;
654 }
655}
656
Roland Levillain6b469232014-09-25 10:10:38 +0100657bool HInstructionList::Contains(HInstruction* instruction) const {
658 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
659 if (it.Current() == instruction) {
660 return true;
661 }
662 }
663 return false;
664}
665
Roland Levillainccc07a92014-09-16 14:48:16 +0100666bool HInstructionList::FoundBefore(const HInstruction* instruction1,
667 const HInstruction* instruction2) const {
668 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
669 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
670 if (it.Current() == instruction1) {
671 return true;
672 }
673 if (it.Current() == instruction2) {
674 return false;
675 }
676 }
677 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
678 return true;
679}
680
Roland Levillain6c82d402014-10-13 16:10:27 +0100681bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
682 if (other_instruction == this) {
683 // An instruction does not strictly dominate itself.
684 return false;
685 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100686 HBasicBlock* block = GetBlock();
687 HBasicBlock* other_block = other_instruction->GetBlock();
688 if (block != other_block) {
689 return GetBlock()->Dominates(other_instruction->GetBlock());
690 } else {
691 // If both instructions are in the same block, ensure this
692 // instruction comes before `other_instruction`.
693 if (IsPhi()) {
694 if (!other_instruction->IsPhi()) {
695 // Phis appear before non phi-instructions so this instruction
696 // dominates `other_instruction`.
697 return true;
698 } else {
699 // There is no order among phis.
700 LOG(FATAL) << "There is no dominance between phis of a same block.";
701 return false;
702 }
703 } else {
704 // `this` is not a phi.
705 if (other_instruction->IsPhi()) {
706 // Phis appear before non phi-instructions so this instruction
707 // does not dominate `other_instruction`.
708 return false;
709 } else {
710 // Check whether this instruction comes before
711 // `other_instruction` in the instruction list.
712 return block->GetInstructions().FoundBefore(this, other_instruction);
713 }
714 }
715 }
716}
717
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100718void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100719 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000720 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
721 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100722 HInstruction* user = current->GetUser();
723 size_t input_index = current->GetIndex();
724 user->SetRawInputAt(input_index, other);
725 other->AddUseAt(user, input_index);
726 }
727
David Brazdiled596192015-01-23 10:39:45 +0000728 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
729 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100730 HEnvironment* user = current->GetUser();
731 size_t input_index = current->GetIndex();
732 user->SetRawEnvAt(input_index, other);
733 other->AddEnvUseAt(user, input_index);
734 }
735
David Brazdiled596192015-01-23 10:39:45 +0000736 uses_.Clear();
737 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100738}
739
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100740void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000741 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100742 SetRawInputAt(index, replacement);
743 replacement->AddUseAt(this, index);
744}
745
Nicolas Geoffray39468442014-09-02 15:17:15 +0100746size_t HInstruction::EnvironmentSize() const {
747 return HasEnvironment() ? environment_->Size() : 0;
748}
749
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100750void HPhi::AddInput(HInstruction* input) {
751 DCHECK(input->GetBlock() != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +0000752 inputs_.Add(HUserRecord<HInstruction*>(input));
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100753 input->AddUseAt(this, inputs_.Size() - 1);
754}
755
David Brazdil2d7352b2015-04-20 14:52:42 +0100756void HPhi::RemoveInputAt(size_t index) {
757 RemoveAsUserOfInput(index);
758 inputs_.DeleteAt(index);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100759 for (size_t i = index, e = InputCount(); i < e; ++i) {
760 InputRecordAt(i).GetUseNode()->SetIndex(i);
761 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100762}
763
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100764#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000765void H##name::Accept(HGraphVisitor* visitor) { \
766 visitor->Visit##name(this); \
767}
768
769FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
770
771#undef DEFINE_ACCEPT
772
773void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100774 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
775 for (size_t i = 0 ; i < blocks.Size(); i++) {
David Brazdil46e2a392015-03-16 17:31:52 +0000776 HBasicBlock* block = blocks.Get(i);
777 if (block != nullptr) {
778 VisitBasicBlock(block);
779 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000780 }
781}
782
Roland Levillain633021e2014-10-01 14:12:25 +0100783void HGraphVisitor::VisitReversePostOrder() {
784 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
785 VisitBasicBlock(it.Current());
786 }
787}
788
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000789void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100790 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100791 it.Current()->Accept(this);
792 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100793 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000794 it.Current()->Accept(this);
795 }
796}
797
Roland Levillain9240d6a2014-10-20 16:47:04 +0100798HConstant* HUnaryOperation::TryStaticEvaluation() const {
799 if (GetInput()->IsIntConstant()) {
800 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000801 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain9240d6a2014-10-20 16:47:04 +0100802 } else if (GetInput()->IsLongConstant()) {
Roland Levillainb762d2e2014-10-22 10:11:06 +0100803 // TODO: Implement static evaluation of long unary operations.
804 //
805 // Do not exit with a fatal condition here. Instead, simply
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700806 // return `null' to notify the caller that this instruction
Roland Levillainb762d2e2014-10-22 10:11:06 +0100807 // cannot (yet) be statically evaluated.
Roland Levillain9240d6a2014-10-20 16:47:04 +0100808 return nullptr;
809 }
810 return nullptr;
811}
812
813HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +0100814 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
815 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
816 GetRight()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000817 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100818 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
819 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
820 GetRight()->AsLongConstant()->GetValue());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000821 if (GetResultType() == Primitive::kPrimLong) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000822 return GetBlock()->GetGraph()->GetLongConstant(value);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000823 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000824 DCHECK_EQ(GetResultType(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000825 return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000826 }
Roland Levillain556c3d12014-09-18 15:25:07 +0100827 }
828 return nullptr;
829}
Dave Allison20dfc792014-06-16 20:44:29 -0700830
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000831HConstant* HBinaryOperation::GetConstantRight() const {
832 if (GetRight()->IsConstant()) {
833 return GetRight()->AsConstant();
834 } else if (IsCommutative() && GetLeft()->IsConstant()) {
835 return GetLeft()->AsConstant();
836 } else {
837 return nullptr;
838 }
839}
840
841// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700842// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000843HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
844 HInstruction* most_constant_right = GetConstantRight();
845 if (most_constant_right == nullptr) {
846 return nullptr;
847 } else if (most_constant_right == GetLeft()) {
848 return GetRight();
849 } else {
850 return GetLeft();
851 }
852}
853
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700854bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
855 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100856}
857
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100858bool HInstruction::Equals(HInstruction* other) const {
859 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100860 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100861 if (!InstructionDataEquals(other)) return false;
862 if (GetType() != other->GetType()) return false;
863 if (InputCount() != other->InputCount()) return false;
864
865 for (size_t i = 0, e = InputCount(); i < e; ++i) {
866 if (InputAt(i) != other->InputAt(i)) return false;
867 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100868 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100869 return true;
870}
871
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700872std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
873#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
874 switch (rhs) {
875 FOR_EACH_INSTRUCTION(DECLARE_CASE)
876 default:
877 os << "Unknown instruction kind " << static_cast<int>(rhs);
878 break;
879 }
880#undef DECLARE_CASE
881 return os;
882}
883
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000884void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000885 next_->previous_ = previous_;
886 if (previous_ != nullptr) {
887 previous_->next_ = next_;
888 }
889 if (block_->instructions_.first_instruction_ == this) {
890 block_->instructions_.first_instruction_ = next_;
891 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000892 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000893
894 previous_ = cursor->previous_;
895 if (previous_ != nullptr) {
896 previous_->next_ = this;
897 }
898 next_ = cursor;
899 cursor->previous_ = this;
900 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000901
902 if (block_->instructions_.first_instruction_ == cursor) {
903 block_->instructions_.first_instruction_ = this;
904 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000905}
906
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000907HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
908 DCHECK(!cursor->IsControlFlow());
909 DCHECK_NE(instructions_.last_instruction_, cursor);
910 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000911
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000912 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
913 new_block->instructions_.first_instruction_ = cursor->GetNext();
914 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
915 cursor->next_->previous_ = nullptr;
916 cursor->next_ = nullptr;
917 instructions_.last_instruction_ = cursor;
918
919 new_block->instructions_.SetBlockOfInstructions(new_block);
920 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
921 HBasicBlock* successor = GetSuccessors().Get(i);
922 new_block->successors_.Add(successor);
923 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
924 }
925 successors_.Reset();
926
927 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
928 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
929 dominated->dominator_ = new_block;
930 new_block->dominated_blocks_.Add(dominated);
931 }
932 dominated_blocks_.Reset();
933 return new_block;
934}
935
David Brazdil46e2a392015-03-16 17:31:52 +0000936bool HBasicBlock::IsSingleGoto() const {
937 HLoopInformation* loop_info = GetLoopInformation();
938 // TODO: Remove the null check b/19084197.
939 return GetFirstInstruction() != nullptr
940 && GetPhis().IsEmpty()
941 && GetFirstInstruction() == GetLastInstruction()
942 && GetLastInstruction()->IsGoto()
943 // Back edges generate the suspend check.
944 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
945}
946
David Brazdil8d5b8b22015-03-24 10:51:52 +0000947bool HBasicBlock::EndsWithControlFlowInstruction() const {
948 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
949}
950
David Brazdilb2bd1c52015-03-25 11:17:37 +0000951bool HBasicBlock::EndsWithIf() const {
952 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
953}
954
955bool HBasicBlock::HasSinglePhi() const {
956 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
957}
958
David Brazdil2d7352b2015-04-20 14:52:42 +0100959size_t HInstructionList::CountSize() const {
960 size_t size = 0;
961 HInstruction* current = first_instruction_;
962 for (; current != nullptr; current = current->GetNext()) {
963 size++;
964 }
965 return size;
966}
967
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000968void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
969 for (HInstruction* current = first_instruction_;
970 current != nullptr;
971 current = current->GetNext()) {
972 current->SetBlock(block);
973 }
974}
975
976void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
977 DCHECK(Contains(cursor));
978 if (!instruction_list.IsEmpty()) {
979 if (cursor == last_instruction_) {
980 last_instruction_ = instruction_list.last_instruction_;
981 } else {
982 cursor->next_->previous_ = instruction_list.last_instruction_;
983 }
984 instruction_list.last_instruction_->next_ = cursor->next_;
985 cursor->next_ = instruction_list.first_instruction_;
986 instruction_list.first_instruction_->previous_ = cursor;
987 }
988}
989
990void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +0000991 if (IsEmpty()) {
992 first_instruction_ = instruction_list.first_instruction_;
993 last_instruction_ = instruction_list.last_instruction_;
994 } else {
995 AddAfter(last_instruction_, instruction_list);
996 }
997}
998
David Brazdil2d7352b2015-04-20 14:52:42 +0100999void HBasicBlock::DisconnectAndDelete() {
1000 // Dominators must be removed after all the blocks they dominate. This way
1001 // a loop header is removed last, a requirement for correct loop information
1002 // iteration.
1003 DCHECK(dominated_blocks_.IsEmpty());
David Brazdil46e2a392015-03-16 17:31:52 +00001004
David Brazdil2d7352b2015-04-20 14:52:42 +01001005 // Remove the block from all loops it is included in.
1006 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
1007 HLoopInformation* loop_info = it.Current();
1008 loop_info->Remove(this);
1009 if (loop_info->IsBackEdge(*this)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001010 // If this was the last back edge of the loop, we deliberately leave the
1011 // loop in an inconsistent state and will fail SSAChecker unless the
1012 // entire loop is removed during the pass.
David Brazdil2d7352b2015-04-20 14:52:42 +01001013 loop_info->RemoveBackEdge(this);
1014 }
1015 }
1016
1017 // Disconnect the block from its predecessors and update their control-flow
1018 // instructions.
David Brazdil46e2a392015-03-16 17:31:52 +00001019 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001020 HBasicBlock* predecessor = predecessors_.Get(i);
1021 HInstruction* last_instruction = predecessor->GetLastInstruction();
1022 predecessor->RemoveInstruction(last_instruction);
1023 predecessor->RemoveSuccessor(this);
1024 if (predecessor->GetSuccessors().Size() == 1u) {
1025 DCHECK(last_instruction->IsIf());
1026 predecessor->AddInstruction(new (graph_->GetArena()) HGoto());
1027 } else {
1028 // The predecessor has no remaining successors and therefore must be dead.
1029 // We deliberately leave it without a control-flow instruction so that the
1030 // SSAChecker fails unless it is not removed during the pass too.
1031 DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u);
1032 }
David Brazdil46e2a392015-03-16 17:31:52 +00001033 }
David Brazdil46e2a392015-03-16 17:31:52 +00001034 predecessors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001035
1036 // Disconnect the block from its successors and update their dominators
1037 // and phis.
1038 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
1039 HBasicBlock* successor = successors_.Get(i);
1040 // Delete this block from the list of predecessors.
1041 size_t this_index = successor->GetPredecessorIndexOf(this);
1042 successor->predecessors_.DeleteAt(this_index);
1043
1044 // Check that `successor` has other predecessors, otherwise `this` is the
1045 // dominator of `successor` which violates the order DCHECKed at the top.
1046 DCHECK(!successor->predecessors_.IsEmpty());
1047
1048 // Recompute the successor's dominator.
1049 HBasicBlock* old_dominator = successor->GetDominator();
1050 HBasicBlock* new_dominator = successor->predecessors_.Get(0);
1051 for (size_t j = 1, f = successor->predecessors_.Size(); j < f; ++j) {
1052 new_dominator = graph_->FindCommonDominator(
1053 new_dominator, successor->predecessors_.Get(j));
1054 }
1055 if (old_dominator != new_dominator) {
1056 successor->SetDominator(new_dominator);
1057 old_dominator->RemoveDominatedBlock(successor);
1058 new_dominator->AddDominatedBlock(successor);
1059 }
1060
1061 // Remove this block's entries in the successor's phis.
1062 if (successor->predecessors_.Size() == 1u) {
1063 // The successor has just one predecessor left. Replace phis with the only
1064 // remaining input.
1065 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1066 HPhi* phi = phi_it.Current()->AsPhi();
1067 phi->ReplaceWith(phi->InputAt(1 - this_index));
1068 successor->RemovePhi(phi);
1069 }
1070 } else {
1071 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1072 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1073 }
1074 }
1075 }
David Brazdil46e2a392015-03-16 17:31:52 +00001076 successors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001077
1078 // Disconnect from the dominator.
1079 dominator_->RemoveDominatedBlock(this);
1080 SetDominator(nullptr);
1081
1082 // Delete from the graph. The function safely deletes remaining instructions
1083 // and updates the reverse post order.
1084 graph_->DeleteDeadBlock(this);
1085 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001086}
1087
1088void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001089 DCHECK_EQ(GetGraph(), other->GetGraph());
1090 DCHECK(GetDominatedBlocks().Contains(other));
1091 DCHECK_EQ(GetSuccessors().Size(), 1u);
1092 DCHECK_EQ(GetSuccessors().Get(0), other);
1093 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1094 DCHECK_EQ(other->GetPredecessors().Get(0), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001095 DCHECK(other->GetPhis().IsEmpty());
1096
David Brazdil2d7352b2015-04-20 14:52:42 +01001097 // Move instructions from `other` to `this`.
1098 DCHECK(EndsWithControlFlowInstruction());
1099 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001100 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001101 other->instructions_.SetBlockOfInstructions(this);
1102 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001103
David Brazdil2d7352b2015-04-20 14:52:42 +01001104 // Remove `other` from the loops it is included in.
1105 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1106 HLoopInformation* loop_info = it.Current();
1107 loop_info->Remove(other);
1108 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001109 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01001110 }
1111 }
1112
1113 // Update links to the successors of `other`.
1114 successors_.Reset();
1115 while (!other->successors_.IsEmpty()) {
1116 HBasicBlock* successor = other->successors_.Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001117 successor->ReplacePredecessor(other, this);
1118 }
1119
David Brazdil2d7352b2015-04-20 14:52:42 +01001120 // Update the dominator tree.
1121 dominated_blocks_.Delete(other);
1122 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1123 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1124 dominated_blocks_.Add(dominated);
1125 dominated->SetDominator(this);
1126 }
1127 other->dominated_blocks_.Reset();
1128 other->dominator_ = nullptr;
1129
1130 // Clear the list of predecessors of `other` in preparation of deleting it.
1131 other->predecessors_.Reset();
1132
1133 // Delete `other` from the graph. The function updates reverse post order.
1134 graph_->DeleteDeadBlock(other);
1135 other->SetGraph(nullptr);
1136}
1137
1138void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1139 DCHECK_NE(GetGraph(), other->GetGraph());
1140 DCHECK(GetDominatedBlocks().IsEmpty());
1141 DCHECK(GetSuccessors().IsEmpty());
1142 DCHECK(!EndsWithControlFlowInstruction());
1143 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1144 DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock());
1145 DCHECK(other->GetPhis().IsEmpty());
1146 DCHECK(!other->IsInLoop());
1147
1148 // Move instructions from `other` to `this`.
1149 instructions_.Add(other->GetInstructions());
1150 other->instructions_.SetBlockOfInstructions(this);
1151
1152 // Update links to the successors of `other`.
1153 successors_.Reset();
1154 while (!other->successors_.IsEmpty()) {
1155 HBasicBlock* successor = other->successors_.Get(0);
1156 successor->ReplacePredecessor(other, this);
1157 }
1158
1159 // Update the dominator tree.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001160 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1161 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1162 dominated_blocks_.Add(dominated);
1163 dominated->SetDominator(this);
1164 }
1165 other->dominated_blocks_.Reset();
1166 other->dominator_ = nullptr;
1167 other->graph_ = nullptr;
1168}
1169
1170void HBasicBlock::ReplaceWith(HBasicBlock* other) {
1171 while (!GetPredecessors().IsEmpty()) {
1172 HBasicBlock* predecessor = GetPredecessors().Get(0);
1173 predecessor->ReplaceSuccessor(this, other);
1174 }
1175 while (!GetSuccessors().IsEmpty()) {
1176 HBasicBlock* successor = GetSuccessors().Get(0);
1177 successor->ReplacePredecessor(this, other);
1178 }
1179 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
1180 other->AddDominatedBlock(dominated_blocks_.Get(i));
1181 }
1182 GetDominator()->ReplaceDominatedBlock(this, other);
1183 other->SetDominator(GetDominator());
1184 dominator_ = nullptr;
1185 graph_ = nullptr;
1186}
1187
1188// Create space in `blocks` for adding `number_of_new_blocks` entries
1189// starting at location `at`. Blocks after `at` are moved accordingly.
1190static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
1191 size_t number_of_new_blocks,
1192 size_t at) {
1193 size_t old_size = blocks->Size();
1194 size_t new_size = old_size + number_of_new_blocks;
1195 blocks->SetSize(new_size);
1196 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
1197 blocks->Put(j, blocks->Get(i));
1198 }
1199}
1200
David Brazdil2d7352b2015-04-20 14:52:42 +01001201void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1202 DCHECK_EQ(block->GetGraph(), this);
1203 DCHECK(block->GetSuccessors().IsEmpty());
1204 DCHECK(block->GetPredecessors().IsEmpty());
1205 DCHECK(block->GetDominatedBlocks().IsEmpty());
1206 DCHECK(block->GetDominator() == nullptr);
1207
1208 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1209 block->RemoveInstruction(it.Current());
1210 }
1211 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1212 block->RemovePhi(it.Current()->AsPhi());
1213 }
1214
1215 reverse_post_order_.Delete(block);
1216 blocks_.Put(block->GetBlockId(), nullptr);
1217}
1218
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001219void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001220 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001221 // Simple case of an entry block, a body block, and an exit block.
1222 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001223 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001224 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
1225 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001226 DCHECK(!body->IsExitBlock());
1227 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001228
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001229 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1230 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001231
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001232 // Replace the invoke with the return value of the inlined graph.
1233 if (last->IsReturn()) {
1234 invoke->ReplaceWith(last->InputAt(0));
1235 } else {
1236 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001237 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001238
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001239 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001240 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001241 // Need to inline multiple blocks. We split `invoke`'s block
1242 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001243 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001244 // with the second half.
1245 ArenaAllocator* allocator = outer_graph->GetArena();
1246 HBasicBlock* at = invoke->GetBlock();
1247 HBasicBlock* to = at->SplitAfter(invoke);
1248
1249 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
1250 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001251 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001252 exit_block_->ReplaceWith(to);
1253
1254 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001255 // to not `HReturn` but `HGoto` instead.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001256 HInstruction* return_value = nullptr;
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001257 bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
1258 if (to->GetPredecessors().Size() == 1) {
1259 HBasicBlock* predecessor = to->GetPredecessors().Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001260 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001261 if (!returns_void) {
1262 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001263 }
1264 predecessor->AddInstruction(new (allocator) HGoto());
1265 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001266 } else {
1267 if (!returns_void) {
1268 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001269 return_value = new (allocator) HPhi(
1270 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001271 to->AddPhi(return_value->AsPhi());
1272 }
1273 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
1274 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
1275 HInstruction* last = predecessor->GetLastInstruction();
1276 if (!returns_void) {
1277 return_value->AsPhi()->AddInput(last->InputAt(0));
1278 }
1279 predecessor->AddInstruction(new (allocator) HGoto());
1280 predecessor->RemoveInstruction(last);
1281 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001282 }
1283
1284 if (return_value != nullptr) {
1285 invoke->ReplaceWith(return_value);
1286 }
1287
1288 // Update the meta information surrounding blocks:
1289 // (1) the graph they are now in,
1290 // (2) the reverse post order of that graph,
1291 // (3) the potential loop information they are now in.
1292
1293 // We don't add the entry block, the exit block, and the first block, which
1294 // has been merged with `at`.
1295 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1296
1297 // We add the `to` block.
1298 static constexpr int kNumberOfNewBlocksInCaller = 1;
1299 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
1300 + kNumberOfNewBlocksInCaller;
1301
1302 // Find the location of `at` in the outer graph's reverse post order. The new
1303 // blocks will be added after it.
1304 size_t index_of_at = 0;
1305 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
1306 index_of_at++;
1307 }
1308 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1309
1310 // Do a reverse post order of the blocks in the callee and do (1), (2),
1311 // and (3) to the blocks that apply.
1312 HLoopInformation* info = at->GetLoopInformation();
1313 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1314 HBasicBlock* current = it.Current();
1315 if (current != exit_block_ && current != entry_block_ && current != first) {
1316 DCHECK(!current->IsInLoop());
1317 DCHECK(current->GetGraph() == this);
1318 current->SetGraph(outer_graph);
1319 outer_graph->AddBlock(current);
1320 outer_graph->reverse_post_order_.Put(++index_of_at, current);
1321 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001322 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001323 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1324 loop_it.Current()->Add(current);
1325 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001326 }
1327 }
1328 }
1329
1330 // Do (1), (2), and (3) to `to`.
1331 to->SetGraph(outer_graph);
1332 outer_graph->AddBlock(to);
1333 outer_graph->reverse_post_order_.Put(++index_of_at, to);
1334 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001335 to->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001336 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1337 loop_it.Current()->Add(to);
1338 }
David Brazdil46e2a392015-03-16 17:31:52 +00001339 if (info->IsBackEdge(*at)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001340 // Only `to` can become a back edge, as the inlined blocks
1341 // are predecessors of `to`.
1342 info->ReplaceBackEdge(at, to);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001343 }
1344 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001345 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001346
David Brazdil05144f42015-04-16 15:18:00 +01001347 // Update the next instruction id of the outer graph, so that instructions
1348 // added later get bigger ids than those in the inner graph.
1349 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1350
1351 // Walk over the entry block and:
1352 // - Move constants from the entry block to the outer_graph's entry block,
1353 // - Replace HParameterValue instructions with their real value.
1354 // - Remove suspend checks, that hold an environment.
1355 // We must do this after the other blocks have been inlined, otherwise ids of
1356 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001357 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001358 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1359 HInstruction* current = it.Current();
1360 if (current->IsNullConstant()) {
1361 current->ReplaceWith(outer_graph->GetNullConstant());
1362 } else if (current->IsIntConstant()) {
1363 current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue()));
1364 } else if (current->IsLongConstant()) {
1365 current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue()));
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001366 } else if (current->IsFloatConstant()) {
1367 current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue()));
1368 } else if (current->IsDoubleConstant()) {
1369 current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue()));
David Brazdil05144f42015-04-16 15:18:00 +01001370 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001371 if (kIsDebugBuild
1372 && invoke->IsInvokeStaticOrDirect()
1373 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1374 // Ensure we do not use the last input of `invoke`, as it
1375 // contains a clinit check which is not an actual argument.
1376 size_t last_input_index = invoke->InputCount() - 1;
1377 DCHECK(parameter_index != last_input_index);
1378 }
David Brazdil05144f42015-04-16 15:18:00 +01001379 current->ReplaceWith(invoke->InputAt(parameter_index++));
1380 } else {
1381 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1382 entry_block_->RemoveInstruction(current);
1383 }
1384 }
1385
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001386 // Finally remove the invoke from the caller.
1387 invoke->GetBlock()->RemoveInstruction(invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001388}
1389
Calin Juravleacf735c2015-02-12 15:25:22 +00001390std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1391 ScopedObjectAccess soa(Thread::Current());
1392 os << "["
1393 << " is_top=" << rhs.IsTop()
1394 << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
1395 << " is_exact=" << rhs.IsExact()
1396 << " ]";
1397 return os;
1398}
1399
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001400} // namespace art