blob: 2ece5a559ced4c3dea645c7943429089ef1b68f7 [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"
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 Brazdila4b8c212015-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 Brazdila4b8c212015-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
Mark Mendelle82549b2015-05-06 10:55:34 -0400798HConstant* HTypeConversion::TryStaticEvaluation() const {
799 HGraph* graph = GetBlock()->GetGraph();
800 if (GetInput()->IsIntConstant()) {
801 int32_t value = GetInput()->AsIntConstant()->GetValue();
802 switch (GetResultType()) {
803 case Primitive::kPrimLong:
804 return graph->GetLongConstant(static_cast<int64_t>(value));
805 case Primitive::kPrimFloat:
806 return graph->GetFloatConstant(static_cast<float>(value));
807 case Primitive::kPrimDouble:
808 return graph->GetDoubleConstant(static_cast<double>(value));
809 default:
810 return nullptr;
811 }
812 } else if (GetInput()->IsLongConstant()) {
813 int64_t value = GetInput()->AsLongConstant()->GetValue();
814 switch (GetResultType()) {
815 case Primitive::kPrimInt:
816 return graph->GetIntConstant(static_cast<int32_t>(value));
817 case Primitive::kPrimFloat:
818 return graph->GetFloatConstant(static_cast<float>(value));
819 case Primitive::kPrimDouble:
820 return graph->GetDoubleConstant(static_cast<double>(value));
821 default:
822 return nullptr;
823 }
824 } else if (GetInput()->IsFloatConstant()) {
825 float value = GetInput()->AsFloatConstant()->GetValue();
826 switch (GetResultType()) {
827 case Primitive::kPrimInt:
828 if (std::isnan(value))
829 return graph->GetIntConstant(0);
830 if (value >= kPrimIntMax)
831 return graph->GetIntConstant(kPrimIntMax);
832 if (value <= kPrimIntMin)
833 return graph->GetIntConstant(kPrimIntMin);
834 return graph->GetIntConstant(static_cast<int32_t>(value));
835 case Primitive::kPrimLong:
836 if (std::isnan(value))
837 return graph->GetLongConstant(0);
838 if (value >= kPrimLongMax)
839 return graph->GetLongConstant(kPrimLongMax);
840 if (value <= kPrimLongMin)
841 return graph->GetLongConstant(kPrimLongMin);
842 return graph->GetLongConstant(static_cast<int64_t>(value));
843 case Primitive::kPrimDouble:
844 return graph->GetDoubleConstant(static_cast<double>(value));
845 default:
846 return nullptr;
847 }
848 } else if (GetInput()->IsDoubleConstant()) {
849 double value = GetInput()->AsDoubleConstant()->GetValue();
850 switch (GetResultType()) {
851 case Primitive::kPrimInt:
852 if (std::isnan(value))
853 return graph->GetIntConstant(0);
854 if (value >= kPrimIntMax)
855 return graph->GetIntConstant(kPrimIntMax);
856 if (value <= kPrimLongMin)
857 return graph->GetIntConstant(kPrimIntMin);
858 return graph->GetIntConstant(static_cast<int32_t>(value));
859 case Primitive::kPrimLong:
860 if (std::isnan(value))
861 return graph->GetLongConstant(0);
862 if (value >= kPrimLongMax)
863 return graph->GetLongConstant(kPrimLongMax);
864 if (value <= kPrimLongMin)
865 return graph->GetLongConstant(kPrimLongMin);
866 return graph->GetLongConstant(static_cast<int64_t>(value));
867 case Primitive::kPrimFloat:
868 return graph->GetFloatConstant(static_cast<float>(value));
869 default:
870 return nullptr;
871 }
872 }
873 return nullptr;
874}
875
Roland Levillain9240d6a2014-10-20 16:47:04 +0100876HConstant* HUnaryOperation::TryStaticEvaluation() const {
877 if (GetInput()->IsIntConstant()) {
878 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000879 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain9240d6a2014-10-20 16:47:04 +0100880 } else if (GetInput()->IsLongConstant()) {
Roland Levillainb762d2e2014-10-22 10:11:06 +0100881 // TODO: Implement static evaluation of long unary operations.
882 //
883 // Do not exit with a fatal condition here. Instead, simply
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700884 // return `null' to notify the caller that this instruction
Roland Levillainb762d2e2014-10-22 10:11:06 +0100885 // cannot (yet) be statically evaluated.
Roland Levillain9240d6a2014-10-20 16:47:04 +0100886 return nullptr;
887 }
888 return nullptr;
889}
890
891HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +0100892 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
893 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
894 GetRight()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000895 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100896 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
897 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
898 GetRight()->AsLongConstant()->GetValue());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000899 if (GetResultType() == Primitive::kPrimLong) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000900 return GetBlock()->GetGraph()->GetLongConstant(value);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000901 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000902 DCHECK_EQ(GetResultType(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000903 return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000904 }
Roland Levillain556c3d12014-09-18 15:25:07 +0100905 }
906 return nullptr;
907}
Dave Allison20dfc792014-06-16 20:44:29 -0700908
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000909HConstant* HBinaryOperation::GetConstantRight() const {
910 if (GetRight()->IsConstant()) {
911 return GetRight()->AsConstant();
912 } else if (IsCommutative() && GetLeft()->IsConstant()) {
913 return GetLeft()->AsConstant();
914 } else {
915 return nullptr;
916 }
917}
918
919// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700920// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000921HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
922 HInstruction* most_constant_right = GetConstantRight();
923 if (most_constant_right == nullptr) {
924 return nullptr;
925 } else if (most_constant_right == GetLeft()) {
926 return GetRight();
927 } else {
928 return GetLeft();
929 }
930}
931
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700932bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
933 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100934}
935
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100936bool HInstruction::Equals(HInstruction* other) const {
937 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100938 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100939 if (!InstructionDataEquals(other)) return false;
940 if (GetType() != other->GetType()) return false;
941 if (InputCount() != other->InputCount()) return false;
942
943 for (size_t i = 0, e = InputCount(); i < e; ++i) {
944 if (InputAt(i) != other->InputAt(i)) return false;
945 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100946 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100947 return true;
948}
949
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700950std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
951#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
952 switch (rhs) {
953 FOR_EACH_INSTRUCTION(DECLARE_CASE)
954 default:
955 os << "Unknown instruction kind " << static_cast<int>(rhs);
956 break;
957 }
958#undef DECLARE_CASE
959 return os;
960}
961
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000962void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000963 next_->previous_ = previous_;
964 if (previous_ != nullptr) {
965 previous_->next_ = next_;
966 }
967 if (block_->instructions_.first_instruction_ == this) {
968 block_->instructions_.first_instruction_ = next_;
969 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000970 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000971
972 previous_ = cursor->previous_;
973 if (previous_ != nullptr) {
974 previous_->next_ = this;
975 }
976 next_ = cursor;
977 cursor->previous_ = this;
978 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000979
980 if (block_->instructions_.first_instruction_ == cursor) {
981 block_->instructions_.first_instruction_ = this;
982 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000983}
984
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000985HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
986 DCHECK(!cursor->IsControlFlow());
987 DCHECK_NE(instructions_.last_instruction_, cursor);
988 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000989
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000990 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
991 new_block->instructions_.first_instruction_ = cursor->GetNext();
992 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
993 cursor->next_->previous_ = nullptr;
994 cursor->next_ = nullptr;
995 instructions_.last_instruction_ = cursor;
996
997 new_block->instructions_.SetBlockOfInstructions(new_block);
998 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
999 HBasicBlock* successor = GetSuccessors().Get(i);
1000 new_block->successors_.Add(successor);
1001 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
1002 }
1003 successors_.Reset();
1004
1005 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
1006 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
1007 dominated->dominator_ = new_block;
1008 new_block->dominated_blocks_.Add(dominated);
1009 }
1010 dominated_blocks_.Reset();
1011 return new_block;
1012}
1013
David Brazdil46e2a392015-03-16 17:31:52 +00001014bool HBasicBlock::IsSingleGoto() const {
1015 HLoopInformation* loop_info = GetLoopInformation();
1016 // TODO: Remove the null check b/19084197.
1017 return GetFirstInstruction() != nullptr
1018 && GetPhis().IsEmpty()
1019 && GetFirstInstruction() == GetLastInstruction()
1020 && GetLastInstruction()->IsGoto()
1021 // Back edges generate the suspend check.
1022 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1023}
1024
David Brazdil8d5b8b22015-03-24 10:51:52 +00001025bool HBasicBlock::EndsWithControlFlowInstruction() const {
1026 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
1027}
1028
David Brazdilb2bd1c52015-03-25 11:17:37 +00001029bool HBasicBlock::EndsWithIf() const {
1030 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
1031}
1032
1033bool HBasicBlock::HasSinglePhi() const {
1034 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
1035}
1036
David Brazdil2d7352b2015-04-20 14:52:42 +01001037size_t HInstructionList::CountSize() const {
1038 size_t size = 0;
1039 HInstruction* current = first_instruction_;
1040 for (; current != nullptr; current = current->GetNext()) {
1041 size++;
1042 }
1043 return size;
1044}
1045
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001046void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
1047 for (HInstruction* current = first_instruction_;
1048 current != nullptr;
1049 current = current->GetNext()) {
1050 current->SetBlock(block);
1051 }
1052}
1053
1054void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
1055 DCHECK(Contains(cursor));
1056 if (!instruction_list.IsEmpty()) {
1057 if (cursor == last_instruction_) {
1058 last_instruction_ = instruction_list.last_instruction_;
1059 } else {
1060 cursor->next_->previous_ = instruction_list.last_instruction_;
1061 }
1062 instruction_list.last_instruction_->next_ = cursor->next_;
1063 cursor->next_ = instruction_list.first_instruction_;
1064 instruction_list.first_instruction_->previous_ = cursor;
1065 }
1066}
1067
1068void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +00001069 if (IsEmpty()) {
1070 first_instruction_ = instruction_list.first_instruction_;
1071 last_instruction_ = instruction_list.last_instruction_;
1072 } else {
1073 AddAfter(last_instruction_, instruction_list);
1074 }
1075}
1076
David Brazdil2d7352b2015-04-20 14:52:42 +01001077void HBasicBlock::DisconnectAndDelete() {
1078 // Dominators must be removed after all the blocks they dominate. This way
1079 // a loop header is removed last, a requirement for correct loop information
1080 // iteration.
1081 DCHECK(dominated_blocks_.IsEmpty());
David Brazdil46e2a392015-03-16 17:31:52 +00001082
David Brazdil2d7352b2015-04-20 14:52:42 +01001083 // Remove the block from all loops it is included in.
1084 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
1085 HLoopInformation* loop_info = it.Current();
1086 loop_info->Remove(this);
1087 if (loop_info->IsBackEdge(*this)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001088 // If this was the last back edge of the loop, we deliberately leave the
1089 // loop in an inconsistent state and will fail SSAChecker unless the
1090 // entire loop is removed during the pass.
David Brazdil2d7352b2015-04-20 14:52:42 +01001091 loop_info->RemoveBackEdge(this);
1092 }
1093 }
1094
1095 // Disconnect the block from its predecessors and update their control-flow
1096 // instructions.
David Brazdil46e2a392015-03-16 17:31:52 +00001097 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001098 HBasicBlock* predecessor = predecessors_.Get(i);
1099 HInstruction* last_instruction = predecessor->GetLastInstruction();
1100 predecessor->RemoveInstruction(last_instruction);
1101 predecessor->RemoveSuccessor(this);
1102 if (predecessor->GetSuccessors().Size() == 1u) {
1103 DCHECK(last_instruction->IsIf());
1104 predecessor->AddInstruction(new (graph_->GetArena()) HGoto());
1105 } else {
1106 // The predecessor has no remaining successors and therefore must be dead.
1107 // We deliberately leave it without a control-flow instruction so that the
1108 // SSAChecker fails unless it is not removed during the pass too.
1109 DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u);
1110 }
David Brazdil46e2a392015-03-16 17:31:52 +00001111 }
David Brazdil46e2a392015-03-16 17:31:52 +00001112 predecessors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001113
1114 // Disconnect the block from its successors and update their dominators
1115 // and phis.
1116 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
1117 HBasicBlock* successor = successors_.Get(i);
1118 // Delete this block from the list of predecessors.
1119 size_t this_index = successor->GetPredecessorIndexOf(this);
1120 successor->predecessors_.DeleteAt(this_index);
1121
1122 // Check that `successor` has other predecessors, otherwise `this` is the
1123 // dominator of `successor` which violates the order DCHECKed at the top.
1124 DCHECK(!successor->predecessors_.IsEmpty());
1125
1126 // Recompute the successor's dominator.
1127 HBasicBlock* old_dominator = successor->GetDominator();
1128 HBasicBlock* new_dominator = successor->predecessors_.Get(0);
1129 for (size_t j = 1, f = successor->predecessors_.Size(); j < f; ++j) {
1130 new_dominator = graph_->FindCommonDominator(
1131 new_dominator, successor->predecessors_.Get(j));
1132 }
1133 if (old_dominator != new_dominator) {
1134 successor->SetDominator(new_dominator);
1135 old_dominator->RemoveDominatedBlock(successor);
1136 new_dominator->AddDominatedBlock(successor);
1137 }
1138
1139 // Remove this block's entries in the successor's phis.
1140 if (successor->predecessors_.Size() == 1u) {
1141 // The successor has just one predecessor left. Replace phis with the only
1142 // remaining input.
1143 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1144 HPhi* phi = phi_it.Current()->AsPhi();
1145 phi->ReplaceWith(phi->InputAt(1 - this_index));
1146 successor->RemovePhi(phi);
1147 }
1148 } else {
1149 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1150 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1151 }
1152 }
1153 }
David Brazdil46e2a392015-03-16 17:31:52 +00001154 successors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001155
1156 // Disconnect from the dominator.
1157 dominator_->RemoveDominatedBlock(this);
1158 SetDominator(nullptr);
1159
1160 // Delete from the graph. The function safely deletes remaining instructions
1161 // and updates the reverse post order.
1162 graph_->DeleteDeadBlock(this);
1163 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001164}
1165
1166void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001167 DCHECK_EQ(GetGraph(), other->GetGraph());
1168 DCHECK(GetDominatedBlocks().Contains(other));
1169 DCHECK_EQ(GetSuccessors().Size(), 1u);
1170 DCHECK_EQ(GetSuccessors().Get(0), other);
1171 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1172 DCHECK_EQ(other->GetPredecessors().Get(0), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001173 DCHECK(other->GetPhis().IsEmpty());
1174
David Brazdil2d7352b2015-04-20 14:52:42 +01001175 // Move instructions from `other` to `this`.
1176 DCHECK(EndsWithControlFlowInstruction());
1177 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001178 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001179 other->instructions_.SetBlockOfInstructions(this);
1180 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001181
David Brazdil2d7352b2015-04-20 14:52:42 +01001182 // Remove `other` from the loops it is included in.
1183 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1184 HLoopInformation* loop_info = it.Current();
1185 loop_info->Remove(other);
1186 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001187 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01001188 }
1189 }
1190
1191 // Update links to the successors of `other`.
1192 successors_.Reset();
1193 while (!other->successors_.IsEmpty()) {
1194 HBasicBlock* successor = other->successors_.Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001195 successor->ReplacePredecessor(other, this);
1196 }
1197
David Brazdil2d7352b2015-04-20 14:52:42 +01001198 // Update the dominator tree.
1199 dominated_blocks_.Delete(other);
1200 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1201 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1202 dominated_blocks_.Add(dominated);
1203 dominated->SetDominator(this);
1204 }
1205 other->dominated_blocks_.Reset();
1206 other->dominator_ = nullptr;
1207
1208 // Clear the list of predecessors of `other` in preparation of deleting it.
1209 other->predecessors_.Reset();
1210
1211 // Delete `other` from the graph. The function updates reverse post order.
1212 graph_->DeleteDeadBlock(other);
1213 other->SetGraph(nullptr);
1214}
1215
1216void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1217 DCHECK_NE(GetGraph(), other->GetGraph());
1218 DCHECK(GetDominatedBlocks().IsEmpty());
1219 DCHECK(GetSuccessors().IsEmpty());
1220 DCHECK(!EndsWithControlFlowInstruction());
1221 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1222 DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock());
1223 DCHECK(other->GetPhis().IsEmpty());
1224 DCHECK(!other->IsInLoop());
1225
1226 // Move instructions from `other` to `this`.
1227 instructions_.Add(other->GetInstructions());
1228 other->instructions_.SetBlockOfInstructions(this);
1229
1230 // Update links to the successors of `other`.
1231 successors_.Reset();
1232 while (!other->successors_.IsEmpty()) {
1233 HBasicBlock* successor = other->successors_.Get(0);
1234 successor->ReplacePredecessor(other, this);
1235 }
1236
1237 // Update the dominator tree.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001238 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1239 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1240 dominated_blocks_.Add(dominated);
1241 dominated->SetDominator(this);
1242 }
1243 other->dominated_blocks_.Reset();
1244 other->dominator_ = nullptr;
1245 other->graph_ = nullptr;
1246}
1247
1248void HBasicBlock::ReplaceWith(HBasicBlock* other) {
1249 while (!GetPredecessors().IsEmpty()) {
1250 HBasicBlock* predecessor = GetPredecessors().Get(0);
1251 predecessor->ReplaceSuccessor(this, other);
1252 }
1253 while (!GetSuccessors().IsEmpty()) {
1254 HBasicBlock* successor = GetSuccessors().Get(0);
1255 successor->ReplacePredecessor(this, other);
1256 }
1257 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
1258 other->AddDominatedBlock(dominated_blocks_.Get(i));
1259 }
1260 GetDominator()->ReplaceDominatedBlock(this, other);
1261 other->SetDominator(GetDominator());
1262 dominator_ = nullptr;
1263 graph_ = nullptr;
1264}
1265
1266// Create space in `blocks` for adding `number_of_new_blocks` entries
1267// starting at location `at`. Blocks after `at` are moved accordingly.
1268static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
1269 size_t number_of_new_blocks,
1270 size_t at) {
1271 size_t old_size = blocks->Size();
1272 size_t new_size = old_size + number_of_new_blocks;
1273 blocks->SetSize(new_size);
1274 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
1275 blocks->Put(j, blocks->Get(i));
1276 }
1277}
1278
David Brazdil2d7352b2015-04-20 14:52:42 +01001279void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1280 DCHECK_EQ(block->GetGraph(), this);
1281 DCHECK(block->GetSuccessors().IsEmpty());
1282 DCHECK(block->GetPredecessors().IsEmpty());
1283 DCHECK(block->GetDominatedBlocks().IsEmpty());
1284 DCHECK(block->GetDominator() == nullptr);
1285
1286 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1287 block->RemoveInstruction(it.Current());
1288 }
1289 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1290 block->RemovePhi(it.Current()->AsPhi());
1291 }
1292
David Brazdilc7af85d2015-05-26 12:05:55 +01001293 if (block->IsExitBlock()) {
1294 exit_block_ = nullptr;
1295 }
1296
David Brazdil2d7352b2015-04-20 14:52:42 +01001297 reverse_post_order_.Delete(block);
1298 blocks_.Put(block->GetBlockId(), nullptr);
1299}
1300
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001301void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
David Brazdilc7af85d2015-05-26 12:05:55 +01001302 DCHECK(HasExitBlock()) << "Unimplemented scenario";
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001303 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001304 // Simple case of an entry block, a body block, and an exit block.
1305 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001306 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001307 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
1308 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001309 DCHECK(!body->IsExitBlock());
1310 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001311
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001312 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1313 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001314
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001315 // Replace the invoke with the return value of the inlined graph.
1316 if (last->IsReturn()) {
1317 invoke->ReplaceWith(last->InputAt(0));
1318 } else {
1319 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001320 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001321
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001322 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001323 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001324 // Need to inline multiple blocks. We split `invoke`'s block
1325 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001326 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001327 // with the second half.
1328 ArenaAllocator* allocator = outer_graph->GetArena();
1329 HBasicBlock* at = invoke->GetBlock();
1330 HBasicBlock* to = at->SplitAfter(invoke);
1331
1332 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
1333 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001334 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001335 exit_block_->ReplaceWith(to);
1336
1337 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001338 // to not `HReturn` but `HGoto` instead.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001339 HInstruction* return_value = nullptr;
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001340 bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
1341 if (to->GetPredecessors().Size() == 1) {
1342 HBasicBlock* predecessor = to->GetPredecessors().Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001343 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001344 if (!returns_void) {
1345 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001346 }
1347 predecessor->AddInstruction(new (allocator) HGoto());
1348 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001349 } else {
1350 if (!returns_void) {
1351 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001352 return_value = new (allocator) HPhi(
1353 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001354 to->AddPhi(return_value->AsPhi());
1355 }
1356 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
1357 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
1358 HInstruction* last = predecessor->GetLastInstruction();
1359 if (!returns_void) {
1360 return_value->AsPhi()->AddInput(last->InputAt(0));
1361 }
1362 predecessor->AddInstruction(new (allocator) HGoto());
1363 predecessor->RemoveInstruction(last);
1364 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001365 }
1366
1367 if (return_value != nullptr) {
1368 invoke->ReplaceWith(return_value);
1369 }
1370
1371 // Update the meta information surrounding blocks:
1372 // (1) the graph they are now in,
1373 // (2) the reverse post order of that graph,
1374 // (3) the potential loop information they are now in.
1375
1376 // We don't add the entry block, the exit block, and the first block, which
1377 // has been merged with `at`.
1378 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1379
1380 // We add the `to` block.
1381 static constexpr int kNumberOfNewBlocksInCaller = 1;
1382 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
1383 + kNumberOfNewBlocksInCaller;
1384
1385 // Find the location of `at` in the outer graph's reverse post order. The new
1386 // blocks will be added after it.
1387 size_t index_of_at = 0;
1388 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
1389 index_of_at++;
1390 }
1391 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1392
1393 // Do a reverse post order of the blocks in the callee and do (1), (2),
1394 // and (3) to the blocks that apply.
1395 HLoopInformation* info = at->GetLoopInformation();
1396 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1397 HBasicBlock* current = it.Current();
1398 if (current != exit_block_ && current != entry_block_ && current != first) {
1399 DCHECK(!current->IsInLoop());
1400 DCHECK(current->GetGraph() == this);
1401 current->SetGraph(outer_graph);
1402 outer_graph->AddBlock(current);
1403 outer_graph->reverse_post_order_.Put(++index_of_at, current);
1404 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001405 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001406 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1407 loop_it.Current()->Add(current);
1408 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001409 }
1410 }
1411 }
1412
1413 // Do (1), (2), and (3) to `to`.
1414 to->SetGraph(outer_graph);
1415 outer_graph->AddBlock(to);
1416 outer_graph->reverse_post_order_.Put(++index_of_at, to);
1417 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001418 to->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001419 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1420 loop_it.Current()->Add(to);
1421 }
David Brazdil46e2a392015-03-16 17:31:52 +00001422 if (info->IsBackEdge(*at)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001423 // Only `to` can become a back edge, as the inlined blocks
1424 // are predecessors of `to`.
1425 info->ReplaceBackEdge(at, to);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001426 }
1427 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001428 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001429
David Brazdil05144f42015-04-16 15:18:00 +01001430 // Update the next instruction id of the outer graph, so that instructions
1431 // added later get bigger ids than those in the inner graph.
1432 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1433
1434 // Walk over the entry block and:
1435 // - Move constants from the entry block to the outer_graph's entry block,
1436 // - Replace HParameterValue instructions with their real value.
1437 // - Remove suspend checks, that hold an environment.
1438 // We must do this after the other blocks have been inlined, otherwise ids of
1439 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001440 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001441 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1442 HInstruction* current = it.Current();
1443 if (current->IsNullConstant()) {
1444 current->ReplaceWith(outer_graph->GetNullConstant());
1445 } else if (current->IsIntConstant()) {
1446 current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue()));
1447 } else if (current->IsLongConstant()) {
1448 current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue()));
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001449 } else if (current->IsFloatConstant()) {
1450 current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue()));
1451 } else if (current->IsDoubleConstant()) {
1452 current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue()));
David Brazdil05144f42015-04-16 15:18:00 +01001453 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001454 if (kIsDebugBuild
1455 && invoke->IsInvokeStaticOrDirect()
1456 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1457 // Ensure we do not use the last input of `invoke`, as it
1458 // contains a clinit check which is not an actual argument.
1459 size_t last_input_index = invoke->InputCount() - 1;
1460 DCHECK(parameter_index != last_input_index);
1461 }
David Brazdil05144f42015-04-16 15:18:00 +01001462 current->ReplaceWith(invoke->InputAt(parameter_index++));
1463 } else {
1464 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1465 entry_block_->RemoveInstruction(current);
1466 }
1467 }
1468
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001469 // Finally remove the invoke from the caller.
1470 invoke->GetBlock()->RemoveInstruction(invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001471}
1472
Calin Juravleacf735c2015-02-12 15:25:22 +00001473std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1474 ScopedObjectAccess soa(Thread::Current());
1475 os << "["
1476 << " is_top=" << rhs.IsTop()
1477 << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
1478 << " is_exact=" << rhs.IsExact()
1479 << " ]";
1480 return os;
1481}
1482
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001483} // namespace art