blob: efaf48cc9f5f9e8298927c96d0038a131a9c828d [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() {
David Brazdilffee3d32015-07-06 11:48:53 +0100101 // (1) Simplify the CFG so that catch blocks have only exceptional incoming
102 // edges. This invariant simplifies building SSA form because Phis cannot
103 // collect both normal- and exceptional-flow values at the same time.
104 SimplifyCatchBlocks();
105
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000106 ArenaBitVector visited(arena_, blocks_.Size(), false);
107
David Brazdilffee3d32015-07-06 11:48:53 +0100108 // (2) Find the back edges in the graph doing a DFS traversal.
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000109 FindBackEdges(&visited);
110
David Brazdilffee3d32015-07-06 11:48:53 +0100111 // (3) Remove instructions and phis from blocks not visited during
Roland Levillainfc600dc2014-12-02 17:16:31 +0000112 // the initial DFS as users from other instructions, so that
113 // users can be safely removed before uses later.
114 RemoveInstructionsAsUsersFromDeadBlocks(visited);
115
David Brazdilffee3d32015-07-06 11:48:53 +0100116 // (4) Remove blocks not visited during the initial DFS.
Roland Levillainfc600dc2014-12-02 17:16:31 +0000117 // Step (4) requires dead blocks to be removed from the
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000118 // predecessors list of live blocks.
119 RemoveDeadBlocks(visited);
120
David Brazdilffee3d32015-07-06 11:48:53 +0100121 // (5) Simplify the CFG now, so that we don't need to recompute
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100122 // dominators and the reverse post order.
123 SimplifyCFG();
124
David Brazdilffee3d32015-07-06 11:48:53 +0100125 // (6) Compute the dominance information and the reverse post order.
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100126 ComputeDominanceInformation();
127}
128
129void HGraph::ClearDominanceInformation() {
130 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
131 it.Current()->ClearDominanceInformation();
132 }
133 reverse_post_order_.Reset();
134}
135
136void HBasicBlock::ClearDominanceInformation() {
137 dominated_blocks_.Reset();
138 dominator_ = nullptr;
139}
140
141void HGraph::ComputeDominanceInformation() {
142 DCHECK(reverse_post_order_.IsEmpty());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000143 GrowableArray<size_t> visits(arena_, blocks_.Size());
144 visits.SetSize(blocks_.Size());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100145 reverse_post_order_.Add(entry_block_);
146 for (size_t i = 0; i < entry_block_->GetSuccessors().Size(); i++) {
147 VisitBlockForDominatorTree(entry_block_->GetSuccessors().Get(i), entry_block_, &visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000148 }
149}
150
151HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
152 ArenaBitVector visited(arena_, blocks_.Size(), false);
153 // Walk the dominator tree of the first block and mark the visited blocks.
154 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000155 visited.SetBit(first->GetBlockId());
156 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000157 }
158 // Walk the dominator tree of the second block until a marked block is found.
159 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000160 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000161 return second;
162 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000163 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000164 }
165 LOG(ERROR) << "Could not find common dominator";
166 return nullptr;
167}
168
169void HGraph::VisitBlockForDominatorTree(HBasicBlock* block,
170 HBasicBlock* predecessor,
171 GrowableArray<size_t>* visits) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000172 if (block->GetDominator() == nullptr) {
173 block->SetDominator(predecessor);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000174 } else {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000175 block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000176 }
177
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000178 visits->Increment(block->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000179 // Once all the forward edges have been visited, we know the immediate
180 // dominator of the block. We can then start visiting its successors.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000181 if (visits->Get(block->GetBlockId()) ==
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100182 block->GetPredecessors().Size() - block->NumberOfBackEdges()) {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100183 block->GetDominator()->AddDominatedBlock(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100184 reverse_post_order_.Add(block);
185 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
186 VisitBlockForDominatorTree(block->GetSuccessors().Get(i), block, visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000187 }
188 }
189}
190
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000191void HGraph::TransformToSsa() {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100192 DCHECK(!reverse_post_order_.IsEmpty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100193 SsaBuilder ssa_builder(this);
194 ssa_builder.BuildSsa();
195}
196
David Brazdilfc6a86a2015-06-26 10:33:45 +0000197HBasicBlock* HGraph::SplitEdge(HBasicBlock* block, HBasicBlock* successor) {
David Brazdil3e187382015-06-26 09:59:52 +0000198 HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc());
199 AddBlock(new_block);
David Brazdil3e187382015-06-26 09:59:52 +0000200 // Use `InsertBetween` to ensure the predecessor index and successor index of
201 // `block` and `successor` are preserved.
202 new_block->InsertBetween(block, successor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000203 return new_block;
204}
205
206void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) {
207 // Insert a new node between `block` and `successor` to split the
208 // critical edge.
209 HBasicBlock* new_block = SplitEdge(block, successor);
210 new_block->AddInstruction(new (arena_) HGoto());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100211 if (successor->IsLoopHeader()) {
212 // If we split at a back edge boundary, make the new block the back edge.
213 HLoopInformation* info = successor->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000214 if (info->IsBackEdge(*block)) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100215 info->RemoveBackEdge(block);
216 info->AddBackEdge(new_block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100217 }
218 }
219}
220
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100221void HGraph::SimplifyLoop(HBasicBlock* header) {
222 HLoopInformation* info = header->GetLoopInformation();
223
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100224 // Make sure the loop has only one pre header. This simplifies SSA building by having
225 // to just look at the pre header to know which locals are initialized at entry of the
226 // loop.
227 size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges();
228 if (number_of_incomings != 1) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100229 HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100230 AddBlock(pre_header);
231 pre_header->AddInstruction(new (arena_) HGoto());
232
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100233 for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) {
234 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100235 if (!info->IsBackEdge(*predecessor)) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100236 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100237 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238 }
239 }
240 pre_header->AddSuccessor(header);
241 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100242
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100243 // Make sure the first predecessor of a loop header is the incoming block.
244 if (info->IsBackEdge(*header->GetPredecessors().Get(0))) {
245 HBasicBlock* to_swap = header->GetPredecessors().Get(0);
246 for (size_t pred = 1, e = header->GetPredecessors().Size(); pred < e; ++pred) {
247 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
248 if (!info->IsBackEdge(*predecessor)) {
249 header->predecessors_.Put(pred, to_swap);
250 header->predecessors_.Put(0, predecessor);
251 break;
252 }
253 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100254 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100255
256 // Place the suspend check at the beginning of the header, so that live registers
257 // will be known when allocating registers. Note that code generation can still
258 // generate the suspend check at the back edge, but needs to be careful with
259 // loop phi spill slots (which are not written to at back edge).
260 HInstruction* first_instruction = header->GetFirstInstruction();
261 if (!first_instruction->IsSuspendCheck()) {
262 HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc());
263 header->InsertInstructionBefore(check, first_instruction);
264 first_instruction = check;
265 }
266 info->SetSuspendCheck(first_instruction->AsSuspendCheck());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100267}
268
David Brazdilffee3d32015-07-06 11:48:53 +0100269static bool CheckIfPredecessorAtIsExceptional(const HBasicBlock& block, size_t pred_idx) {
270 HBasicBlock* predecessor = block.GetPredecessors().Get(pred_idx);
271 if (!predecessor->EndsWithTryBoundary()) {
272 // Only edges from HTryBoundary can be exceptional.
273 return false;
274 }
275 HTryBoundary* try_boundary = predecessor->GetLastInstruction()->AsTryBoundary();
276 if (try_boundary->GetNormalFlowSuccessor() == &block) {
277 // This block is the normal-flow successor of `try_boundary`, but it could
278 // also be one of its exception handlers if catch blocks have not been
279 // simplified yet. Predecessors are unordered, so we will consider the first
280 // occurrence to be the normal edge and a possible second occurrence to be
281 // the exceptional edge.
282 return !block.IsFirstIndexOfPredecessor(predecessor, pred_idx);
283 } else {
284 // This is not the normal-flow successor of `try_boundary`, hence it must be
285 // one of its exception handlers.
286 DCHECK(try_boundary->HasExceptionHandler(block));
287 return true;
288 }
289}
290
291void HGraph::SimplifyCatchBlocks() {
292 for (size_t i = 0; i < blocks_.Size(); ++i) {
293 HBasicBlock* catch_block = blocks_.Get(i);
294 if (!catch_block->IsCatchBlock()) {
295 continue;
296 }
297
298 bool exceptional_predecessors_only = true;
299 for (size_t j = 0; j < catch_block->GetPredecessors().Size(); ++j) {
300 if (!CheckIfPredecessorAtIsExceptional(*catch_block, j)) {
301 exceptional_predecessors_only = false;
302 break;
303 }
304 }
305
306 if (!exceptional_predecessors_only) {
307 // Catch block has normal-flow predecessors and needs to be simplified.
308 // Splitting the block before its first instruction moves all its
309 // instructions into `normal_block` and links the two blocks with a Goto.
310 // Afterwards, incoming normal-flow edges are re-linked to `normal_block`,
311 // leaving `catch_block` with the exceptional edges only.
312 // Note that catch blocks with normal-flow predecessors cannot begin with
313 // a MOVE_EXCEPTION instruction, as guaranteed by the verifier.
314 DCHECK(!catch_block->GetFirstInstruction()->IsLoadException());
315 HBasicBlock* normal_block = catch_block->SplitBefore(catch_block->GetFirstInstruction());
316 for (size_t j = 0; j < catch_block->GetPredecessors().Size(); ++j) {
317 if (!CheckIfPredecessorAtIsExceptional(*catch_block, j)) {
318 catch_block->GetPredecessors().Get(j)->ReplaceSuccessor(catch_block, normal_block);
319 --j;
320 }
321 }
322 }
323 }
324}
325
326void HGraph::ComputeTryBlockInformation() {
327 // Iterate in reverse post order to propagate try membership information from
328 // predecessors to their successors.
329 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
330 HBasicBlock* block = it.Current();
331 if (block->IsEntryBlock() || block->IsCatchBlock()) {
332 // Catch blocks after simplification have only exceptional predecessors
333 // and hence are never in tries.
334 continue;
335 }
336
337 // Infer try membership from the first predecessor. Having simplified loops,
338 // the first predecessor can never be a back edge and therefore it must have
339 // been visited already and had its try membership set.
340 HBasicBlock* first_predecessor = block->GetPredecessors().Get(0);
341 DCHECK(!block->IsLoopHeader() || !block->GetLoopInformation()->IsBackEdge(*first_predecessor));
342 block->SetTryEntry(first_predecessor->ComputeTryEntryOfSuccessors());
343 }
344}
345
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100346void HGraph::SimplifyCFG() {
347 // Simplify the CFG for future analysis, and code generation:
348 // (1): Split critical edges.
349 // (2): Simplify loops by having only one back edge, and one preheader.
350 for (size_t i = 0; i < blocks_.Size(); ++i) {
351 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100352 if (block == nullptr) continue;
David Brazdilffee3d32015-07-06 11:48:53 +0100353 if (block->NumberOfNormalSuccessors() > 1) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100354 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
355 HBasicBlock* successor = block->GetSuccessors().Get(j);
David Brazdilffee3d32015-07-06 11:48:53 +0100356 DCHECK(!successor->IsCatchBlock());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100357 if (successor->GetPredecessors().Size() > 1) {
358 SplitCriticalEdge(block, successor);
359 --j;
360 }
361 }
362 }
363 if (block->IsLoopHeader()) {
364 SimplifyLoop(block);
365 }
366 }
367}
368
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000369bool HGraph::AnalyzeNaturalLoops() const {
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100370 // Order does not matter.
371 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
372 HBasicBlock* block = it.Current();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100373 if (block->IsLoopHeader()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100374 if (block->IsCatchBlock()) {
375 // TODO: Dealing with exceptional back edges could be tricky because
376 // they only approximate the real control flow. Bail out for now.
377 return false;
378 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100379 HLoopInformation* info = block->GetLoopInformation();
380 if (!info->Populate()) {
381 // Abort if the loop is non natural. We currently bailout in such cases.
382 return false;
383 }
384 }
385 }
386 return true;
387}
388
David Brazdil8d5b8b22015-03-24 10:51:52 +0000389void HGraph::InsertConstant(HConstant* constant) {
390 // New constants are inserted before the final control-flow instruction
391 // of the graph, or at its end if called from the graph builder.
392 if (entry_block_->EndsWithControlFlowInstruction()) {
393 entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction());
David Brazdil46e2a392015-03-16 17:31:52 +0000394 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000395 entry_block_->AddInstruction(constant);
David Brazdil46e2a392015-03-16 17:31:52 +0000396 }
397}
398
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000399HNullConstant* HGraph::GetNullConstant() {
Nicolas Geoffray18e68732015-06-17 23:09:05 +0100400 // For simplicity, don't bother reviving the cached null constant if it is
401 // not null and not in a block. Otherwise, we need to clear the instruction
402 // id and/or any invariants the graph is assuming when adding new instructions.
403 if ((cached_null_constant_ == nullptr) || (cached_null_constant_->GetBlock() == nullptr)) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000404 cached_null_constant_ = new (arena_) HNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000405 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000406 }
407 return cached_null_constant_;
408}
409
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100410HCurrentMethod* HGraph::GetCurrentMethod() {
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100411 // For simplicity, don't bother reviving the cached current method if it is
412 // not null and not in a block. Otherwise, we need to clear the instruction
413 // id and/or any invariants the graph is assuming when adding new instructions.
414 if ((cached_current_method_ == nullptr) || (cached_current_method_->GetBlock() == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700415 cached_current_method_ = new (arena_) HCurrentMethod(
416 Is64BitInstructionSet(instruction_set_) ? Primitive::kPrimLong : Primitive::kPrimInt);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100417 if (entry_block_->GetFirstInstruction() == nullptr) {
418 entry_block_->AddInstruction(cached_current_method_);
419 } else {
420 entry_block_->InsertInstructionBefore(
421 cached_current_method_, entry_block_->GetFirstInstruction());
422 }
423 }
424 return cached_current_method_;
425}
426
David Brazdil8d5b8b22015-03-24 10:51:52 +0000427HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value) {
428 switch (type) {
429 case Primitive::Type::kPrimBoolean:
430 DCHECK(IsUint<1>(value));
431 FALLTHROUGH_INTENDED;
432 case Primitive::Type::kPrimByte:
433 case Primitive::Type::kPrimChar:
434 case Primitive::Type::kPrimShort:
435 case Primitive::Type::kPrimInt:
436 DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value));
437 return GetIntConstant(static_cast<int32_t>(value));
438
439 case Primitive::Type::kPrimLong:
440 return GetLongConstant(value);
441
442 default:
443 LOG(FATAL) << "Unsupported constant type";
444 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000445 }
David Brazdil46e2a392015-03-16 17:31:52 +0000446}
447
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000448void HGraph::CacheFloatConstant(HFloatConstant* constant) {
449 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
450 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
451 cached_float_constants_.Overwrite(value, constant);
452}
453
454void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
455 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
456 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
457 cached_double_constants_.Overwrite(value, constant);
458}
459
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000460void HLoopInformation::Add(HBasicBlock* block) {
461 blocks_.SetBit(block->GetBlockId());
462}
463
David Brazdil46e2a392015-03-16 17:31:52 +0000464void HLoopInformation::Remove(HBasicBlock* block) {
465 blocks_.ClearBit(block->GetBlockId());
466}
467
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100468void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
469 if (blocks_.IsBitSet(block->GetBlockId())) {
470 return;
471 }
472
473 blocks_.SetBit(block->GetBlockId());
474 block->SetInLoop(this);
475 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
476 PopulateRecursive(block->GetPredecessors().Get(i));
477 }
478}
479
480bool HLoopInformation::Populate() {
David Brazdila4b8c212015-05-07 09:59:30 +0100481 DCHECK_EQ(blocks_.NumSetBits(), 0u) << "Loop information has already been populated";
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100482 for (size_t i = 0, e = GetBackEdges().Size(); i < e; ++i) {
483 HBasicBlock* back_edge = GetBackEdges().Get(i);
484 DCHECK(back_edge->GetDominator() != nullptr);
485 if (!header_->Dominates(back_edge)) {
486 // This loop is not natural. Do not bother going further.
487 return false;
488 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100489
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100490 // Populate this loop: starting with the back edge, recursively add predecessors
491 // that are not already part of that loop. Set the header as part of the loop
492 // to end the recursion.
493 // This is a recursive implementation of the algorithm described in
494 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
495 blocks_.SetBit(header_->GetBlockId());
496 PopulateRecursive(back_edge);
497 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100498 return true;
499}
500
David Brazdila4b8c212015-05-07 09:59:30 +0100501void HLoopInformation::Update() {
502 HGraph* graph = header_->GetGraph();
503 for (uint32_t id : blocks_.Indexes()) {
504 HBasicBlock* block = graph->GetBlocks().Get(id);
505 // Reset loop information of non-header blocks inside the loop, except
506 // members of inner nested loops because those should already have been
507 // updated by their own LoopInformation.
508 if (block->GetLoopInformation() == this && block != header_) {
509 block->SetLoopInformation(nullptr);
510 }
511 }
512 blocks_.ClearAllBits();
513
514 if (back_edges_.IsEmpty()) {
515 // The loop has been dismantled, delete its suspend check and remove info
516 // from the header.
517 DCHECK(HasSuspendCheck());
518 header_->RemoveInstruction(suspend_check_);
519 header_->SetLoopInformation(nullptr);
520 header_ = nullptr;
521 suspend_check_ = nullptr;
522 } else {
523 if (kIsDebugBuild) {
524 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
525 DCHECK(header_->Dominates(back_edges_.Get(i)));
526 }
527 }
528 // This loop still has reachable back edges. Repopulate the list of blocks.
529 bool populate_successful = Populate();
530 DCHECK(populate_successful);
531 }
532}
533
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100534HBasicBlock* HLoopInformation::GetPreHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100535 return header_->GetDominator();
536}
537
538bool HLoopInformation::Contains(const HBasicBlock& block) const {
539 return blocks_.IsBitSet(block.GetBlockId());
540}
541
542bool HLoopInformation::IsIn(const HLoopInformation& other) const {
543 return other.blocks_.IsBitSet(header_->GetBlockId());
544}
545
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100546size_t HLoopInformation::GetLifetimeEnd() const {
547 size_t last_position = 0;
548 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
549 last_position = std::max(back_edges_.Get(i)->GetLifetimeEnd(), last_position);
550 }
551 return last_position;
552}
553
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100554bool HBasicBlock::Dominates(HBasicBlock* other) const {
555 // Walk up the dominator tree from `other`, to find out if `this`
556 // is an ancestor.
557 HBasicBlock* current = other;
558 while (current != nullptr) {
559 if (current == this) {
560 return true;
561 }
562 current = current->GetDominator();
563 }
564 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100565}
566
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100567static void UpdateInputsUsers(HInstruction* instruction) {
568 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
569 instruction->InputAt(i)->AddUseAt(instruction, i);
570 }
571 // Environment should be created later.
572 DCHECK(!instruction->HasEnvironment());
573}
574
Roland Levillainccc07a92014-09-16 14:48:16 +0100575void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
576 HInstruction* replacement) {
577 DCHECK(initial->GetBlock() == this);
578 InsertInstructionBefore(replacement, initial);
579 initial->ReplaceWith(replacement);
580 RemoveInstruction(initial);
581}
582
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100583static void Add(HInstructionList* instruction_list,
584 HBasicBlock* block,
585 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000586 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000587 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100588 instruction->SetBlock(block);
589 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100590 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100591 instruction_list->AddInstruction(instruction);
592}
593
594void HBasicBlock::AddInstruction(HInstruction* instruction) {
595 Add(&instructions_, this, instruction);
596}
597
598void HBasicBlock::AddPhi(HPhi* phi) {
599 Add(&phis_, this, phi);
600}
601
David Brazdilc3d743f2015-04-22 13:40:50 +0100602void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
603 DCHECK(!cursor->IsPhi());
604 DCHECK(!instruction->IsPhi());
605 DCHECK_EQ(instruction->GetId(), -1);
606 DCHECK_NE(cursor->GetId(), -1);
607 DCHECK_EQ(cursor->GetBlock(), this);
608 DCHECK(!instruction->IsControlFlow());
609 instruction->SetBlock(this);
610 instruction->SetId(GetGraph()->GetNextInstructionId());
611 UpdateInputsUsers(instruction);
612 instructions_.InsertInstructionBefore(instruction, cursor);
613}
614
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100615void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
616 DCHECK(!cursor->IsPhi());
617 DCHECK(!instruction->IsPhi());
618 DCHECK_EQ(instruction->GetId(), -1);
619 DCHECK_NE(cursor->GetId(), -1);
620 DCHECK_EQ(cursor->GetBlock(), this);
621 DCHECK(!instruction->IsControlFlow());
622 DCHECK(!cursor->IsControlFlow());
623 instruction->SetBlock(this);
624 instruction->SetId(GetGraph()->GetNextInstructionId());
625 UpdateInputsUsers(instruction);
626 instructions_.InsertInstructionAfter(instruction, cursor);
627}
628
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100629void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
630 DCHECK_EQ(phi->GetId(), -1);
631 DCHECK_NE(cursor->GetId(), -1);
632 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100633 phi->SetBlock(this);
634 phi->SetId(GetGraph()->GetNextInstructionId());
635 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100636 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100637}
638
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100639static void Remove(HInstructionList* instruction_list,
640 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000641 HInstruction* instruction,
642 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100643 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100644 instruction->SetBlock(nullptr);
645 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000646 if (ensure_safety) {
647 DCHECK(instruction->GetUses().IsEmpty());
648 DCHECK(instruction->GetEnvUses().IsEmpty());
649 RemoveAsUser(instruction);
650 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100651}
652
David Brazdil1abb4192015-02-17 18:33:36 +0000653void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100654 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000655 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100656}
657
David Brazdil1abb4192015-02-17 18:33:36 +0000658void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
659 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100660}
661
David Brazdilc7508e92015-04-27 13:28:57 +0100662void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
663 if (instruction->IsPhi()) {
664 RemovePhi(instruction->AsPhi(), ensure_safety);
665 } else {
666 RemoveInstruction(instruction, ensure_safety);
667 }
668}
669
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +0100670void HEnvironment::CopyFrom(const GrowableArray<HInstruction*>& locals) {
671 for (size_t i = 0; i < locals.Size(); i++) {
672 HInstruction* instruction = locals.Get(i);
673 SetRawEnvAt(i, instruction);
674 if (instruction != nullptr) {
675 instruction->AddEnvUseAt(this, i);
676 }
677 }
678}
679
David Brazdiled596192015-01-23 10:39:45 +0000680void HEnvironment::CopyFrom(HEnvironment* env) {
681 for (size_t i = 0; i < env->Size(); i++) {
682 HInstruction* instruction = env->GetInstructionAt(i);
683 SetRawEnvAt(i, instruction);
684 if (instruction != nullptr) {
685 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100686 }
David Brazdiled596192015-01-23 10:39:45 +0000687 }
688}
689
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700690void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
691 HBasicBlock* loop_header) {
692 DCHECK(loop_header->IsLoopHeader());
693 for (size_t i = 0; i < env->Size(); i++) {
694 HInstruction* instruction = env->GetInstructionAt(i);
695 SetRawEnvAt(i, instruction);
696 if (instruction == nullptr) {
697 continue;
698 }
699 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
700 // At the end of the loop pre-header, the corresponding value for instruction
701 // is the first input of the phi.
702 HInstruction* initial = instruction->AsPhi()->InputAt(0);
703 DCHECK(initial->GetBlock()->Dominates(loop_header));
704 SetRawEnvAt(i, initial);
705 initial->AddEnvUseAt(this, i);
706 } else {
707 instruction->AddEnvUseAt(this, i);
708 }
709 }
710}
711
David Brazdil1abb4192015-02-17 18:33:36 +0000712void HEnvironment::RemoveAsUserOfInput(size_t index) const {
713 const HUserRecord<HEnvironment*> user_record = vregs_.Get(index);
714 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100715}
716
Calin Juravle77520bc2015-01-12 18:45:46 +0000717HInstruction* HInstruction::GetNextDisregardingMoves() const {
718 HInstruction* next = GetNext();
719 while (next != nullptr && next->IsParallelMove()) {
720 next = next->GetNext();
721 }
722 return next;
723}
724
725HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
726 HInstruction* previous = GetPrevious();
727 while (previous != nullptr && previous->IsParallelMove()) {
728 previous = previous->GetPrevious();
729 }
730 return previous;
731}
732
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100733void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000734 if (first_instruction_ == nullptr) {
735 DCHECK(last_instruction_ == nullptr);
736 first_instruction_ = last_instruction_ = instruction;
737 } else {
738 last_instruction_->next_ = instruction;
739 instruction->previous_ = last_instruction_;
740 last_instruction_ = instruction;
741 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000742}
743
David Brazdilc3d743f2015-04-22 13:40:50 +0100744void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
745 DCHECK(Contains(cursor));
746 if (cursor == first_instruction_) {
747 cursor->previous_ = instruction;
748 instruction->next_ = cursor;
749 first_instruction_ = instruction;
750 } else {
751 instruction->previous_ = cursor->previous_;
752 instruction->next_ = cursor;
753 cursor->previous_ = instruction;
754 instruction->previous_->next_ = instruction;
755 }
756}
757
758void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
759 DCHECK(Contains(cursor));
760 if (cursor == last_instruction_) {
761 cursor->next_ = instruction;
762 instruction->previous_ = cursor;
763 last_instruction_ = instruction;
764 } else {
765 instruction->next_ = cursor->next_;
766 instruction->previous_ = cursor;
767 cursor->next_ = instruction;
768 instruction->next_->previous_ = instruction;
769 }
770}
771
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100772void HInstructionList::RemoveInstruction(HInstruction* instruction) {
773 if (instruction->previous_ != nullptr) {
774 instruction->previous_->next_ = instruction->next_;
775 }
776 if (instruction->next_ != nullptr) {
777 instruction->next_->previous_ = instruction->previous_;
778 }
779 if (instruction == first_instruction_) {
780 first_instruction_ = instruction->next_;
781 }
782 if (instruction == last_instruction_) {
783 last_instruction_ = instruction->previous_;
784 }
785}
786
Roland Levillain6b469232014-09-25 10:10:38 +0100787bool HInstructionList::Contains(HInstruction* instruction) const {
788 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
789 if (it.Current() == instruction) {
790 return true;
791 }
792 }
793 return false;
794}
795
Roland Levillainccc07a92014-09-16 14:48:16 +0100796bool HInstructionList::FoundBefore(const HInstruction* instruction1,
797 const HInstruction* instruction2) const {
798 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
799 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
800 if (it.Current() == instruction1) {
801 return true;
802 }
803 if (it.Current() == instruction2) {
804 return false;
805 }
806 }
807 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
808 return true;
809}
810
Roland Levillain6c82d402014-10-13 16:10:27 +0100811bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
812 if (other_instruction == this) {
813 // An instruction does not strictly dominate itself.
814 return false;
815 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100816 HBasicBlock* block = GetBlock();
817 HBasicBlock* other_block = other_instruction->GetBlock();
818 if (block != other_block) {
819 return GetBlock()->Dominates(other_instruction->GetBlock());
820 } else {
821 // If both instructions are in the same block, ensure this
822 // instruction comes before `other_instruction`.
823 if (IsPhi()) {
824 if (!other_instruction->IsPhi()) {
825 // Phis appear before non phi-instructions so this instruction
826 // dominates `other_instruction`.
827 return true;
828 } else {
829 // There is no order among phis.
830 LOG(FATAL) << "There is no dominance between phis of a same block.";
831 return false;
832 }
833 } else {
834 // `this` is not a phi.
835 if (other_instruction->IsPhi()) {
836 // Phis appear before non phi-instructions so this instruction
837 // does not dominate `other_instruction`.
838 return false;
839 } else {
840 // Check whether this instruction comes before
841 // `other_instruction` in the instruction list.
842 return block->GetInstructions().FoundBefore(this, other_instruction);
843 }
844 }
845 }
846}
847
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100848void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100849 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000850 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
851 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852 HInstruction* user = current->GetUser();
853 size_t input_index = current->GetIndex();
854 user->SetRawInputAt(input_index, other);
855 other->AddUseAt(user, input_index);
856 }
857
David Brazdiled596192015-01-23 10:39:45 +0000858 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
859 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100860 HEnvironment* user = current->GetUser();
861 size_t input_index = current->GetIndex();
862 user->SetRawEnvAt(input_index, other);
863 other->AddEnvUseAt(user, input_index);
864 }
865
David Brazdiled596192015-01-23 10:39:45 +0000866 uses_.Clear();
867 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100868}
869
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100870void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000871 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100872 SetRawInputAt(index, replacement);
873 replacement->AddUseAt(this, index);
874}
875
Nicolas Geoffray39468442014-09-02 15:17:15 +0100876size_t HInstruction::EnvironmentSize() const {
877 return HasEnvironment() ? environment_->Size() : 0;
878}
879
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100880void HPhi::AddInput(HInstruction* input) {
881 DCHECK(input->GetBlock() != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +0000882 inputs_.Add(HUserRecord<HInstruction*>(input));
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100883 input->AddUseAt(this, inputs_.Size() - 1);
884}
885
David Brazdil2d7352b2015-04-20 14:52:42 +0100886void HPhi::RemoveInputAt(size_t index) {
887 RemoveAsUserOfInput(index);
888 inputs_.DeleteAt(index);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100889 for (size_t i = index, e = InputCount(); i < e; ++i) {
890 InputRecordAt(i).GetUseNode()->SetIndex(i);
891 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100892}
893
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100894#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000895void H##name::Accept(HGraphVisitor* visitor) { \
896 visitor->Visit##name(this); \
897}
898
899FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
900
901#undef DEFINE_ACCEPT
902
903void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100904 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
905 for (size_t i = 0 ; i < blocks.Size(); i++) {
David Brazdil46e2a392015-03-16 17:31:52 +0000906 HBasicBlock* block = blocks.Get(i);
907 if (block != nullptr) {
908 VisitBasicBlock(block);
909 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910 }
911}
912
Roland Levillain633021e2014-10-01 14:12:25 +0100913void HGraphVisitor::VisitReversePostOrder() {
914 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
915 VisitBasicBlock(it.Current());
916 }
917}
918
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000919void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100920 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100921 it.Current()->Accept(this);
922 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100923 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000924 it.Current()->Accept(this);
925 }
926}
927
Mark Mendelle82549b2015-05-06 10:55:34 -0400928HConstant* HTypeConversion::TryStaticEvaluation() const {
929 HGraph* graph = GetBlock()->GetGraph();
930 if (GetInput()->IsIntConstant()) {
931 int32_t value = GetInput()->AsIntConstant()->GetValue();
932 switch (GetResultType()) {
933 case Primitive::kPrimLong:
934 return graph->GetLongConstant(static_cast<int64_t>(value));
935 case Primitive::kPrimFloat:
936 return graph->GetFloatConstant(static_cast<float>(value));
937 case Primitive::kPrimDouble:
938 return graph->GetDoubleConstant(static_cast<double>(value));
939 default:
940 return nullptr;
941 }
942 } else if (GetInput()->IsLongConstant()) {
943 int64_t value = GetInput()->AsLongConstant()->GetValue();
944 switch (GetResultType()) {
945 case Primitive::kPrimInt:
946 return graph->GetIntConstant(static_cast<int32_t>(value));
947 case Primitive::kPrimFloat:
948 return graph->GetFloatConstant(static_cast<float>(value));
949 case Primitive::kPrimDouble:
950 return graph->GetDoubleConstant(static_cast<double>(value));
951 default:
952 return nullptr;
953 }
954 } else if (GetInput()->IsFloatConstant()) {
955 float value = GetInput()->AsFloatConstant()->GetValue();
956 switch (GetResultType()) {
957 case Primitive::kPrimInt:
958 if (std::isnan(value))
959 return graph->GetIntConstant(0);
960 if (value >= kPrimIntMax)
961 return graph->GetIntConstant(kPrimIntMax);
962 if (value <= kPrimIntMin)
963 return graph->GetIntConstant(kPrimIntMin);
964 return graph->GetIntConstant(static_cast<int32_t>(value));
965 case Primitive::kPrimLong:
966 if (std::isnan(value))
967 return graph->GetLongConstant(0);
968 if (value >= kPrimLongMax)
969 return graph->GetLongConstant(kPrimLongMax);
970 if (value <= kPrimLongMin)
971 return graph->GetLongConstant(kPrimLongMin);
972 return graph->GetLongConstant(static_cast<int64_t>(value));
973 case Primitive::kPrimDouble:
974 return graph->GetDoubleConstant(static_cast<double>(value));
975 default:
976 return nullptr;
977 }
978 } else if (GetInput()->IsDoubleConstant()) {
979 double value = GetInput()->AsDoubleConstant()->GetValue();
980 switch (GetResultType()) {
981 case Primitive::kPrimInt:
982 if (std::isnan(value))
983 return graph->GetIntConstant(0);
984 if (value >= kPrimIntMax)
985 return graph->GetIntConstant(kPrimIntMax);
986 if (value <= kPrimLongMin)
987 return graph->GetIntConstant(kPrimIntMin);
988 return graph->GetIntConstant(static_cast<int32_t>(value));
989 case Primitive::kPrimLong:
990 if (std::isnan(value))
991 return graph->GetLongConstant(0);
992 if (value >= kPrimLongMax)
993 return graph->GetLongConstant(kPrimLongMax);
994 if (value <= kPrimLongMin)
995 return graph->GetLongConstant(kPrimLongMin);
996 return graph->GetLongConstant(static_cast<int64_t>(value));
997 case Primitive::kPrimFloat:
998 return graph->GetFloatConstant(static_cast<float>(value));
999 default:
1000 return nullptr;
1001 }
1002 }
1003 return nullptr;
1004}
1005
Roland Levillain9240d6a2014-10-20 16:47:04 +01001006HConstant* HUnaryOperation::TryStaticEvaluation() const {
1007 if (GetInput()->IsIntConstant()) {
1008 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +00001009 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain9240d6a2014-10-20 16:47:04 +01001010 } else if (GetInput()->IsLongConstant()) {
Roland Levillainc90bc7c2014-12-11 12:14:33 +00001011 int64_t value = Evaluate(GetInput()->AsLongConstant()->GetValue());
1012 return GetBlock()->GetGraph()->GetLongConstant(value);
Roland Levillain9240d6a2014-10-20 16:47:04 +01001013 }
1014 return nullptr;
1015}
1016
1017HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +01001018 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
1019 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
1020 GetRight()->AsIntConstant()->GetValue());
David Brazdil8d5b8b22015-03-24 10:51:52 +00001021 return GetBlock()->GetGraph()->GetIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +01001022 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
1023 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
1024 GetRight()->AsLongConstant()->GetValue());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +00001025 if (GetResultType() == Primitive::kPrimLong) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001026 return GetBlock()->GetGraph()->GetLongConstant(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001027 } else if (GetResultType() == Primitive::kPrimBoolean) {
1028 // This can be the result of an HCondition evaluation.
1029 return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +00001030 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001031 DCHECK_EQ(GetResultType(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +00001032 return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +00001033 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001034 }
1035 return nullptr;
1036}
Dave Allison20dfc792014-06-16 20:44:29 -07001037
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001038HConstant* HBinaryOperation::GetConstantRight() const {
1039 if (GetRight()->IsConstant()) {
1040 return GetRight()->AsConstant();
1041 } else if (IsCommutative() && GetLeft()->IsConstant()) {
1042 return GetLeft()->AsConstant();
1043 } else {
1044 return nullptr;
1045 }
1046}
1047
1048// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001049// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001050HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
1051 HInstruction* most_constant_right = GetConstantRight();
1052 if (most_constant_right == nullptr) {
1053 return nullptr;
1054 } else if (most_constant_right == GetLeft()) {
1055 return GetRight();
1056 } else {
1057 return GetLeft();
1058 }
1059}
1060
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001061bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
1062 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001063}
1064
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001065bool HInstruction::Equals(HInstruction* other) const {
1066 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001067 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001068 if (!InstructionDataEquals(other)) return false;
1069 if (GetType() != other->GetType()) return false;
1070 if (InputCount() != other->InputCount()) return false;
1071
1072 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1073 if (InputAt(i) != other->InputAt(i)) return false;
1074 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001075 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001076 return true;
1077}
1078
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001079std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
1080#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
1081 switch (rhs) {
1082 FOR_EACH_INSTRUCTION(DECLARE_CASE)
1083 default:
1084 os << "Unknown instruction kind " << static_cast<int>(rhs);
1085 break;
1086 }
1087#undef DECLARE_CASE
1088 return os;
1089}
1090
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001091void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001092 next_->previous_ = previous_;
1093 if (previous_ != nullptr) {
1094 previous_->next_ = next_;
1095 }
1096 if (block_->instructions_.first_instruction_ == this) {
1097 block_->instructions_.first_instruction_ = next_;
1098 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001099 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001100
1101 previous_ = cursor->previous_;
1102 if (previous_ != nullptr) {
1103 previous_->next_ = this;
1104 }
1105 next_ = cursor;
1106 cursor->previous_ = this;
1107 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001108
1109 if (block_->instructions_.first_instruction_ == cursor) {
1110 block_->instructions_.first_instruction_ = this;
1111 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001112}
1113
David Brazdilfc6a86a2015-06-26 10:33:45 +00001114HBasicBlock* HBasicBlock::SplitBefore(HInstruction* cursor) {
1115 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented";
1116 DCHECK_EQ(cursor->GetBlock(), this);
1117
1118 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
1119 new_block->instructions_.first_instruction_ = cursor;
1120 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1121 instructions_.last_instruction_ = cursor->previous_;
1122 if (cursor->previous_ == nullptr) {
1123 instructions_.first_instruction_ = nullptr;
1124 } else {
1125 cursor->previous_->next_ = nullptr;
1126 cursor->previous_ = nullptr;
1127 }
1128
1129 new_block->instructions_.SetBlockOfInstructions(new_block);
1130 AddInstruction(new (GetGraph()->GetArena()) HGoto());
1131
1132 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
1133 HBasicBlock* successor = GetSuccessors().Get(i);
1134 new_block->successors_.Add(successor);
1135 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
1136 }
1137 successors_.Reset();
1138 AddSuccessor(new_block);
1139
David Brazdil56e1acc2015-06-30 15:41:36 +01001140 GetGraph()->AddBlock(new_block);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001141 return new_block;
1142}
1143
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001144HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
1145 DCHECK(!cursor->IsControlFlow());
1146 DCHECK_NE(instructions_.last_instruction_, cursor);
1147 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001148
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001149 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
1150 new_block->instructions_.first_instruction_ = cursor->GetNext();
1151 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1152 cursor->next_->previous_ = nullptr;
1153 cursor->next_ = nullptr;
1154 instructions_.last_instruction_ = cursor;
1155
1156 new_block->instructions_.SetBlockOfInstructions(new_block);
1157 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
1158 HBasicBlock* successor = GetSuccessors().Get(i);
1159 new_block->successors_.Add(successor);
1160 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
1161 }
1162 successors_.Reset();
1163
1164 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
1165 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
1166 dominated->dominator_ = new_block;
1167 new_block->dominated_blocks_.Add(dominated);
1168 }
1169 dominated_blocks_.Reset();
1170 return new_block;
1171}
1172
David Brazdilffee3d32015-07-06 11:48:53 +01001173HTryBoundary* HBasicBlock::ComputeTryEntryOfSuccessors() const {
1174 if (EndsWithTryBoundary()) {
1175 HTryBoundary* try_boundary = GetLastInstruction()->AsTryBoundary();
1176 if (try_boundary->IsEntry()) {
1177 DCHECK(try_entry_ == nullptr);
1178 return try_boundary;
1179 } else {
1180 DCHECK(try_entry_ != nullptr);
1181 DCHECK(try_entry_->HasSameExceptionHandlersAs(*try_boundary));
1182 return nullptr;
1183 }
1184 } else {
1185 return try_entry_;
1186 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00001187}
1188
1189static bool HasOnlyOneInstruction(const HBasicBlock& block) {
1190 return block.GetPhis().IsEmpty()
1191 && !block.GetInstructions().IsEmpty()
1192 && block.GetFirstInstruction() == block.GetLastInstruction();
1193}
1194
David Brazdil46e2a392015-03-16 17:31:52 +00001195bool HBasicBlock::IsSingleGoto() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001196 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsGoto();
1197}
1198
1199bool HBasicBlock::IsSingleTryBoundary() const {
1200 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsTryBoundary();
David Brazdil46e2a392015-03-16 17:31:52 +00001201}
1202
David Brazdil8d5b8b22015-03-24 10:51:52 +00001203bool HBasicBlock::EndsWithControlFlowInstruction() const {
1204 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
1205}
1206
David Brazdilb2bd1c52015-03-25 11:17:37 +00001207bool HBasicBlock::EndsWithIf() const {
1208 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
1209}
1210
David Brazdilffee3d32015-07-06 11:48:53 +01001211bool HBasicBlock::EndsWithTryBoundary() const {
1212 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsTryBoundary();
1213}
1214
David Brazdilb2bd1c52015-03-25 11:17:37 +00001215bool HBasicBlock::HasSinglePhi() const {
1216 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
1217}
1218
David Brazdilffee3d32015-07-06 11:48:53 +01001219bool HTryBoundary::HasSameExceptionHandlersAs(const HTryBoundary& other) const {
1220 if (GetBlock()->GetSuccessors().Size() != other.GetBlock()->GetSuccessors().Size()) {
1221 return false;
1222 }
1223
1224 // Exception handler lists cannot contain duplicates, which makes it
1225 // sufficient to test inclusion only in one direction.
1226 for (HExceptionHandlerIterator it(other); !it.Done(); it.Advance()) {
1227 if (!HasExceptionHandler(*it.Current())) {
1228 return false;
1229 }
1230 }
1231 return true;
1232}
1233
David Brazdil2d7352b2015-04-20 14:52:42 +01001234size_t HInstructionList::CountSize() const {
1235 size_t size = 0;
1236 HInstruction* current = first_instruction_;
1237 for (; current != nullptr; current = current->GetNext()) {
1238 size++;
1239 }
1240 return size;
1241}
1242
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001243void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
1244 for (HInstruction* current = first_instruction_;
1245 current != nullptr;
1246 current = current->GetNext()) {
1247 current->SetBlock(block);
1248 }
1249}
1250
1251void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
1252 DCHECK(Contains(cursor));
1253 if (!instruction_list.IsEmpty()) {
1254 if (cursor == last_instruction_) {
1255 last_instruction_ = instruction_list.last_instruction_;
1256 } else {
1257 cursor->next_->previous_ = instruction_list.last_instruction_;
1258 }
1259 instruction_list.last_instruction_->next_ = cursor->next_;
1260 cursor->next_ = instruction_list.first_instruction_;
1261 instruction_list.first_instruction_->previous_ = cursor;
1262 }
1263}
1264
1265void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +00001266 if (IsEmpty()) {
1267 first_instruction_ = instruction_list.first_instruction_;
1268 last_instruction_ = instruction_list.last_instruction_;
1269 } else {
1270 AddAfter(last_instruction_, instruction_list);
1271 }
1272}
1273
David Brazdil2d7352b2015-04-20 14:52:42 +01001274void HBasicBlock::DisconnectAndDelete() {
1275 // Dominators must be removed after all the blocks they dominate. This way
1276 // a loop header is removed last, a requirement for correct loop information
1277 // iteration.
1278 DCHECK(dominated_blocks_.IsEmpty());
David Brazdil46e2a392015-03-16 17:31:52 +00001279
David Brazdil2d7352b2015-04-20 14:52:42 +01001280 // Remove the block from all loops it is included in.
1281 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
1282 HLoopInformation* loop_info = it.Current();
1283 loop_info->Remove(this);
1284 if (loop_info->IsBackEdge(*this)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001285 // If this was the last back edge of the loop, we deliberately leave the
1286 // loop in an inconsistent state and will fail SSAChecker unless the
1287 // entire loop is removed during the pass.
David Brazdil2d7352b2015-04-20 14:52:42 +01001288 loop_info->RemoveBackEdge(this);
1289 }
1290 }
1291
1292 // Disconnect the block from its predecessors and update their control-flow
1293 // instructions.
David Brazdil46e2a392015-03-16 17:31:52 +00001294 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001295 HBasicBlock* predecessor = predecessors_.Get(i);
1296 HInstruction* last_instruction = predecessor->GetLastInstruction();
1297 predecessor->RemoveInstruction(last_instruction);
1298 predecessor->RemoveSuccessor(this);
1299 if (predecessor->GetSuccessors().Size() == 1u) {
1300 DCHECK(last_instruction->IsIf());
1301 predecessor->AddInstruction(new (graph_->GetArena()) HGoto());
1302 } else {
1303 // The predecessor has no remaining successors and therefore must be dead.
1304 // We deliberately leave it without a control-flow instruction so that the
1305 // SSAChecker fails unless it is not removed during the pass too.
1306 DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u);
1307 }
David Brazdil46e2a392015-03-16 17:31:52 +00001308 }
David Brazdil46e2a392015-03-16 17:31:52 +00001309 predecessors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001310
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001311 // Disconnect the block from its successors and update their phis.
David Brazdil2d7352b2015-04-20 14:52:42 +01001312 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
1313 HBasicBlock* successor = successors_.Get(i);
1314 // Delete this block from the list of predecessors.
1315 size_t this_index = successor->GetPredecessorIndexOf(this);
1316 successor->predecessors_.DeleteAt(this_index);
1317
1318 // Check that `successor` has other predecessors, otherwise `this` is the
1319 // dominator of `successor` which violates the order DCHECKed at the top.
1320 DCHECK(!successor->predecessors_.IsEmpty());
1321
David Brazdil2d7352b2015-04-20 14:52:42 +01001322 // Remove this block's entries in the successor's phis.
1323 if (successor->predecessors_.Size() == 1u) {
1324 // The successor has just one predecessor left. Replace phis with the only
1325 // remaining input.
1326 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1327 HPhi* phi = phi_it.Current()->AsPhi();
1328 phi->ReplaceWith(phi->InputAt(1 - this_index));
1329 successor->RemovePhi(phi);
1330 }
1331 } else {
1332 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1333 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1334 }
1335 }
1336 }
David Brazdil46e2a392015-03-16 17:31:52 +00001337 successors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001338
1339 // Disconnect from the dominator.
1340 dominator_->RemoveDominatedBlock(this);
1341 SetDominator(nullptr);
1342
1343 // Delete from the graph. The function safely deletes remaining instructions
1344 // and updates the reverse post order.
1345 graph_->DeleteDeadBlock(this);
1346 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001347}
1348
1349void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001350 DCHECK_EQ(GetGraph(), other->GetGraph());
1351 DCHECK(GetDominatedBlocks().Contains(other));
1352 DCHECK_EQ(GetSuccessors().Size(), 1u);
1353 DCHECK_EQ(GetSuccessors().Get(0), other);
1354 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1355 DCHECK_EQ(other->GetPredecessors().Get(0), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001356 DCHECK(other->GetPhis().IsEmpty());
1357
David Brazdil2d7352b2015-04-20 14:52:42 +01001358 // Move instructions from `other` to `this`.
1359 DCHECK(EndsWithControlFlowInstruction());
1360 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001361 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001362 other->instructions_.SetBlockOfInstructions(this);
1363 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001364
David Brazdil2d7352b2015-04-20 14:52:42 +01001365 // Remove `other` from the loops it is included in.
1366 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1367 HLoopInformation* loop_info = it.Current();
1368 loop_info->Remove(other);
1369 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001370 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01001371 }
1372 }
1373
1374 // Update links to the successors of `other`.
1375 successors_.Reset();
1376 while (!other->successors_.IsEmpty()) {
1377 HBasicBlock* successor = other->successors_.Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001378 successor->ReplacePredecessor(other, this);
1379 }
1380
David Brazdil2d7352b2015-04-20 14:52:42 +01001381 // Update the dominator tree.
1382 dominated_blocks_.Delete(other);
1383 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1384 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1385 dominated_blocks_.Add(dominated);
1386 dominated->SetDominator(this);
1387 }
1388 other->dominated_blocks_.Reset();
1389 other->dominator_ = nullptr;
1390
1391 // Clear the list of predecessors of `other` in preparation of deleting it.
1392 other->predecessors_.Reset();
1393
1394 // Delete `other` from the graph. The function updates reverse post order.
1395 graph_->DeleteDeadBlock(other);
1396 other->SetGraph(nullptr);
1397}
1398
1399void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1400 DCHECK_NE(GetGraph(), other->GetGraph());
1401 DCHECK(GetDominatedBlocks().IsEmpty());
1402 DCHECK(GetSuccessors().IsEmpty());
1403 DCHECK(!EndsWithControlFlowInstruction());
1404 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1405 DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock());
1406 DCHECK(other->GetPhis().IsEmpty());
1407 DCHECK(!other->IsInLoop());
1408
1409 // Move instructions from `other` to `this`.
1410 instructions_.Add(other->GetInstructions());
1411 other->instructions_.SetBlockOfInstructions(this);
1412
1413 // Update links to the successors of `other`.
1414 successors_.Reset();
1415 while (!other->successors_.IsEmpty()) {
1416 HBasicBlock* successor = other->successors_.Get(0);
1417 successor->ReplacePredecessor(other, this);
1418 }
1419
1420 // Update the dominator tree.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001421 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1422 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1423 dominated_blocks_.Add(dominated);
1424 dominated->SetDominator(this);
1425 }
1426 other->dominated_blocks_.Reset();
1427 other->dominator_ = nullptr;
1428 other->graph_ = nullptr;
1429}
1430
1431void HBasicBlock::ReplaceWith(HBasicBlock* other) {
1432 while (!GetPredecessors().IsEmpty()) {
1433 HBasicBlock* predecessor = GetPredecessors().Get(0);
1434 predecessor->ReplaceSuccessor(this, other);
1435 }
1436 while (!GetSuccessors().IsEmpty()) {
1437 HBasicBlock* successor = GetSuccessors().Get(0);
1438 successor->ReplacePredecessor(this, other);
1439 }
1440 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
1441 other->AddDominatedBlock(dominated_blocks_.Get(i));
1442 }
1443 GetDominator()->ReplaceDominatedBlock(this, other);
1444 other->SetDominator(GetDominator());
1445 dominator_ = nullptr;
1446 graph_ = nullptr;
1447}
1448
1449// Create space in `blocks` for adding `number_of_new_blocks` entries
1450// starting at location `at`. Blocks after `at` are moved accordingly.
1451static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
1452 size_t number_of_new_blocks,
1453 size_t at) {
1454 size_t old_size = blocks->Size();
1455 size_t new_size = old_size + number_of_new_blocks;
1456 blocks->SetSize(new_size);
1457 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
1458 blocks->Put(j, blocks->Get(i));
1459 }
1460}
1461
David Brazdil2d7352b2015-04-20 14:52:42 +01001462void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1463 DCHECK_EQ(block->GetGraph(), this);
1464 DCHECK(block->GetSuccessors().IsEmpty());
1465 DCHECK(block->GetPredecessors().IsEmpty());
1466 DCHECK(block->GetDominatedBlocks().IsEmpty());
1467 DCHECK(block->GetDominator() == nullptr);
1468
1469 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1470 block->RemoveInstruction(it.Current());
1471 }
1472 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1473 block->RemovePhi(it.Current()->AsPhi());
1474 }
1475
David Brazdilc7af85d2015-05-26 12:05:55 +01001476 if (block->IsExitBlock()) {
1477 exit_block_ = nullptr;
1478 }
1479
David Brazdil2d7352b2015-04-20 14:52:42 +01001480 reverse_post_order_.Delete(block);
1481 blocks_.Put(block->GetBlockId(), nullptr);
1482}
1483
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001484void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
David Brazdilc7af85d2015-05-26 12:05:55 +01001485 DCHECK(HasExitBlock()) << "Unimplemented scenario";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001486 // Update the environments in this graph to have the invoke's environment
1487 // as parent.
1488 {
1489 HReversePostOrderIterator it(*this);
1490 it.Advance(); // Skip the entry block, we do not need to update the entry's suspend check.
1491 for (; !it.Done(); it.Advance()) {
1492 HBasicBlock* block = it.Current();
1493 for (HInstructionIterator instr_it(block->GetInstructions());
1494 !instr_it.Done();
1495 instr_it.Advance()) {
1496 HInstruction* current = instr_it.Current();
1497 if (current->NeedsEnvironment()) {
1498 current->GetEnvironment()->SetAndCopyParentChain(
1499 outer_graph->GetArena(), invoke->GetEnvironment());
1500 }
1501 }
1502 }
1503 }
1504 outer_graph->UpdateMaximumNumberOfOutVRegs(GetMaximumNumberOfOutVRegs());
1505 if (HasBoundsChecks()) {
1506 outer_graph->SetHasBoundsChecks(true);
1507 }
1508
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001509 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001510 // Simple case of an entry block, a body block, and an exit block.
1511 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001512 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001513 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
1514 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001515 DCHECK(!body->IsExitBlock());
1516 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001517
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001518 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1519 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001520
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001521 // Replace the invoke with the return value of the inlined graph.
1522 if (last->IsReturn()) {
1523 invoke->ReplaceWith(last->InputAt(0));
1524 } else {
1525 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001526 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001527
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001528 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001529 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001530 // Need to inline multiple blocks. We split `invoke`'s block
1531 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001532 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001533 // with the second half.
1534 ArenaAllocator* allocator = outer_graph->GetArena();
1535 HBasicBlock* at = invoke->GetBlock();
1536 HBasicBlock* to = at->SplitAfter(invoke);
1537
1538 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
1539 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001540 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001541 exit_block_->ReplaceWith(to);
1542
1543 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001544 // to not `HReturn` but `HGoto` instead.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001545 HInstruction* return_value = nullptr;
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001546 bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
1547 if (to->GetPredecessors().Size() == 1) {
1548 HBasicBlock* predecessor = to->GetPredecessors().Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001549 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001550 if (!returns_void) {
1551 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001552 }
1553 predecessor->AddInstruction(new (allocator) HGoto());
1554 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001555 } else {
1556 if (!returns_void) {
1557 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001558 return_value = new (allocator) HPhi(
1559 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001560 to->AddPhi(return_value->AsPhi());
1561 }
1562 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
1563 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
1564 HInstruction* last = predecessor->GetLastInstruction();
1565 if (!returns_void) {
1566 return_value->AsPhi()->AddInput(last->InputAt(0));
1567 }
1568 predecessor->AddInstruction(new (allocator) HGoto());
1569 predecessor->RemoveInstruction(last);
1570 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001571 }
1572
1573 if (return_value != nullptr) {
1574 invoke->ReplaceWith(return_value);
1575 }
1576
1577 // Update the meta information surrounding blocks:
1578 // (1) the graph they are now in,
1579 // (2) the reverse post order of that graph,
1580 // (3) the potential loop information they are now in.
1581
1582 // We don't add the entry block, the exit block, and the first block, which
1583 // has been merged with `at`.
1584 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1585
1586 // We add the `to` block.
1587 static constexpr int kNumberOfNewBlocksInCaller = 1;
1588 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
1589 + kNumberOfNewBlocksInCaller;
1590
1591 // Find the location of `at` in the outer graph's reverse post order. The new
1592 // blocks will be added after it.
1593 size_t index_of_at = 0;
1594 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
1595 index_of_at++;
1596 }
1597 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1598
1599 // Do a reverse post order of the blocks in the callee and do (1), (2),
1600 // and (3) to the blocks that apply.
1601 HLoopInformation* info = at->GetLoopInformation();
1602 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1603 HBasicBlock* current = it.Current();
1604 if (current != exit_block_ && current != entry_block_ && current != first) {
1605 DCHECK(!current->IsInLoop());
1606 DCHECK(current->GetGraph() == this);
1607 current->SetGraph(outer_graph);
1608 outer_graph->AddBlock(current);
1609 outer_graph->reverse_post_order_.Put(++index_of_at, current);
1610 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001611 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001612 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1613 loop_it.Current()->Add(current);
1614 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001615 }
1616 }
1617 }
1618
1619 // Do (1), (2), and (3) to `to`.
1620 to->SetGraph(outer_graph);
1621 outer_graph->AddBlock(to);
1622 outer_graph->reverse_post_order_.Put(++index_of_at, to);
1623 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001624 to->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001625 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1626 loop_it.Current()->Add(to);
1627 }
David Brazdil46e2a392015-03-16 17:31:52 +00001628 if (info->IsBackEdge(*at)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001629 // Only `to` can become a back edge, as the inlined blocks
1630 // are predecessors of `to`.
1631 info->ReplaceBackEdge(at, to);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001632 }
1633 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001634 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001635
David Brazdil05144f42015-04-16 15:18:00 +01001636 // Update the next instruction id of the outer graph, so that instructions
1637 // added later get bigger ids than those in the inner graph.
1638 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1639
1640 // Walk over the entry block and:
1641 // - Move constants from the entry block to the outer_graph's entry block,
1642 // - Replace HParameterValue instructions with their real value.
1643 // - Remove suspend checks, that hold an environment.
1644 // We must do this after the other blocks have been inlined, otherwise ids of
1645 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001646 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001647 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1648 HInstruction* current = it.Current();
1649 if (current->IsNullConstant()) {
1650 current->ReplaceWith(outer_graph->GetNullConstant());
1651 } else if (current->IsIntConstant()) {
1652 current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue()));
1653 } else if (current->IsLongConstant()) {
1654 current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue()));
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001655 } else if (current->IsFloatConstant()) {
1656 current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue()));
1657 } else if (current->IsDoubleConstant()) {
1658 current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue()));
David Brazdil05144f42015-04-16 15:18:00 +01001659 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001660 if (kIsDebugBuild
1661 && invoke->IsInvokeStaticOrDirect()
1662 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1663 // Ensure we do not use the last input of `invoke`, as it
1664 // contains a clinit check which is not an actual argument.
1665 size_t last_input_index = invoke->InputCount() - 1;
1666 DCHECK(parameter_index != last_input_index);
1667 }
David Brazdil05144f42015-04-16 15:18:00 +01001668 current->ReplaceWith(invoke->InputAt(parameter_index++));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001669 } else if (current->IsCurrentMethod()) {
1670 current->ReplaceWith(outer_graph->GetCurrentMethod());
David Brazdil05144f42015-04-16 15:18:00 +01001671 } else {
1672 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1673 entry_block_->RemoveInstruction(current);
1674 }
1675 }
1676
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001677 // Finally remove the invoke from the caller.
1678 invoke->GetBlock()->RemoveInstruction(invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001679}
1680
Mingyao Yang3584bce2015-05-19 16:01:59 -07001681/*
1682 * Loop will be transformed to:
1683 * old_pre_header
1684 * |
1685 * if_block
1686 * / \
1687 * dummy_block deopt_block
1688 * \ /
1689 * new_pre_header
1690 * |
1691 * header
1692 */
1693void HGraph::TransformLoopHeaderForBCE(HBasicBlock* header) {
1694 DCHECK(header->IsLoopHeader());
1695 HBasicBlock* pre_header = header->GetDominator();
1696
1697 // Need this to avoid critical edge.
1698 HBasicBlock* if_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1699 // Need this to avoid critical edge.
1700 HBasicBlock* dummy_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1701 HBasicBlock* deopt_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1702 HBasicBlock* new_pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
1703 AddBlock(if_block);
1704 AddBlock(dummy_block);
1705 AddBlock(deopt_block);
1706 AddBlock(new_pre_header);
1707
1708 header->ReplacePredecessor(pre_header, new_pre_header);
1709 pre_header->successors_.Reset();
1710 pre_header->dominated_blocks_.Reset();
1711
1712 pre_header->AddSuccessor(if_block);
1713 if_block->AddSuccessor(dummy_block); // True successor
1714 if_block->AddSuccessor(deopt_block); // False successor
1715 dummy_block->AddSuccessor(new_pre_header);
1716 deopt_block->AddSuccessor(new_pre_header);
1717
1718 pre_header->dominated_blocks_.Add(if_block);
1719 if_block->SetDominator(pre_header);
1720 if_block->dominated_blocks_.Add(dummy_block);
1721 dummy_block->SetDominator(if_block);
1722 if_block->dominated_blocks_.Add(deopt_block);
1723 deopt_block->SetDominator(if_block);
1724 if_block->dominated_blocks_.Add(new_pre_header);
1725 new_pre_header->SetDominator(if_block);
1726 new_pre_header->dominated_blocks_.Add(header);
1727 header->SetDominator(new_pre_header);
1728
1729 size_t index_of_header = 0;
1730 while (reverse_post_order_.Get(index_of_header) != header) {
1731 index_of_header++;
1732 }
1733 MakeRoomFor(&reverse_post_order_, 4, index_of_header - 1);
1734 reverse_post_order_.Put(index_of_header++, if_block);
1735 reverse_post_order_.Put(index_of_header++, dummy_block);
1736 reverse_post_order_.Put(index_of_header++, deopt_block);
1737 reverse_post_order_.Put(index_of_header++, new_pre_header);
1738
1739 HLoopInformation* info = pre_header->GetLoopInformation();
1740 if (info != nullptr) {
1741 if_block->SetLoopInformation(info);
1742 dummy_block->SetLoopInformation(info);
1743 deopt_block->SetLoopInformation(info);
1744 new_pre_header->SetLoopInformation(info);
1745 for (HLoopInformationOutwardIterator loop_it(*pre_header);
1746 !loop_it.Done();
1747 loop_it.Advance()) {
1748 loop_it.Current()->Add(if_block);
1749 loop_it.Current()->Add(dummy_block);
1750 loop_it.Current()->Add(deopt_block);
1751 loop_it.Current()->Add(new_pre_header);
1752 }
1753 }
1754}
1755
Calin Juravle3fabec72015-07-16 16:51:30 +01001756void HInstruction::SetReferenceTypeInfo(ReferenceTypeInfo rti) {
1757 if (kIsDebugBuild) {
1758 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1759 ScopedObjectAccess soa(Thread::Current());
1760 DCHECK(rti.IsValid()) << "Invalid RTI for " << DebugName();
1761 if (IsBoundType()) {
1762 // Having the test here spares us from making the method virtual just for
1763 // the sake of a DCHECK.
1764 ReferenceTypeInfo upper_bound_rti = AsBoundType()->GetUpperBound();
1765 DCHECK(upper_bound_rti.IsSupertypeOf(rti))
1766 << " upper_bound_rti: " << upper_bound_rti
1767 << " rti: " << rti;
1768 DCHECK(!upper_bound_rti.GetTypeHandle()->IsFinal() || rti.IsExact());
1769 }
1770 }
1771 reference_type_info_ = rti;
1772}
1773
1774ReferenceTypeInfo::ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
1775
1776ReferenceTypeInfo::ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
1777 : type_handle_(type_handle), is_exact_(is_exact) {
1778 if (kIsDebugBuild) {
1779 ScopedObjectAccess soa(Thread::Current());
1780 DCHECK(IsValidHandle(type_handle));
1781 }
1782}
1783
Calin Juravleacf735c2015-02-12 15:25:22 +00001784std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1785 ScopedObjectAccess soa(Thread::Current());
1786 os << "["
Calin Juravle3fabec72015-07-16 16:51:30 +01001787 << " is_valid=" << rhs.IsValid()
1788 << " type=" << (!rhs.IsValid() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
Calin Juravleacf735c2015-02-12 15:25:22 +00001789 << " is_exact=" << rhs.IsExact()
1790 << " ]";
1791 return os;
1792}
1793
Mark Mendellc4701932015-04-10 13:18:51 -04001794bool HInstruction::HasAnyEnvironmentUseBefore(HInstruction* other) {
1795 // For now, assume that instructions in different blocks may use the
1796 // environment.
1797 // TODO: Use the control flow to decide if this is true.
1798 if (GetBlock() != other->GetBlock()) {
1799 return true;
1800 }
1801
1802 // We know that we are in the same block. Walk from 'this' to 'other',
1803 // checking to see if there is any instruction with an environment.
1804 HInstruction* current = this;
1805 for (; current != other && current != nullptr; current = current->GetNext()) {
1806 // This is a conservative check, as the instruction result may not be in
1807 // the referenced environment.
1808 if (current->HasEnvironment()) {
1809 return true;
1810 }
1811 }
1812
1813 // We should have been called with 'this' before 'other' in the block.
1814 // Just confirm this.
1815 DCHECK(current != nullptr);
1816 return false;
1817}
1818
1819void HInstruction::RemoveEnvironmentUsers() {
1820 for (HUseIterator<HEnvironment*> use_it(GetEnvUses()); !use_it.Done(); use_it.Advance()) {
1821 HUseListNode<HEnvironment*>* user_node = use_it.Current();
1822 HEnvironment* user = user_node->GetUser();
1823 user->SetRawEnvAt(user_node->GetIndex(), nullptr);
1824 }
1825 env_uses_.Clear();
1826}
1827
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001828} // namespace art