blob: 91daeb7a4cc8b8ca4cf2b00ed745baa659300b0d [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
Mark Mendelle82549b2015-05-06 10:55:34 -040019#include "code_generator.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010020#include "ssa_builder.h"
David Brazdila4b8c212015-05-07 09:59:30 +010021#include "base/bit_vector-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010022#include "base/bit_utils.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "utils/growable_array.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "scoped_thread_state_change.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000025
26namespace art {
27
28void HGraph::AddBlock(HBasicBlock* block) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000029 block->SetBlockId(blocks_.Size());
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030 blocks_.Add(block);
31}
32
Nicolas Geoffray804d0932014-05-02 08:46:00 +010033void HGraph::FindBackEdges(ArenaBitVector* visited) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000034 ArenaBitVector visiting(arena_, blocks_.Size(), false);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035 VisitBlockForBackEdges(entry_block_, visited, &visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000036}
37
Roland Levillainfc600dc2014-12-02 17:16:31 +000038static void RemoveAsUser(HInstruction* instruction) {
39 for (size_t i = 0; i < instruction->InputCount(); i++) {
David Brazdil1abb4192015-02-17 18:33:36 +000040 instruction->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000041 }
42
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010043 for (HEnvironment* environment = instruction->GetEnvironment();
44 environment != nullptr;
45 environment = environment->GetParent()) {
Roland Levillainfc600dc2014-12-02 17:16:31 +000046 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
David Brazdil1abb4192015-02-17 18:33:36 +000047 if (environment->GetInstructionAt(i) != nullptr) {
48 environment->RemoveAsUserOfInput(i);
Roland Levillainfc600dc2014-12-02 17:16:31 +000049 }
50 }
51 }
52}
53
54void HGraph::RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const {
55 for (size_t i = 0; i < blocks_.Size(); ++i) {
56 if (!visited.IsBitSet(i)) {
57 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010058 DCHECK(block->GetPhis().IsEmpty()) << "Phis are not inserted at this stage";
Roland Levillainfc600dc2014-12-02 17:16:31 +000059 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
60 RemoveAsUser(it.Current());
61 }
62 }
63 }
64}
65
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010066void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010067 for (size_t i = 0; i < blocks_.Size(); ++i) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000068 if (!visited.IsBitSet(i)) {
David Brazdil1abb4192015-02-17 18:33:36 +000069 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010070 // We only need to update the successor, which might be live.
David Brazdil1abb4192015-02-17 18:33:36 +000071 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
72 block->GetSuccessors().Get(j)->RemovePredecessor(block);
73 }
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010074 // Remove the block from the list of blocks, so that further analyses
75 // never see it.
76 blocks_.Put(i, nullptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000077 }
78 }
79}
80
81void HGraph::VisitBlockForBackEdges(HBasicBlock* block,
82 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +010083 ArenaBitVector* visiting) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000084 int id = block->GetBlockId();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000085 if (visited->IsBitSet(id)) return;
86
87 visited->SetBit(id);
88 visiting->SetBit(id);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010089 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
90 HBasicBlock* successor = block->GetSuccessors().Get(i);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000091 if (visiting->IsBitSet(successor->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000092 successor->AddBackEdge(block);
93 } else {
94 VisitBlockForBackEdges(successor, visited, visiting);
95 }
96 }
97 visiting->ClearBit(id);
98}
99
100void HGraph::BuildDominatorTree() {
101 ArenaBitVector visited(arena_, blocks_.Size(), false);
102
103 // (1) Find the back edges in the graph doing a DFS traversal.
104 FindBackEdges(&visited);
105
Roland Levillainfc600dc2014-12-02 17:16:31 +0000106 // (2) Remove instructions and phis from blocks not visited during
107 // the initial DFS as users from other instructions, so that
108 // users can be safely removed before uses later.
109 RemoveInstructionsAsUsersFromDeadBlocks(visited);
110
111 // (3) Remove blocks not visited during the initial DFS.
112 // Step (4) requires dead blocks to be removed from the
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000113 // predecessors list of live blocks.
114 RemoveDeadBlocks(visited);
115
Roland Levillainfc600dc2014-12-02 17:16:31 +0000116 // (4) Simplify the CFG now, so that we don't need to recompute
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100117 // dominators and the reverse post order.
118 SimplifyCFG();
119
Roland Levillainfc600dc2014-12-02 17:16:31 +0000120 // (5) Compute the immediate dominator of each block. We visit
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000121 // the successors of a block only when all its forward branches
122 // have been processed.
123 GrowableArray<size_t> visits(arena_, blocks_.Size());
124 visits.SetSize(blocks_.Size());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100125 reverse_post_order_.Add(entry_block_);
126 for (size_t i = 0; i < entry_block_->GetSuccessors().Size(); i++) {
127 VisitBlockForDominatorTree(entry_block_->GetSuccessors().Get(i), entry_block_, &visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000128 }
129}
130
131HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
132 ArenaBitVector visited(arena_, blocks_.Size(), false);
133 // Walk the dominator tree of the first block and mark the visited blocks.
134 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000135 visited.SetBit(first->GetBlockId());
136 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000137 }
138 // Walk the dominator tree of the second block until a marked block is found.
139 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000140 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000141 return second;
142 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000143 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000144 }
145 LOG(ERROR) << "Could not find common dominator";
146 return nullptr;
147}
148
149void HGraph::VisitBlockForDominatorTree(HBasicBlock* block,
150 HBasicBlock* predecessor,
151 GrowableArray<size_t>* visits) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000152 if (block->GetDominator() == nullptr) {
153 block->SetDominator(predecessor);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000154 } else {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000155 block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000156 }
157
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000158 visits->Increment(block->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000159 // Once all the forward edges have been visited, we know the immediate
160 // dominator of the block. We can then start visiting its successors.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000161 if (visits->Get(block->GetBlockId()) ==
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100162 block->GetPredecessors().Size() - block->NumberOfBackEdges()) {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100163 block->GetDominator()->AddDominatedBlock(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100164 reverse_post_order_.Add(block);
165 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
166 VisitBlockForDominatorTree(block->GetSuccessors().Get(i), block, visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000167 }
168 }
169}
170
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171void HGraph::TransformToSsa() {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100172 DCHECK(!reverse_post_order_.IsEmpty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100173 SsaBuilder ssa_builder(this);
174 ssa_builder.BuildSsa();
175}
176
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100177void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) {
178 // Insert a new node between `block` and `successor` to split the
179 // critical edge.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100180 HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100181 AddBlock(new_block);
182 new_block->AddInstruction(new (arena_) HGoto());
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100183 block->ReplaceSuccessor(successor, new_block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100184 new_block->AddSuccessor(successor);
185 if (successor->IsLoopHeader()) {
186 // If we split at a back edge boundary, make the new block the back edge.
187 HLoopInformation* info = successor->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000188 if (info->IsBackEdge(*block)) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100189 info->RemoveBackEdge(block);
190 info->AddBackEdge(new_block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100191 }
192 }
193}
194
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100195void HGraph::SimplifyLoop(HBasicBlock* header) {
196 HLoopInformation* info = header->GetLoopInformation();
197
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100198 // Make sure the loop has only one pre header. This simplifies SSA building by having
199 // to just look at the pre header to know which locals are initialized at entry of the
200 // loop.
201 size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges();
202 if (number_of_incomings != 1) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100203 HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100204 AddBlock(pre_header);
205 pre_header->AddInstruction(new (arena_) HGoto());
206
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100207 for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) {
208 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100209 if (!info->IsBackEdge(*predecessor)) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100210 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100211 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100212 }
213 }
214 pre_header->AddSuccessor(header);
215 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100216
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100217 // Make sure the first predecessor of a loop header is the incoming block.
218 if (info->IsBackEdge(*header->GetPredecessors().Get(0))) {
219 HBasicBlock* to_swap = header->GetPredecessors().Get(0);
220 for (size_t pred = 1, e = header->GetPredecessors().Size(); pred < e; ++pred) {
221 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
222 if (!info->IsBackEdge(*predecessor)) {
223 header->predecessors_.Put(pred, to_swap);
224 header->predecessors_.Put(0, predecessor);
225 break;
226 }
227 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100228 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100229
230 // Place the suspend check at the beginning of the header, so that live registers
231 // will be known when allocating registers. Note that code generation can still
232 // generate the suspend check at the back edge, but needs to be careful with
233 // loop phi spill slots (which are not written to at back edge).
234 HInstruction* first_instruction = header->GetFirstInstruction();
235 if (!first_instruction->IsSuspendCheck()) {
236 HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc());
237 header->InsertInstructionBefore(check, first_instruction);
238 first_instruction = check;
239 }
240 info->SetSuspendCheck(first_instruction->AsSuspendCheck());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100241}
242
243void HGraph::SimplifyCFG() {
244 // Simplify the CFG for future analysis, and code generation:
245 // (1): Split critical edges.
246 // (2): Simplify loops by having only one back edge, and one preheader.
247 for (size_t i = 0; i < blocks_.Size(); ++i) {
248 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100249 if (block == nullptr) continue;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100250 if (block->GetSuccessors().Size() > 1) {
251 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
252 HBasicBlock* successor = block->GetSuccessors().Get(j);
253 if (successor->GetPredecessors().Size() > 1) {
254 SplitCriticalEdge(block, successor);
255 --j;
256 }
257 }
258 }
259 if (block->IsLoopHeader()) {
260 SimplifyLoop(block);
261 }
262 }
263}
264
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000265bool HGraph::AnalyzeNaturalLoops() const {
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100266 // Order does not matter.
267 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
268 HBasicBlock* block = it.Current();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100269 if (block->IsLoopHeader()) {
270 HLoopInformation* info = block->GetLoopInformation();
271 if (!info->Populate()) {
272 // Abort if the loop is non natural. We currently bailout in such cases.
273 return false;
274 }
275 }
276 }
277 return true;
278}
279
David Brazdil8d5b8b22015-03-24 10:51:52 +0000280void HGraph::InsertConstant(HConstant* constant) {
281 // New constants are inserted before the final control-flow instruction
282 // of the graph, or at its end if called from the graph builder.
283 if (entry_block_->EndsWithControlFlowInstruction()) {
284 entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction());
David Brazdil46e2a392015-03-16 17:31:52 +0000285 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000286 entry_block_->AddInstruction(constant);
David Brazdil46e2a392015-03-16 17:31:52 +0000287 }
288}
289
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000290HNullConstant* HGraph::GetNullConstant() {
291 if (cached_null_constant_ == nullptr) {
292 cached_null_constant_ = new (arena_) HNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000293 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000294 }
295 return cached_null_constant_;
296}
297
David Brazdil8d5b8b22015-03-24 10:51:52 +0000298HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value) {
299 switch (type) {
300 case Primitive::Type::kPrimBoolean:
301 DCHECK(IsUint<1>(value));
302 FALLTHROUGH_INTENDED;
303 case Primitive::Type::kPrimByte:
304 case Primitive::Type::kPrimChar:
305 case Primitive::Type::kPrimShort:
306 case Primitive::Type::kPrimInt:
307 DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value));
308 return GetIntConstant(static_cast<int32_t>(value));
309
310 case Primitive::Type::kPrimLong:
311 return GetLongConstant(value);
312
313 default:
314 LOG(FATAL) << "Unsupported constant type";
315 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000316 }
David Brazdil46e2a392015-03-16 17:31:52 +0000317}
318
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000319void HGraph::CacheFloatConstant(HFloatConstant* constant) {
320 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
321 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
322 cached_float_constants_.Overwrite(value, constant);
323}
324
325void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
326 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
327 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
328 cached_double_constants_.Overwrite(value, constant);
329}
330
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000331void HLoopInformation::Add(HBasicBlock* block) {
332 blocks_.SetBit(block->GetBlockId());
333}
334
David Brazdil46e2a392015-03-16 17:31:52 +0000335void HLoopInformation::Remove(HBasicBlock* block) {
336 blocks_.ClearBit(block->GetBlockId());
337}
338
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100339void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
340 if (blocks_.IsBitSet(block->GetBlockId())) {
341 return;
342 }
343
344 blocks_.SetBit(block->GetBlockId());
345 block->SetInLoop(this);
346 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
347 PopulateRecursive(block->GetPredecessors().Get(i));
348 }
349}
350
351bool HLoopInformation::Populate() {
David Brazdila4b8c212015-05-07 09:59:30 +0100352 DCHECK_EQ(blocks_.NumSetBits(), 0u) << "Loop information has already been populated";
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100353 for (size_t i = 0, e = GetBackEdges().Size(); i < e; ++i) {
354 HBasicBlock* back_edge = GetBackEdges().Get(i);
355 DCHECK(back_edge->GetDominator() != nullptr);
356 if (!header_->Dominates(back_edge)) {
357 // This loop is not natural. Do not bother going further.
358 return false;
359 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100360
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100361 // Populate this loop: starting with the back edge, recursively add predecessors
362 // that are not already part of that loop. Set the header as part of the loop
363 // to end the recursion.
364 // This is a recursive implementation of the algorithm described in
365 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
366 blocks_.SetBit(header_->GetBlockId());
367 PopulateRecursive(back_edge);
368 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100369 return true;
370}
371
David Brazdila4b8c212015-05-07 09:59:30 +0100372void HLoopInformation::Update() {
373 HGraph* graph = header_->GetGraph();
374 for (uint32_t id : blocks_.Indexes()) {
375 HBasicBlock* block = graph->GetBlocks().Get(id);
376 // Reset loop information of non-header blocks inside the loop, except
377 // members of inner nested loops because those should already have been
378 // updated by their own LoopInformation.
379 if (block->GetLoopInformation() == this && block != header_) {
380 block->SetLoopInformation(nullptr);
381 }
382 }
383 blocks_.ClearAllBits();
384
385 if (back_edges_.IsEmpty()) {
386 // The loop has been dismantled, delete its suspend check and remove info
387 // from the header.
388 DCHECK(HasSuspendCheck());
389 header_->RemoveInstruction(suspend_check_);
390 header_->SetLoopInformation(nullptr);
391 header_ = nullptr;
392 suspend_check_ = nullptr;
393 } else {
394 if (kIsDebugBuild) {
395 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
396 DCHECK(header_->Dominates(back_edges_.Get(i)));
397 }
398 }
399 // This loop still has reachable back edges. Repopulate the list of blocks.
400 bool populate_successful = Populate();
401 DCHECK(populate_successful);
402 }
403}
404
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100405HBasicBlock* HLoopInformation::GetPreHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100406 return header_->GetDominator();
407}
408
409bool HLoopInformation::Contains(const HBasicBlock& block) const {
410 return blocks_.IsBitSet(block.GetBlockId());
411}
412
413bool HLoopInformation::IsIn(const HLoopInformation& other) const {
414 return other.blocks_.IsBitSet(header_->GetBlockId());
415}
416
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100417size_t HLoopInformation::GetLifetimeEnd() const {
418 size_t last_position = 0;
419 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
420 last_position = std::max(back_edges_.Get(i)->GetLifetimeEnd(), last_position);
421 }
422 return last_position;
423}
424
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100425bool HBasicBlock::Dominates(HBasicBlock* other) const {
426 // Walk up the dominator tree from `other`, to find out if `this`
427 // is an ancestor.
428 HBasicBlock* current = other;
429 while (current != nullptr) {
430 if (current == this) {
431 return true;
432 }
433 current = current->GetDominator();
434 }
435 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100436}
437
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100438static void UpdateInputsUsers(HInstruction* instruction) {
439 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
440 instruction->InputAt(i)->AddUseAt(instruction, i);
441 }
442 // Environment should be created later.
443 DCHECK(!instruction->HasEnvironment());
444}
445
Roland Levillainccc07a92014-09-16 14:48:16 +0100446void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
447 HInstruction* replacement) {
448 DCHECK(initial->GetBlock() == this);
449 InsertInstructionBefore(replacement, initial);
450 initial->ReplaceWith(replacement);
451 RemoveInstruction(initial);
452}
453
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100454static void Add(HInstructionList* instruction_list,
455 HBasicBlock* block,
456 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000457 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000458 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100459 instruction->SetBlock(block);
460 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100461 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100462 instruction_list->AddInstruction(instruction);
463}
464
465void HBasicBlock::AddInstruction(HInstruction* instruction) {
466 Add(&instructions_, this, instruction);
467}
468
469void HBasicBlock::AddPhi(HPhi* phi) {
470 Add(&phis_, this, phi);
471}
472
David Brazdilc3d743f2015-04-22 13:40:50 +0100473void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
474 DCHECK(!cursor->IsPhi());
475 DCHECK(!instruction->IsPhi());
476 DCHECK_EQ(instruction->GetId(), -1);
477 DCHECK_NE(cursor->GetId(), -1);
478 DCHECK_EQ(cursor->GetBlock(), this);
479 DCHECK(!instruction->IsControlFlow());
480 instruction->SetBlock(this);
481 instruction->SetId(GetGraph()->GetNextInstructionId());
482 UpdateInputsUsers(instruction);
483 instructions_.InsertInstructionBefore(instruction, cursor);
484}
485
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100486void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
487 DCHECK(!cursor->IsPhi());
488 DCHECK(!instruction->IsPhi());
489 DCHECK_EQ(instruction->GetId(), -1);
490 DCHECK_NE(cursor->GetId(), -1);
491 DCHECK_EQ(cursor->GetBlock(), this);
492 DCHECK(!instruction->IsControlFlow());
493 DCHECK(!cursor->IsControlFlow());
494 instruction->SetBlock(this);
495 instruction->SetId(GetGraph()->GetNextInstructionId());
496 UpdateInputsUsers(instruction);
497 instructions_.InsertInstructionAfter(instruction, cursor);
498}
499
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100500void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
501 DCHECK_EQ(phi->GetId(), -1);
502 DCHECK_NE(cursor->GetId(), -1);
503 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100504 phi->SetBlock(this);
505 phi->SetId(GetGraph()->GetNextInstructionId());
506 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100507 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100508}
509
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100510static void Remove(HInstructionList* instruction_list,
511 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000512 HInstruction* instruction,
513 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100514 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100515 instruction->SetBlock(nullptr);
516 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000517 if (ensure_safety) {
518 DCHECK(instruction->GetUses().IsEmpty());
519 DCHECK(instruction->GetEnvUses().IsEmpty());
520 RemoveAsUser(instruction);
521 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100522}
523
David Brazdil1abb4192015-02-17 18:33:36 +0000524void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100525 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000526 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100527}
528
David Brazdil1abb4192015-02-17 18:33:36 +0000529void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
530 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100531}
532
David Brazdilc7508e92015-04-27 13:28:57 +0100533void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
534 if (instruction->IsPhi()) {
535 RemovePhi(instruction->AsPhi(), ensure_safety);
536 } else {
537 RemoveInstruction(instruction, ensure_safety);
538 }
539}
540
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +0100541void HEnvironment::CopyFrom(const GrowableArray<HInstruction*>& locals) {
542 for (size_t i = 0; i < locals.Size(); i++) {
543 HInstruction* instruction = locals.Get(i);
544 SetRawEnvAt(i, instruction);
545 if (instruction != nullptr) {
546 instruction->AddEnvUseAt(this, i);
547 }
548 }
549}
550
David Brazdiled596192015-01-23 10:39:45 +0000551void HEnvironment::CopyFrom(HEnvironment* env) {
552 for (size_t i = 0; i < env->Size(); i++) {
553 HInstruction* instruction = env->GetInstructionAt(i);
554 SetRawEnvAt(i, instruction);
555 if (instruction != nullptr) {
556 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100557 }
David Brazdiled596192015-01-23 10:39:45 +0000558 }
559}
560
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700561void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
562 HBasicBlock* loop_header) {
563 DCHECK(loop_header->IsLoopHeader());
564 for (size_t i = 0; i < env->Size(); i++) {
565 HInstruction* instruction = env->GetInstructionAt(i);
566 SetRawEnvAt(i, instruction);
567 if (instruction == nullptr) {
568 continue;
569 }
570 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
571 // At the end of the loop pre-header, the corresponding value for instruction
572 // is the first input of the phi.
573 HInstruction* initial = instruction->AsPhi()->InputAt(0);
574 DCHECK(initial->GetBlock()->Dominates(loop_header));
575 SetRawEnvAt(i, initial);
576 initial->AddEnvUseAt(this, i);
577 } else {
578 instruction->AddEnvUseAt(this, i);
579 }
580 }
581}
582
David Brazdil1abb4192015-02-17 18:33:36 +0000583void HEnvironment::RemoveAsUserOfInput(size_t index) const {
584 const HUserRecord<HEnvironment*> user_record = vregs_.Get(index);
585 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100586}
587
Calin Juravle77520bc2015-01-12 18:45:46 +0000588HInstruction* HInstruction::GetNextDisregardingMoves() const {
589 HInstruction* next = GetNext();
590 while (next != nullptr && next->IsParallelMove()) {
591 next = next->GetNext();
592 }
593 return next;
594}
595
596HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
597 HInstruction* previous = GetPrevious();
598 while (previous != nullptr && previous->IsParallelMove()) {
599 previous = previous->GetPrevious();
600 }
601 return previous;
602}
603
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100604void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000605 if (first_instruction_ == nullptr) {
606 DCHECK(last_instruction_ == nullptr);
607 first_instruction_ = last_instruction_ = instruction;
608 } else {
609 last_instruction_->next_ = instruction;
610 instruction->previous_ = last_instruction_;
611 last_instruction_ = instruction;
612 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000613}
614
David Brazdilc3d743f2015-04-22 13:40:50 +0100615void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
616 DCHECK(Contains(cursor));
617 if (cursor == first_instruction_) {
618 cursor->previous_ = instruction;
619 instruction->next_ = cursor;
620 first_instruction_ = instruction;
621 } else {
622 instruction->previous_ = cursor->previous_;
623 instruction->next_ = cursor;
624 cursor->previous_ = instruction;
625 instruction->previous_->next_ = instruction;
626 }
627}
628
629void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
630 DCHECK(Contains(cursor));
631 if (cursor == last_instruction_) {
632 cursor->next_ = instruction;
633 instruction->previous_ = cursor;
634 last_instruction_ = instruction;
635 } else {
636 instruction->next_ = cursor->next_;
637 instruction->previous_ = cursor;
638 cursor->next_ = instruction;
639 instruction->next_->previous_ = instruction;
640 }
641}
642
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100643void HInstructionList::RemoveInstruction(HInstruction* instruction) {
644 if (instruction->previous_ != nullptr) {
645 instruction->previous_->next_ = instruction->next_;
646 }
647 if (instruction->next_ != nullptr) {
648 instruction->next_->previous_ = instruction->previous_;
649 }
650 if (instruction == first_instruction_) {
651 first_instruction_ = instruction->next_;
652 }
653 if (instruction == last_instruction_) {
654 last_instruction_ = instruction->previous_;
655 }
656}
657
Roland Levillain6b469232014-09-25 10:10:38 +0100658bool HInstructionList::Contains(HInstruction* instruction) const {
659 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
660 if (it.Current() == instruction) {
661 return true;
662 }
663 }
664 return false;
665}
666
Roland Levillainccc07a92014-09-16 14:48:16 +0100667bool HInstructionList::FoundBefore(const HInstruction* instruction1,
668 const HInstruction* instruction2) const {
669 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
670 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
671 if (it.Current() == instruction1) {
672 return true;
673 }
674 if (it.Current() == instruction2) {
675 return false;
676 }
677 }
678 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
679 return true;
680}
681
Roland Levillain6c82d402014-10-13 16:10:27 +0100682bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
683 if (other_instruction == this) {
684 // An instruction does not strictly dominate itself.
685 return false;
686 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100687 HBasicBlock* block = GetBlock();
688 HBasicBlock* other_block = other_instruction->GetBlock();
689 if (block != other_block) {
690 return GetBlock()->Dominates(other_instruction->GetBlock());
691 } else {
692 // If both instructions are in the same block, ensure this
693 // instruction comes before `other_instruction`.
694 if (IsPhi()) {
695 if (!other_instruction->IsPhi()) {
696 // Phis appear before non phi-instructions so this instruction
697 // dominates `other_instruction`.
698 return true;
699 } else {
700 // There is no order among phis.
701 LOG(FATAL) << "There is no dominance between phis of a same block.";
702 return false;
703 }
704 } else {
705 // `this` is not a phi.
706 if (other_instruction->IsPhi()) {
707 // Phis appear before non phi-instructions so this instruction
708 // does not dominate `other_instruction`.
709 return false;
710 } else {
711 // Check whether this instruction comes before
712 // `other_instruction` in the instruction list.
713 return block->GetInstructions().FoundBefore(this, other_instruction);
714 }
715 }
716 }
717}
718
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100719void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100720 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000721 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
722 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100723 HInstruction* user = current->GetUser();
724 size_t input_index = current->GetIndex();
725 user->SetRawInputAt(input_index, other);
726 other->AddUseAt(user, input_index);
727 }
728
David Brazdiled596192015-01-23 10:39:45 +0000729 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
730 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100731 HEnvironment* user = current->GetUser();
732 size_t input_index = current->GetIndex();
733 user->SetRawEnvAt(input_index, other);
734 other->AddEnvUseAt(user, input_index);
735 }
736
David Brazdiled596192015-01-23 10:39:45 +0000737 uses_.Clear();
738 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100739}
740
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100741void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000742 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100743 SetRawInputAt(index, replacement);
744 replacement->AddUseAt(this, index);
745}
746
Nicolas Geoffray39468442014-09-02 15:17:15 +0100747size_t HInstruction::EnvironmentSize() const {
748 return HasEnvironment() ? environment_->Size() : 0;
749}
750
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100751void HPhi::AddInput(HInstruction* input) {
752 DCHECK(input->GetBlock() != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +0000753 inputs_.Add(HUserRecord<HInstruction*>(input));
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100754 input->AddUseAt(this, inputs_.Size() - 1);
755}
756
David Brazdil2d7352b2015-04-20 14:52:42 +0100757void HPhi::RemoveInputAt(size_t index) {
758 RemoveAsUserOfInput(index);
759 inputs_.DeleteAt(index);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100760 for (size_t i = index, e = InputCount(); i < e; ++i) {
761 InputRecordAt(i).GetUseNode()->SetIndex(i);
762 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100763}
764
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100765#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000766void H##name::Accept(HGraphVisitor* visitor) { \
767 visitor->Visit##name(this); \
768}
769
770FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
771
772#undef DEFINE_ACCEPT
773
774void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100775 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
776 for (size_t i = 0 ; i < blocks.Size(); i++) {
David Brazdil46e2a392015-03-16 17:31:52 +0000777 HBasicBlock* block = blocks.Get(i);
778 if (block != nullptr) {
779 VisitBasicBlock(block);
780 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000781 }
782}
783
Roland Levillain633021e2014-10-01 14:12:25 +0100784void HGraphVisitor::VisitReversePostOrder() {
785 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
786 VisitBasicBlock(it.Current());
787 }
788}
789
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000790void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100791 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100792 it.Current()->Accept(this);
793 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100794 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000795 it.Current()->Accept(this);
796 }
797}
798
Mark Mendelle82549b2015-05-06 10:55:34 -0400799HConstant* HTypeConversion::TryStaticEvaluation() const {
800 HGraph* graph = GetBlock()->GetGraph();
801 if (GetInput()->IsIntConstant()) {
802 int32_t value = GetInput()->AsIntConstant()->GetValue();
803 switch (GetResultType()) {
804 case Primitive::kPrimLong:
805 return graph->GetLongConstant(static_cast<int64_t>(value));
806 case Primitive::kPrimFloat:
807 return graph->GetFloatConstant(static_cast<float>(value));
808 case Primitive::kPrimDouble:
809 return graph->GetDoubleConstant(static_cast<double>(value));
810 default:
811 return nullptr;
812 }
813 } else if (GetInput()->IsLongConstant()) {
814 int64_t value = GetInput()->AsLongConstant()->GetValue();
815 switch (GetResultType()) {
816 case Primitive::kPrimInt:
817 return graph->GetIntConstant(static_cast<int32_t>(value));
818 case Primitive::kPrimFloat:
819 return graph->GetFloatConstant(static_cast<float>(value));
820 case Primitive::kPrimDouble:
821 return graph->GetDoubleConstant(static_cast<double>(value));
822 default:
823 return nullptr;
824 }
825 } else if (GetInput()->IsFloatConstant()) {
826 float value = GetInput()->AsFloatConstant()->GetValue();
827 switch (GetResultType()) {
828 case Primitive::kPrimInt:
829 if (std::isnan(value))
830 return graph->GetIntConstant(0);
831 if (value >= kPrimIntMax)
832 return graph->GetIntConstant(kPrimIntMax);
833 if (value <= kPrimIntMin)
834 return graph->GetIntConstant(kPrimIntMin);
835 return graph->GetIntConstant(static_cast<int32_t>(value));
836 case Primitive::kPrimLong:
837 if (std::isnan(value))
838 return graph->GetLongConstant(0);
839 if (value >= kPrimLongMax)
840 return graph->GetLongConstant(kPrimLongMax);
841 if (value <= kPrimLongMin)
842 return graph->GetLongConstant(kPrimLongMin);
843 return graph->GetLongConstant(static_cast<int64_t>(value));
844 case Primitive::kPrimDouble:
845 return graph->GetDoubleConstant(static_cast<double>(value));
846 default:
847 return nullptr;
848 }
849 } else if (GetInput()->IsDoubleConstant()) {
850 double value = GetInput()->AsDoubleConstant()->GetValue();
851 switch (GetResultType()) {
852 case Primitive::kPrimInt:
853 if (std::isnan(value))
854 return graph->GetIntConstant(0);
855 if (value >= kPrimIntMax)
856 return graph->GetIntConstant(kPrimIntMax);
857 if (value <= kPrimLongMin)
858 return graph->GetIntConstant(kPrimIntMin);
859 return graph->GetIntConstant(static_cast<int32_t>(value));
860 case Primitive::kPrimLong:
861 if (std::isnan(value))
862 return graph->GetLongConstant(0);
863 if (value >= kPrimLongMax)
864 return graph->GetLongConstant(kPrimLongMax);
865 if (value <= kPrimLongMin)
866 return graph->GetLongConstant(kPrimLongMin);
867 return graph->GetLongConstant(static_cast<int64_t>(value));
868 case Primitive::kPrimFloat:
869 return graph->GetFloatConstant(static_cast<float>(value));
870 default:
871 return nullptr;
872 }
873 }
874 return nullptr;
875}
876
Roland Levillain9240d6a2014-10-20 16:47:04 +0100877HConstant* HUnaryOperation::TryStaticEvaluation() const {
878 if (GetInput()->IsIntConstant()) {
879 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000880 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain9240d6a2014-10-20 16:47:04 +0100881 } else if (GetInput()->IsLongConstant()) {
Roland Levillainb762d2e2014-10-22 10:11:06 +0100882 // TODO: Implement static evaluation of long unary operations.
883 //
884 // Do not exit with a fatal condition here. Instead, simply
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700885 // return `null' to notify the caller that this instruction
Roland Levillainb762d2e2014-10-22 10:11:06 +0100886 // cannot (yet) be statically evaluated.
Roland Levillain9240d6a2014-10-20 16:47:04 +0100887 return nullptr;
888 }
889 return nullptr;
890}
891
892HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +0100893 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
894 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
895 GetRight()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000896 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100897 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
898 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
899 GetRight()->AsLongConstant()->GetValue());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000900 if (GetResultType() == Primitive::kPrimLong) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000901 return GetBlock()->GetGraph()->GetLongConstant(value);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000902 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000903 DCHECK_EQ(GetResultType(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000904 return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000905 }
Roland Levillain556c3d12014-09-18 15:25:07 +0100906 }
907 return nullptr;
908}
Dave Allison20dfc792014-06-16 20:44:29 -0700909
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000910HConstant* HBinaryOperation::GetConstantRight() const {
911 if (GetRight()->IsConstant()) {
912 return GetRight()->AsConstant();
913 } else if (IsCommutative() && GetLeft()->IsConstant()) {
914 return GetLeft()->AsConstant();
915 } else {
916 return nullptr;
917 }
918}
919
920// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700921// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000922HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
923 HInstruction* most_constant_right = GetConstantRight();
924 if (most_constant_right == nullptr) {
925 return nullptr;
926 } else if (most_constant_right == GetLeft()) {
927 return GetRight();
928 } else {
929 return GetLeft();
930 }
931}
932
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700933bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
934 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100935}
936
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100937bool HInstruction::Equals(HInstruction* other) const {
938 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100939 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100940 if (!InstructionDataEquals(other)) return false;
941 if (GetType() != other->GetType()) return false;
942 if (InputCount() != other->InputCount()) return false;
943
944 for (size_t i = 0, e = InputCount(); i < e; ++i) {
945 if (InputAt(i) != other->InputAt(i)) return false;
946 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100947 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100948 return true;
949}
950
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700951std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
952#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
953 switch (rhs) {
954 FOR_EACH_INSTRUCTION(DECLARE_CASE)
955 default:
956 os << "Unknown instruction kind " << static_cast<int>(rhs);
957 break;
958 }
959#undef DECLARE_CASE
960 return os;
961}
962
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000963void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000964 next_->previous_ = previous_;
965 if (previous_ != nullptr) {
966 previous_->next_ = next_;
967 }
968 if (block_->instructions_.first_instruction_ == this) {
969 block_->instructions_.first_instruction_ = next_;
970 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000971 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000972
973 previous_ = cursor->previous_;
974 if (previous_ != nullptr) {
975 previous_->next_ = this;
976 }
977 next_ = cursor;
978 cursor->previous_ = this;
979 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000980
981 if (block_->instructions_.first_instruction_ == cursor) {
982 block_->instructions_.first_instruction_ = this;
983 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000984}
985
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000986HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
987 DCHECK(!cursor->IsControlFlow());
988 DCHECK_NE(instructions_.last_instruction_, cursor);
989 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000990
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000991 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
992 new_block->instructions_.first_instruction_ = cursor->GetNext();
993 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
994 cursor->next_->previous_ = nullptr;
995 cursor->next_ = nullptr;
996 instructions_.last_instruction_ = cursor;
997
998 new_block->instructions_.SetBlockOfInstructions(new_block);
999 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
1000 HBasicBlock* successor = GetSuccessors().Get(i);
1001 new_block->successors_.Add(successor);
1002 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
1003 }
1004 successors_.Reset();
1005
1006 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
1007 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
1008 dominated->dominator_ = new_block;
1009 new_block->dominated_blocks_.Add(dominated);
1010 }
1011 dominated_blocks_.Reset();
1012 return new_block;
1013}
1014
David Brazdil46e2a392015-03-16 17:31:52 +00001015bool HBasicBlock::IsSingleGoto() const {
1016 HLoopInformation* loop_info = GetLoopInformation();
1017 // TODO: Remove the null check b/19084197.
1018 return GetFirstInstruction() != nullptr
1019 && GetPhis().IsEmpty()
1020 && GetFirstInstruction() == GetLastInstruction()
1021 && GetLastInstruction()->IsGoto()
1022 // Back edges generate the suspend check.
1023 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1024}
1025
David Brazdil8d5b8b22015-03-24 10:51:52 +00001026bool HBasicBlock::EndsWithControlFlowInstruction() const {
1027 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
1028}
1029
David Brazdilb2bd1c52015-03-25 11:17:37 +00001030bool HBasicBlock::EndsWithIf() const {
1031 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
1032}
1033
1034bool HBasicBlock::HasSinglePhi() const {
1035 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
1036}
1037
David Brazdil2d7352b2015-04-20 14:52:42 +01001038size_t HInstructionList::CountSize() const {
1039 size_t size = 0;
1040 HInstruction* current = first_instruction_;
1041 for (; current != nullptr; current = current->GetNext()) {
1042 size++;
1043 }
1044 return size;
1045}
1046
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001047void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
1048 for (HInstruction* current = first_instruction_;
1049 current != nullptr;
1050 current = current->GetNext()) {
1051 current->SetBlock(block);
1052 }
1053}
1054
1055void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
1056 DCHECK(Contains(cursor));
1057 if (!instruction_list.IsEmpty()) {
1058 if (cursor == last_instruction_) {
1059 last_instruction_ = instruction_list.last_instruction_;
1060 } else {
1061 cursor->next_->previous_ = instruction_list.last_instruction_;
1062 }
1063 instruction_list.last_instruction_->next_ = cursor->next_;
1064 cursor->next_ = instruction_list.first_instruction_;
1065 instruction_list.first_instruction_->previous_ = cursor;
1066 }
1067}
1068
1069void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +00001070 if (IsEmpty()) {
1071 first_instruction_ = instruction_list.first_instruction_;
1072 last_instruction_ = instruction_list.last_instruction_;
1073 } else {
1074 AddAfter(last_instruction_, instruction_list);
1075 }
1076}
1077
David Brazdil2d7352b2015-04-20 14:52:42 +01001078void HBasicBlock::DisconnectAndDelete() {
1079 // Dominators must be removed after all the blocks they dominate. This way
1080 // a loop header is removed last, a requirement for correct loop information
1081 // iteration.
1082 DCHECK(dominated_blocks_.IsEmpty());
David Brazdil46e2a392015-03-16 17:31:52 +00001083
David Brazdil2d7352b2015-04-20 14:52:42 +01001084 // Remove the block from all loops it is included in.
1085 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
1086 HLoopInformation* loop_info = it.Current();
1087 loop_info->Remove(this);
1088 if (loop_info->IsBackEdge(*this)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001089 // If this was the last back edge of the loop, we deliberately leave the
1090 // loop in an inconsistent state and will fail SSAChecker unless the
1091 // entire loop is removed during the pass.
David Brazdil2d7352b2015-04-20 14:52:42 +01001092 loop_info->RemoveBackEdge(this);
1093 }
1094 }
1095
1096 // Disconnect the block from its predecessors and update their control-flow
1097 // instructions.
David Brazdil46e2a392015-03-16 17:31:52 +00001098 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001099 HBasicBlock* predecessor = predecessors_.Get(i);
1100 HInstruction* last_instruction = predecessor->GetLastInstruction();
1101 predecessor->RemoveInstruction(last_instruction);
1102 predecessor->RemoveSuccessor(this);
1103 if (predecessor->GetSuccessors().Size() == 1u) {
1104 DCHECK(last_instruction->IsIf());
1105 predecessor->AddInstruction(new (graph_->GetArena()) HGoto());
1106 } else {
1107 // The predecessor has no remaining successors and therefore must be dead.
1108 // We deliberately leave it without a control-flow instruction so that the
1109 // SSAChecker fails unless it is not removed during the pass too.
1110 DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u);
1111 }
David Brazdil46e2a392015-03-16 17:31:52 +00001112 }
David Brazdil46e2a392015-03-16 17:31:52 +00001113 predecessors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001114
1115 // Disconnect the block from its successors and update their dominators
1116 // and phis.
1117 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
1118 HBasicBlock* successor = successors_.Get(i);
1119 // Delete this block from the list of predecessors.
1120 size_t this_index = successor->GetPredecessorIndexOf(this);
1121 successor->predecessors_.DeleteAt(this_index);
1122
1123 // Check that `successor` has other predecessors, otherwise `this` is the
1124 // dominator of `successor` which violates the order DCHECKed at the top.
1125 DCHECK(!successor->predecessors_.IsEmpty());
1126
1127 // Recompute the successor's dominator.
1128 HBasicBlock* old_dominator = successor->GetDominator();
1129 HBasicBlock* new_dominator = successor->predecessors_.Get(0);
1130 for (size_t j = 1, f = successor->predecessors_.Size(); j < f; ++j) {
1131 new_dominator = graph_->FindCommonDominator(
1132 new_dominator, successor->predecessors_.Get(j));
1133 }
1134 if (old_dominator != new_dominator) {
1135 successor->SetDominator(new_dominator);
1136 old_dominator->RemoveDominatedBlock(successor);
1137 new_dominator->AddDominatedBlock(successor);
1138 }
1139
1140 // Remove this block's entries in the successor's phis.
1141 if (successor->predecessors_.Size() == 1u) {
1142 // The successor has just one predecessor left. Replace phis with the only
1143 // remaining input.
1144 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1145 HPhi* phi = phi_it.Current()->AsPhi();
1146 phi->ReplaceWith(phi->InputAt(1 - this_index));
1147 successor->RemovePhi(phi);
1148 }
1149 } else {
1150 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1151 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1152 }
1153 }
1154 }
David Brazdil46e2a392015-03-16 17:31:52 +00001155 successors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001156
1157 // Disconnect from the dominator.
1158 dominator_->RemoveDominatedBlock(this);
1159 SetDominator(nullptr);
1160
1161 // Delete from the graph. The function safely deletes remaining instructions
1162 // and updates the reverse post order.
1163 graph_->DeleteDeadBlock(this);
1164 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001165}
1166
1167void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001168 DCHECK_EQ(GetGraph(), other->GetGraph());
1169 DCHECK(GetDominatedBlocks().Contains(other));
1170 DCHECK_EQ(GetSuccessors().Size(), 1u);
1171 DCHECK_EQ(GetSuccessors().Get(0), other);
1172 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1173 DCHECK_EQ(other->GetPredecessors().Get(0), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001174 DCHECK(other->GetPhis().IsEmpty());
1175
David Brazdil2d7352b2015-04-20 14:52:42 +01001176 // Move instructions from `other` to `this`.
1177 DCHECK(EndsWithControlFlowInstruction());
1178 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001179 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001180 other->instructions_.SetBlockOfInstructions(this);
1181 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001182
David Brazdil2d7352b2015-04-20 14:52:42 +01001183 // Remove `other` from the loops it is included in.
1184 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1185 HLoopInformation* loop_info = it.Current();
1186 loop_info->Remove(other);
1187 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001188 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01001189 }
1190 }
1191
1192 // Update links to the successors of `other`.
1193 successors_.Reset();
1194 while (!other->successors_.IsEmpty()) {
1195 HBasicBlock* successor = other->successors_.Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001196 successor->ReplacePredecessor(other, this);
1197 }
1198
David Brazdil2d7352b2015-04-20 14:52:42 +01001199 // Update the dominator tree.
1200 dominated_blocks_.Delete(other);
1201 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1202 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1203 dominated_blocks_.Add(dominated);
1204 dominated->SetDominator(this);
1205 }
1206 other->dominated_blocks_.Reset();
1207 other->dominator_ = nullptr;
1208
1209 // Clear the list of predecessors of `other` in preparation of deleting it.
1210 other->predecessors_.Reset();
1211
1212 // Delete `other` from the graph. The function updates reverse post order.
1213 graph_->DeleteDeadBlock(other);
1214 other->SetGraph(nullptr);
1215}
1216
1217void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1218 DCHECK_NE(GetGraph(), other->GetGraph());
1219 DCHECK(GetDominatedBlocks().IsEmpty());
1220 DCHECK(GetSuccessors().IsEmpty());
1221 DCHECK(!EndsWithControlFlowInstruction());
1222 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1223 DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock());
1224 DCHECK(other->GetPhis().IsEmpty());
1225 DCHECK(!other->IsInLoop());
1226
1227 // Move instructions from `other` to `this`.
1228 instructions_.Add(other->GetInstructions());
1229 other->instructions_.SetBlockOfInstructions(this);
1230
1231 // Update links to the successors of `other`.
1232 successors_.Reset();
1233 while (!other->successors_.IsEmpty()) {
1234 HBasicBlock* successor = other->successors_.Get(0);
1235 successor->ReplacePredecessor(other, this);
1236 }
1237
1238 // Update the dominator tree.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001239 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1240 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1241 dominated_blocks_.Add(dominated);
1242 dominated->SetDominator(this);
1243 }
1244 other->dominated_blocks_.Reset();
1245 other->dominator_ = nullptr;
1246 other->graph_ = nullptr;
1247}
1248
1249void HBasicBlock::ReplaceWith(HBasicBlock* other) {
1250 while (!GetPredecessors().IsEmpty()) {
1251 HBasicBlock* predecessor = GetPredecessors().Get(0);
1252 predecessor->ReplaceSuccessor(this, other);
1253 }
1254 while (!GetSuccessors().IsEmpty()) {
1255 HBasicBlock* successor = GetSuccessors().Get(0);
1256 successor->ReplacePredecessor(this, other);
1257 }
1258 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
1259 other->AddDominatedBlock(dominated_blocks_.Get(i));
1260 }
1261 GetDominator()->ReplaceDominatedBlock(this, other);
1262 other->SetDominator(GetDominator());
1263 dominator_ = nullptr;
1264 graph_ = nullptr;
1265}
1266
1267// Create space in `blocks` for adding `number_of_new_blocks` entries
1268// starting at location `at`. Blocks after `at` are moved accordingly.
1269static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
1270 size_t number_of_new_blocks,
1271 size_t at) {
1272 size_t old_size = blocks->Size();
1273 size_t new_size = old_size + number_of_new_blocks;
1274 blocks->SetSize(new_size);
1275 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
1276 blocks->Put(j, blocks->Get(i));
1277 }
1278}
1279
David Brazdil2d7352b2015-04-20 14:52:42 +01001280void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1281 DCHECK_EQ(block->GetGraph(), this);
1282 DCHECK(block->GetSuccessors().IsEmpty());
1283 DCHECK(block->GetPredecessors().IsEmpty());
1284 DCHECK(block->GetDominatedBlocks().IsEmpty());
1285 DCHECK(block->GetDominator() == nullptr);
1286
1287 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1288 block->RemoveInstruction(it.Current());
1289 }
1290 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1291 block->RemovePhi(it.Current()->AsPhi());
1292 }
1293
1294 reverse_post_order_.Delete(block);
1295 blocks_.Put(block->GetBlockId(), nullptr);
1296}
1297
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001298void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001299 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001300 // Simple case of an entry block, a body block, and an exit block.
1301 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001302 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001303 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
1304 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001305 DCHECK(!body->IsExitBlock());
1306 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001307
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001308 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1309 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001310
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001311 // Replace the invoke with the return value of the inlined graph.
1312 if (last->IsReturn()) {
1313 invoke->ReplaceWith(last->InputAt(0));
1314 } else {
1315 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001316 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001317
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001318 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001319 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001320 // Need to inline multiple blocks. We split `invoke`'s block
1321 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001322 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001323 // with the second half.
1324 ArenaAllocator* allocator = outer_graph->GetArena();
1325 HBasicBlock* at = invoke->GetBlock();
1326 HBasicBlock* to = at->SplitAfter(invoke);
1327
1328 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
1329 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001330 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001331 exit_block_->ReplaceWith(to);
1332
1333 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001334 // to not `HReturn` but `HGoto` instead.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001335 HInstruction* return_value = nullptr;
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001336 bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
1337 if (to->GetPredecessors().Size() == 1) {
1338 HBasicBlock* predecessor = to->GetPredecessors().Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001339 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001340 if (!returns_void) {
1341 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001342 }
1343 predecessor->AddInstruction(new (allocator) HGoto());
1344 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001345 } else {
1346 if (!returns_void) {
1347 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001348 return_value = new (allocator) HPhi(
1349 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001350 to->AddPhi(return_value->AsPhi());
1351 }
1352 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
1353 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
1354 HInstruction* last = predecessor->GetLastInstruction();
1355 if (!returns_void) {
1356 return_value->AsPhi()->AddInput(last->InputAt(0));
1357 }
1358 predecessor->AddInstruction(new (allocator) HGoto());
1359 predecessor->RemoveInstruction(last);
1360 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001361 }
1362
1363 if (return_value != nullptr) {
1364 invoke->ReplaceWith(return_value);
1365 }
1366
1367 // Update the meta information surrounding blocks:
1368 // (1) the graph they are now in,
1369 // (2) the reverse post order of that graph,
1370 // (3) the potential loop information they are now in.
1371
1372 // We don't add the entry block, the exit block, and the first block, which
1373 // has been merged with `at`.
1374 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1375
1376 // We add the `to` block.
1377 static constexpr int kNumberOfNewBlocksInCaller = 1;
1378 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
1379 + kNumberOfNewBlocksInCaller;
1380
1381 // Find the location of `at` in the outer graph's reverse post order. The new
1382 // blocks will be added after it.
1383 size_t index_of_at = 0;
1384 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
1385 index_of_at++;
1386 }
1387 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1388
1389 // Do a reverse post order of the blocks in the callee and do (1), (2),
1390 // and (3) to the blocks that apply.
1391 HLoopInformation* info = at->GetLoopInformation();
1392 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1393 HBasicBlock* current = it.Current();
1394 if (current != exit_block_ && current != entry_block_ && current != first) {
1395 DCHECK(!current->IsInLoop());
1396 DCHECK(current->GetGraph() == this);
1397 current->SetGraph(outer_graph);
1398 outer_graph->AddBlock(current);
1399 outer_graph->reverse_post_order_.Put(++index_of_at, current);
1400 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001401 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001402 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1403 loop_it.Current()->Add(current);
1404 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001405 }
1406 }
1407 }
1408
1409 // Do (1), (2), and (3) to `to`.
1410 to->SetGraph(outer_graph);
1411 outer_graph->AddBlock(to);
1412 outer_graph->reverse_post_order_.Put(++index_of_at, to);
1413 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001414 to->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001415 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1416 loop_it.Current()->Add(to);
1417 }
David Brazdil46e2a392015-03-16 17:31:52 +00001418 if (info->IsBackEdge(*at)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001419 // Only `to` can become a back edge, as the inlined blocks
1420 // are predecessors of `to`.
1421 info->ReplaceBackEdge(at, to);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001422 }
1423 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001424 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001425
David Brazdil05144f42015-04-16 15:18:00 +01001426 // Update the next instruction id of the outer graph, so that instructions
1427 // added later get bigger ids than those in the inner graph.
1428 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1429
1430 // Walk over the entry block and:
1431 // - Move constants from the entry block to the outer_graph's entry block,
1432 // - Replace HParameterValue instructions with their real value.
1433 // - Remove suspend checks, that hold an environment.
1434 // We must do this after the other blocks have been inlined, otherwise ids of
1435 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001436 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001437 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1438 HInstruction* current = it.Current();
1439 if (current->IsNullConstant()) {
1440 current->ReplaceWith(outer_graph->GetNullConstant());
1441 } else if (current->IsIntConstant()) {
1442 current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue()));
1443 } else if (current->IsLongConstant()) {
1444 current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue()));
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001445 } else if (current->IsFloatConstant()) {
1446 current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue()));
1447 } else if (current->IsDoubleConstant()) {
1448 current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue()));
David Brazdil05144f42015-04-16 15:18:00 +01001449 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001450 if (kIsDebugBuild
1451 && invoke->IsInvokeStaticOrDirect()
1452 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1453 // Ensure we do not use the last input of `invoke`, as it
1454 // contains a clinit check which is not an actual argument.
1455 size_t last_input_index = invoke->InputCount() - 1;
1456 DCHECK(parameter_index != last_input_index);
1457 }
David Brazdil05144f42015-04-16 15:18:00 +01001458 current->ReplaceWith(invoke->InputAt(parameter_index++));
1459 } else {
1460 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1461 entry_block_->RemoveInstruction(current);
1462 }
1463 }
1464
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001465 // Finally remove the invoke from the caller.
1466 invoke->GetBlock()->RemoveInstruction(invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001467}
1468
Calin Juravleacf735c2015-02-12 15:25:22 +00001469std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1470 ScopedObjectAccess soa(Thread::Current());
1471 os << "["
1472 << " is_top=" << rhs.IsTop()
1473 << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
1474 << " is_exact=" << rhs.IsExact()
1475 << " ]";
1476 return os;
1477}
1478
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001479} // namespace art