blob: 64c680c3fb291ad69af622e3c8f8d04857a9533d [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));
David Brazdilec16f792015-08-19 15:04:01 +0100342 const HTryBoundary* try_entry = first_predecessor->ComputeTryEntryOfSuccessors();
343 if (try_entry != nullptr) {
344 block->SetTryCatchInformation(new (arena_) TryCatchInformation(*try_entry));
345 }
David Brazdilffee3d32015-07-06 11:48:53 +0100346 }
347}
348
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100349void HGraph::SimplifyCFG() {
350 // Simplify the CFG for future analysis, and code generation:
351 // (1): Split critical edges.
352 // (2): Simplify loops by having only one back edge, and one preheader.
353 for (size_t i = 0; i < blocks_.Size(); ++i) {
354 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100355 if (block == nullptr) continue;
David Brazdilffee3d32015-07-06 11:48:53 +0100356 if (block->NumberOfNormalSuccessors() > 1) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100357 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
358 HBasicBlock* successor = block->GetSuccessors().Get(j);
David Brazdilffee3d32015-07-06 11:48:53 +0100359 DCHECK(!successor->IsCatchBlock());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100360 if (successor->GetPredecessors().Size() > 1) {
361 SplitCriticalEdge(block, successor);
362 --j;
363 }
364 }
365 }
366 if (block->IsLoopHeader()) {
367 SimplifyLoop(block);
368 }
369 }
370}
371
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000372bool HGraph::AnalyzeNaturalLoops() const {
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100373 // Order does not matter.
374 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
375 HBasicBlock* block = it.Current();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100376 if (block->IsLoopHeader()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100377 if (block->IsCatchBlock()) {
378 // TODO: Dealing with exceptional back edges could be tricky because
379 // they only approximate the real control flow. Bail out for now.
380 return false;
381 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100382 HLoopInformation* info = block->GetLoopInformation();
383 if (!info->Populate()) {
384 // Abort if the loop is non natural. We currently bailout in such cases.
385 return false;
386 }
387 }
388 }
389 return true;
390}
391
David Brazdil8d5b8b22015-03-24 10:51:52 +0000392void HGraph::InsertConstant(HConstant* constant) {
393 // New constants are inserted before the final control-flow instruction
394 // of the graph, or at its end if called from the graph builder.
395 if (entry_block_->EndsWithControlFlowInstruction()) {
396 entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction());
David Brazdil46e2a392015-03-16 17:31:52 +0000397 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000398 entry_block_->AddInstruction(constant);
David Brazdil46e2a392015-03-16 17:31:52 +0000399 }
400}
401
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000402HNullConstant* HGraph::GetNullConstant() {
Nicolas Geoffray18e68732015-06-17 23:09:05 +0100403 // For simplicity, don't bother reviving the cached null constant if it is
404 // not null and not in a block. Otherwise, we need to clear the instruction
405 // id and/or any invariants the graph is assuming when adding new instructions.
406 if ((cached_null_constant_ == nullptr) || (cached_null_constant_->GetBlock() == nullptr)) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000407 cached_null_constant_ = new (arena_) HNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000408 InsertConstant(cached_null_constant_);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000409 }
410 return cached_null_constant_;
411}
412
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100413HCurrentMethod* HGraph::GetCurrentMethod() {
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100414 // For simplicity, don't bother reviving the cached current method if it is
415 // not null and not in a block. Otherwise, we need to clear the instruction
416 // id and/or any invariants the graph is assuming when adding new instructions.
417 if ((cached_current_method_ == nullptr) || (cached_current_method_->GetBlock() == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700418 cached_current_method_ = new (arena_) HCurrentMethod(
419 Is64BitInstructionSet(instruction_set_) ? Primitive::kPrimLong : Primitive::kPrimInt);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100420 if (entry_block_->GetFirstInstruction() == nullptr) {
421 entry_block_->AddInstruction(cached_current_method_);
422 } else {
423 entry_block_->InsertInstructionBefore(
424 cached_current_method_, entry_block_->GetFirstInstruction());
425 }
426 }
427 return cached_current_method_;
428}
429
David Brazdil8d5b8b22015-03-24 10:51:52 +0000430HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value) {
431 switch (type) {
432 case Primitive::Type::kPrimBoolean:
433 DCHECK(IsUint<1>(value));
434 FALLTHROUGH_INTENDED;
435 case Primitive::Type::kPrimByte:
436 case Primitive::Type::kPrimChar:
437 case Primitive::Type::kPrimShort:
438 case Primitive::Type::kPrimInt:
439 DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value));
440 return GetIntConstant(static_cast<int32_t>(value));
441
442 case Primitive::Type::kPrimLong:
443 return GetLongConstant(value);
444
445 default:
446 LOG(FATAL) << "Unsupported constant type";
447 UNREACHABLE();
David Brazdil46e2a392015-03-16 17:31:52 +0000448 }
David Brazdil46e2a392015-03-16 17:31:52 +0000449}
450
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000451void HGraph::CacheFloatConstant(HFloatConstant* constant) {
452 int32_t value = bit_cast<int32_t, float>(constant->GetValue());
453 DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end());
454 cached_float_constants_.Overwrite(value, constant);
455}
456
457void HGraph::CacheDoubleConstant(HDoubleConstant* constant) {
458 int64_t value = bit_cast<int64_t, double>(constant->GetValue());
459 DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end());
460 cached_double_constants_.Overwrite(value, constant);
461}
462
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000463void HLoopInformation::Add(HBasicBlock* block) {
464 blocks_.SetBit(block->GetBlockId());
465}
466
David Brazdil46e2a392015-03-16 17:31:52 +0000467void HLoopInformation::Remove(HBasicBlock* block) {
468 blocks_.ClearBit(block->GetBlockId());
469}
470
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100471void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
472 if (blocks_.IsBitSet(block->GetBlockId())) {
473 return;
474 }
475
476 blocks_.SetBit(block->GetBlockId());
477 block->SetInLoop(this);
478 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
479 PopulateRecursive(block->GetPredecessors().Get(i));
480 }
481}
482
483bool HLoopInformation::Populate() {
David Brazdila4b8c212015-05-07 09:59:30 +0100484 DCHECK_EQ(blocks_.NumSetBits(), 0u) << "Loop information has already been populated";
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100485 for (size_t i = 0, e = GetBackEdges().Size(); i < e; ++i) {
486 HBasicBlock* back_edge = GetBackEdges().Get(i);
487 DCHECK(back_edge->GetDominator() != nullptr);
488 if (!header_->Dominates(back_edge)) {
489 // This loop is not natural. Do not bother going further.
490 return false;
491 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100492
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100493 // Populate this loop: starting with the back edge, recursively add predecessors
494 // that are not already part of that loop. Set the header as part of the loop
495 // to end the recursion.
496 // This is a recursive implementation of the algorithm described in
497 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
498 blocks_.SetBit(header_->GetBlockId());
499 PopulateRecursive(back_edge);
500 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100501 return true;
502}
503
David Brazdila4b8c212015-05-07 09:59:30 +0100504void HLoopInformation::Update() {
505 HGraph* graph = header_->GetGraph();
506 for (uint32_t id : blocks_.Indexes()) {
507 HBasicBlock* block = graph->GetBlocks().Get(id);
508 // Reset loop information of non-header blocks inside the loop, except
509 // members of inner nested loops because those should already have been
510 // updated by their own LoopInformation.
511 if (block->GetLoopInformation() == this && block != header_) {
512 block->SetLoopInformation(nullptr);
513 }
514 }
515 blocks_.ClearAllBits();
516
517 if (back_edges_.IsEmpty()) {
518 // The loop has been dismantled, delete its suspend check and remove info
519 // from the header.
520 DCHECK(HasSuspendCheck());
521 header_->RemoveInstruction(suspend_check_);
522 header_->SetLoopInformation(nullptr);
523 header_ = nullptr;
524 suspend_check_ = nullptr;
525 } else {
526 if (kIsDebugBuild) {
527 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
528 DCHECK(header_->Dominates(back_edges_.Get(i)));
529 }
530 }
531 // This loop still has reachable back edges. Repopulate the list of blocks.
532 bool populate_successful = Populate();
533 DCHECK(populate_successful);
534 }
535}
536
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100537HBasicBlock* HLoopInformation::GetPreHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100538 return header_->GetDominator();
539}
540
541bool HLoopInformation::Contains(const HBasicBlock& block) const {
542 return blocks_.IsBitSet(block.GetBlockId());
543}
544
545bool HLoopInformation::IsIn(const HLoopInformation& other) const {
546 return other.blocks_.IsBitSet(header_->GetBlockId());
547}
548
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100549size_t HLoopInformation::GetLifetimeEnd() const {
550 size_t last_position = 0;
551 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
552 last_position = std::max(back_edges_.Get(i)->GetLifetimeEnd(), last_position);
553 }
554 return last_position;
555}
556
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100557bool HBasicBlock::Dominates(HBasicBlock* other) const {
558 // Walk up the dominator tree from `other`, to find out if `this`
559 // is an ancestor.
560 HBasicBlock* current = other;
561 while (current != nullptr) {
562 if (current == this) {
563 return true;
564 }
565 current = current->GetDominator();
566 }
567 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100568}
569
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100570static void UpdateInputsUsers(HInstruction* instruction) {
571 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
572 instruction->InputAt(i)->AddUseAt(instruction, i);
573 }
574 // Environment should be created later.
575 DCHECK(!instruction->HasEnvironment());
576}
577
Roland Levillainccc07a92014-09-16 14:48:16 +0100578void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
579 HInstruction* replacement) {
580 DCHECK(initial->GetBlock() == this);
581 InsertInstructionBefore(replacement, initial);
582 initial->ReplaceWith(replacement);
583 RemoveInstruction(initial);
584}
585
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100586static void Add(HInstructionList* instruction_list,
587 HBasicBlock* block,
588 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000589 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000590 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100591 instruction->SetBlock(block);
592 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100593 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100594 instruction_list->AddInstruction(instruction);
595}
596
597void HBasicBlock::AddInstruction(HInstruction* instruction) {
598 Add(&instructions_, this, instruction);
599}
600
601void HBasicBlock::AddPhi(HPhi* phi) {
602 Add(&phis_, this, phi);
603}
604
David Brazdilc3d743f2015-04-22 13:40:50 +0100605void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
606 DCHECK(!cursor->IsPhi());
607 DCHECK(!instruction->IsPhi());
608 DCHECK_EQ(instruction->GetId(), -1);
609 DCHECK_NE(cursor->GetId(), -1);
610 DCHECK_EQ(cursor->GetBlock(), this);
611 DCHECK(!instruction->IsControlFlow());
612 instruction->SetBlock(this);
613 instruction->SetId(GetGraph()->GetNextInstructionId());
614 UpdateInputsUsers(instruction);
615 instructions_.InsertInstructionBefore(instruction, cursor);
616}
617
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100618void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
619 DCHECK(!cursor->IsPhi());
620 DCHECK(!instruction->IsPhi());
621 DCHECK_EQ(instruction->GetId(), -1);
622 DCHECK_NE(cursor->GetId(), -1);
623 DCHECK_EQ(cursor->GetBlock(), this);
624 DCHECK(!instruction->IsControlFlow());
625 DCHECK(!cursor->IsControlFlow());
626 instruction->SetBlock(this);
627 instruction->SetId(GetGraph()->GetNextInstructionId());
628 UpdateInputsUsers(instruction);
629 instructions_.InsertInstructionAfter(instruction, cursor);
630}
631
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100632void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
633 DCHECK_EQ(phi->GetId(), -1);
634 DCHECK_NE(cursor->GetId(), -1);
635 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100636 phi->SetBlock(this);
637 phi->SetId(GetGraph()->GetNextInstructionId());
638 UpdateInputsUsers(phi);
David Brazdilc3d743f2015-04-22 13:40:50 +0100639 phis_.InsertInstructionAfter(phi, cursor);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100640}
641
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100642static void Remove(HInstructionList* instruction_list,
643 HBasicBlock* block,
David Brazdil1abb4192015-02-17 18:33:36 +0000644 HInstruction* instruction,
645 bool ensure_safety) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100646 DCHECK_EQ(block, instruction->GetBlock());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100647 instruction->SetBlock(nullptr);
648 instruction_list->RemoveInstruction(instruction);
David Brazdil1abb4192015-02-17 18:33:36 +0000649 if (ensure_safety) {
650 DCHECK(instruction->GetUses().IsEmpty());
651 DCHECK(instruction->GetEnvUses().IsEmpty());
652 RemoveAsUser(instruction);
653 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100654}
655
David Brazdil1abb4192015-02-17 18:33:36 +0000656void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) {
David Brazdilc7508e92015-04-27 13:28:57 +0100657 DCHECK(!instruction->IsPhi());
David Brazdil1abb4192015-02-17 18:33:36 +0000658 Remove(&instructions_, this, instruction, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100659}
660
David Brazdil1abb4192015-02-17 18:33:36 +0000661void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) {
662 Remove(&phis_, this, phi, ensure_safety);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100663}
664
David Brazdilc7508e92015-04-27 13:28:57 +0100665void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) {
666 if (instruction->IsPhi()) {
667 RemovePhi(instruction->AsPhi(), ensure_safety);
668 } else {
669 RemoveInstruction(instruction, ensure_safety);
670 }
671}
672
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +0100673void HEnvironment::CopyFrom(const GrowableArray<HInstruction*>& locals) {
674 for (size_t i = 0; i < locals.Size(); i++) {
675 HInstruction* instruction = locals.Get(i);
676 SetRawEnvAt(i, instruction);
677 if (instruction != nullptr) {
678 instruction->AddEnvUseAt(this, i);
679 }
680 }
681}
682
David Brazdiled596192015-01-23 10:39:45 +0000683void HEnvironment::CopyFrom(HEnvironment* env) {
684 for (size_t i = 0; i < env->Size(); i++) {
685 HInstruction* instruction = env->GetInstructionAt(i);
686 SetRawEnvAt(i, instruction);
687 if (instruction != nullptr) {
688 instruction->AddEnvUseAt(this, i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100689 }
David Brazdiled596192015-01-23 10:39:45 +0000690 }
691}
692
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700693void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env,
694 HBasicBlock* loop_header) {
695 DCHECK(loop_header->IsLoopHeader());
696 for (size_t i = 0; i < env->Size(); i++) {
697 HInstruction* instruction = env->GetInstructionAt(i);
698 SetRawEnvAt(i, instruction);
699 if (instruction == nullptr) {
700 continue;
701 }
702 if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) {
703 // At the end of the loop pre-header, the corresponding value for instruction
704 // is the first input of the phi.
705 HInstruction* initial = instruction->AsPhi()->InputAt(0);
706 DCHECK(initial->GetBlock()->Dominates(loop_header));
707 SetRawEnvAt(i, initial);
708 initial->AddEnvUseAt(this, i);
709 } else {
710 instruction->AddEnvUseAt(this, i);
711 }
712 }
713}
714
David Brazdil1abb4192015-02-17 18:33:36 +0000715void HEnvironment::RemoveAsUserOfInput(size_t index) const {
716 const HUserRecord<HEnvironment*> user_record = vregs_.Get(index);
717 user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100718}
719
Calin Juravle77520bc2015-01-12 18:45:46 +0000720HInstruction* HInstruction::GetNextDisregardingMoves() const {
721 HInstruction* next = GetNext();
722 while (next != nullptr && next->IsParallelMove()) {
723 next = next->GetNext();
724 }
725 return next;
726}
727
728HInstruction* HInstruction::GetPreviousDisregardingMoves() const {
729 HInstruction* previous = GetPrevious();
730 while (previous != nullptr && previous->IsParallelMove()) {
731 previous = previous->GetPrevious();
732 }
733 return previous;
734}
735
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100736void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000737 if (first_instruction_ == nullptr) {
738 DCHECK(last_instruction_ == nullptr);
739 first_instruction_ = last_instruction_ = instruction;
740 } else {
741 last_instruction_->next_ = instruction;
742 instruction->previous_ = last_instruction_;
743 last_instruction_ = instruction;
744 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000745}
746
David Brazdilc3d743f2015-04-22 13:40:50 +0100747void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
748 DCHECK(Contains(cursor));
749 if (cursor == first_instruction_) {
750 cursor->previous_ = instruction;
751 instruction->next_ = cursor;
752 first_instruction_ = instruction;
753 } else {
754 instruction->previous_ = cursor->previous_;
755 instruction->next_ = cursor;
756 cursor->previous_ = instruction;
757 instruction->previous_->next_ = instruction;
758 }
759}
760
761void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) {
762 DCHECK(Contains(cursor));
763 if (cursor == last_instruction_) {
764 cursor->next_ = instruction;
765 instruction->previous_ = cursor;
766 last_instruction_ = instruction;
767 } else {
768 instruction->next_ = cursor->next_;
769 instruction->previous_ = cursor;
770 cursor->next_ = instruction;
771 instruction->next_->previous_ = instruction;
772 }
773}
774
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100775void HInstructionList::RemoveInstruction(HInstruction* instruction) {
776 if (instruction->previous_ != nullptr) {
777 instruction->previous_->next_ = instruction->next_;
778 }
779 if (instruction->next_ != nullptr) {
780 instruction->next_->previous_ = instruction->previous_;
781 }
782 if (instruction == first_instruction_) {
783 first_instruction_ = instruction->next_;
784 }
785 if (instruction == last_instruction_) {
786 last_instruction_ = instruction->previous_;
787 }
788}
789
Roland Levillain6b469232014-09-25 10:10:38 +0100790bool HInstructionList::Contains(HInstruction* instruction) const {
791 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
792 if (it.Current() == instruction) {
793 return true;
794 }
795 }
796 return false;
797}
798
Roland Levillainccc07a92014-09-16 14:48:16 +0100799bool HInstructionList::FoundBefore(const HInstruction* instruction1,
800 const HInstruction* instruction2) const {
801 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
802 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
803 if (it.Current() == instruction1) {
804 return true;
805 }
806 if (it.Current() == instruction2) {
807 return false;
808 }
809 }
810 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
811 return true;
812}
813
Roland Levillain6c82d402014-10-13 16:10:27 +0100814bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
815 if (other_instruction == this) {
816 // An instruction does not strictly dominate itself.
817 return false;
818 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100819 HBasicBlock* block = GetBlock();
820 HBasicBlock* other_block = other_instruction->GetBlock();
821 if (block != other_block) {
822 return GetBlock()->Dominates(other_instruction->GetBlock());
823 } else {
824 // If both instructions are in the same block, ensure this
825 // instruction comes before `other_instruction`.
826 if (IsPhi()) {
827 if (!other_instruction->IsPhi()) {
828 // Phis appear before non phi-instructions so this instruction
829 // dominates `other_instruction`.
830 return true;
831 } else {
832 // There is no order among phis.
833 LOG(FATAL) << "There is no dominance between phis of a same block.";
834 return false;
835 }
836 } else {
837 // `this` is not a phi.
838 if (other_instruction->IsPhi()) {
839 // Phis appear before non phi-instructions so this instruction
840 // does not dominate `other_instruction`.
841 return false;
842 } else {
843 // Check whether this instruction comes before
844 // `other_instruction` in the instruction list.
845 return block->GetInstructions().FoundBefore(this, other_instruction);
846 }
847 }
848 }
849}
850
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100851void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100852 DCHECK(other != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000853 for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) {
854 HUseListNode<HInstruction*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100855 HInstruction* user = current->GetUser();
856 size_t input_index = current->GetIndex();
857 user->SetRawInputAt(input_index, other);
858 other->AddUseAt(user, input_index);
859 }
860
David Brazdiled596192015-01-23 10:39:45 +0000861 for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) {
862 HUseListNode<HEnvironment*>* current = it.Current();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100863 HEnvironment* user = current->GetUser();
864 size_t input_index = current->GetIndex();
865 user->SetRawEnvAt(input_index, other);
866 other->AddEnvUseAt(user, input_index);
867 }
868
David Brazdiled596192015-01-23 10:39:45 +0000869 uses_.Clear();
870 env_uses_.Clear();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100871}
872
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100873void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +0000874 RemoveAsUserOfInput(index);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100875 SetRawInputAt(index, replacement);
876 replacement->AddUseAt(this, index);
877}
878
Nicolas Geoffray39468442014-09-02 15:17:15 +0100879size_t HInstruction::EnvironmentSize() const {
880 return HasEnvironment() ? environment_->Size() : 0;
881}
882
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100883void HPhi::AddInput(HInstruction* input) {
884 DCHECK(input->GetBlock() != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +0000885 inputs_.Add(HUserRecord<HInstruction*>(input));
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100886 input->AddUseAt(this, inputs_.Size() - 1);
887}
888
David Brazdil2d7352b2015-04-20 14:52:42 +0100889void HPhi::RemoveInputAt(size_t index) {
890 RemoveAsUserOfInput(index);
891 inputs_.DeleteAt(index);
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100892 for (size_t i = index, e = InputCount(); i < e; ++i) {
893 InputRecordAt(i).GetUseNode()->SetIndex(i);
894 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100895}
896
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100897#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000898void H##name::Accept(HGraphVisitor* visitor) { \
899 visitor->Visit##name(this); \
900}
901
902FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
903
904#undef DEFINE_ACCEPT
905
906void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100907 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
908 for (size_t i = 0 ; i < blocks.Size(); i++) {
David Brazdil46e2a392015-03-16 17:31:52 +0000909 HBasicBlock* block = blocks.Get(i);
910 if (block != nullptr) {
911 VisitBasicBlock(block);
912 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000913 }
914}
915
Roland Levillain633021e2014-10-01 14:12:25 +0100916void HGraphVisitor::VisitReversePostOrder() {
917 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
918 VisitBasicBlock(it.Current());
919 }
920}
921
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000922void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100923 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100924 it.Current()->Accept(this);
925 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100926 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927 it.Current()->Accept(this);
928 }
929}
930
Mark Mendelle82549b2015-05-06 10:55:34 -0400931HConstant* HTypeConversion::TryStaticEvaluation() const {
932 HGraph* graph = GetBlock()->GetGraph();
933 if (GetInput()->IsIntConstant()) {
934 int32_t value = GetInput()->AsIntConstant()->GetValue();
935 switch (GetResultType()) {
936 case Primitive::kPrimLong:
937 return graph->GetLongConstant(static_cast<int64_t>(value));
938 case Primitive::kPrimFloat:
939 return graph->GetFloatConstant(static_cast<float>(value));
940 case Primitive::kPrimDouble:
941 return graph->GetDoubleConstant(static_cast<double>(value));
942 default:
943 return nullptr;
944 }
945 } else if (GetInput()->IsLongConstant()) {
946 int64_t value = GetInput()->AsLongConstant()->GetValue();
947 switch (GetResultType()) {
948 case Primitive::kPrimInt:
949 return graph->GetIntConstant(static_cast<int32_t>(value));
950 case Primitive::kPrimFloat:
951 return graph->GetFloatConstant(static_cast<float>(value));
952 case Primitive::kPrimDouble:
953 return graph->GetDoubleConstant(static_cast<double>(value));
954 default:
955 return nullptr;
956 }
957 } else if (GetInput()->IsFloatConstant()) {
958 float value = GetInput()->AsFloatConstant()->GetValue();
959 switch (GetResultType()) {
960 case Primitive::kPrimInt:
961 if (std::isnan(value))
962 return graph->GetIntConstant(0);
963 if (value >= kPrimIntMax)
964 return graph->GetIntConstant(kPrimIntMax);
965 if (value <= kPrimIntMin)
966 return graph->GetIntConstant(kPrimIntMin);
967 return graph->GetIntConstant(static_cast<int32_t>(value));
968 case Primitive::kPrimLong:
969 if (std::isnan(value))
970 return graph->GetLongConstant(0);
971 if (value >= kPrimLongMax)
972 return graph->GetLongConstant(kPrimLongMax);
973 if (value <= kPrimLongMin)
974 return graph->GetLongConstant(kPrimLongMin);
975 return graph->GetLongConstant(static_cast<int64_t>(value));
976 case Primitive::kPrimDouble:
977 return graph->GetDoubleConstant(static_cast<double>(value));
978 default:
979 return nullptr;
980 }
981 } else if (GetInput()->IsDoubleConstant()) {
982 double value = GetInput()->AsDoubleConstant()->GetValue();
983 switch (GetResultType()) {
984 case Primitive::kPrimInt:
985 if (std::isnan(value))
986 return graph->GetIntConstant(0);
987 if (value >= kPrimIntMax)
988 return graph->GetIntConstant(kPrimIntMax);
989 if (value <= kPrimLongMin)
990 return graph->GetIntConstant(kPrimIntMin);
991 return graph->GetIntConstant(static_cast<int32_t>(value));
992 case Primitive::kPrimLong:
993 if (std::isnan(value))
994 return graph->GetLongConstant(0);
995 if (value >= kPrimLongMax)
996 return graph->GetLongConstant(kPrimLongMax);
997 if (value <= kPrimLongMin)
998 return graph->GetLongConstant(kPrimLongMin);
999 return graph->GetLongConstant(static_cast<int64_t>(value));
1000 case Primitive::kPrimFloat:
1001 return graph->GetFloatConstant(static_cast<float>(value));
1002 default:
1003 return nullptr;
1004 }
1005 }
1006 return nullptr;
1007}
1008
Roland Levillain9240d6a2014-10-20 16:47:04 +01001009HConstant* HUnaryOperation::TryStaticEvaluation() const {
1010 if (GetInput()->IsIntConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001011 return Evaluate(GetInput()->AsIntConstant());
Roland Levillain9240d6a2014-10-20 16:47:04 +01001012 } else if (GetInput()->IsLongConstant()) {
Roland Levillain9867bc72015-08-05 10:21:34 +01001013 return Evaluate(GetInput()->AsLongConstant());
Roland Levillain9240d6a2014-10-20 16:47:04 +01001014 }
1015 return nullptr;
1016}
1017
1018HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain9867bc72015-08-05 10:21:34 +01001019 if (GetLeft()->IsIntConstant()) {
1020 if (GetRight()->IsIntConstant()) {
1021 return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsIntConstant());
1022 } else if (GetRight()->IsLongConstant()) {
1023 return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsLongConstant());
1024 }
1025 } else if (GetLeft()->IsLongConstant()) {
1026 if (GetRight()->IsIntConstant()) {
1027 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsIntConstant());
1028 } else if (GetRight()->IsLongConstant()) {
1029 return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsLongConstant());
Nicolas Geoffray9ee66182015-01-16 12:35:40 +00001030 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001031 }
1032 return nullptr;
1033}
Dave Allison20dfc792014-06-16 20:44:29 -07001034
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001035HConstant* HBinaryOperation::GetConstantRight() const {
1036 if (GetRight()->IsConstant()) {
1037 return GetRight()->AsConstant();
1038 } else if (IsCommutative() && GetLeft()->IsConstant()) {
1039 return GetLeft()->AsConstant();
1040 } else {
1041 return nullptr;
1042 }
1043}
1044
1045// If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001046// one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001047HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
1048 HInstruction* most_constant_right = GetConstantRight();
1049 if (most_constant_right == nullptr) {
1050 return nullptr;
1051 } else if (most_constant_right == GetLeft()) {
1052 return GetRight();
1053 } else {
1054 return GetLeft();
1055 }
1056}
1057
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001058bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const {
1059 return this == instruction->GetPreviousDisregardingMoves();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001060}
1061
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001062bool HInstruction::Equals(HInstruction* other) const {
1063 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001064 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001065 if (!InstructionDataEquals(other)) return false;
1066 if (GetType() != other->GetType()) return false;
1067 if (InputCount() != other->InputCount()) return false;
1068
1069 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1070 if (InputAt(i) != other->InputAt(i)) return false;
1071 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001072 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001073 return true;
1074}
1075
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001076std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
1077#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
1078 switch (rhs) {
1079 FOR_EACH_INSTRUCTION(DECLARE_CASE)
1080 default:
1081 os << "Unknown instruction kind " << static_cast<int>(rhs);
1082 break;
1083 }
1084#undef DECLARE_CASE
1085 return os;
1086}
1087
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001088void HInstruction::MoveBefore(HInstruction* cursor) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001089 next_->previous_ = previous_;
1090 if (previous_ != nullptr) {
1091 previous_->next_ = next_;
1092 }
1093 if (block_->instructions_.first_instruction_ == this) {
1094 block_->instructions_.first_instruction_ = next_;
1095 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001096 DCHECK_NE(block_->instructions_.last_instruction_, this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001097
1098 previous_ = cursor->previous_;
1099 if (previous_ != nullptr) {
1100 previous_->next_ = this;
1101 }
1102 next_ = cursor;
1103 cursor->previous_ = this;
1104 block_ = cursor->block_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001105
1106 if (block_->instructions_.first_instruction_ == cursor) {
1107 block_->instructions_.first_instruction_ = this;
1108 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001109}
1110
David Brazdilfc6a86a2015-06-26 10:33:45 +00001111HBasicBlock* HBasicBlock::SplitBefore(HInstruction* cursor) {
1112 DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented";
1113 DCHECK_EQ(cursor->GetBlock(), this);
1114
1115 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
1116 new_block->instructions_.first_instruction_ = cursor;
1117 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1118 instructions_.last_instruction_ = cursor->previous_;
1119 if (cursor->previous_ == nullptr) {
1120 instructions_.first_instruction_ = nullptr;
1121 } else {
1122 cursor->previous_->next_ = nullptr;
1123 cursor->previous_ = nullptr;
1124 }
1125
1126 new_block->instructions_.SetBlockOfInstructions(new_block);
1127 AddInstruction(new (GetGraph()->GetArena()) HGoto());
1128
1129 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
1130 HBasicBlock* successor = GetSuccessors().Get(i);
1131 new_block->successors_.Add(successor);
1132 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
1133 }
1134 successors_.Reset();
1135 AddSuccessor(new_block);
1136
David Brazdil56e1acc2015-06-30 15:41:36 +01001137 GetGraph()->AddBlock(new_block);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001138 return new_block;
1139}
1140
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001141HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) {
1142 DCHECK(!cursor->IsControlFlow());
1143 DCHECK_NE(instructions_.last_instruction_, cursor);
1144 DCHECK_EQ(cursor->GetBlock(), this);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001145
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001146 HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc());
1147 new_block->instructions_.first_instruction_ = cursor->GetNext();
1148 new_block->instructions_.last_instruction_ = instructions_.last_instruction_;
1149 cursor->next_->previous_ = nullptr;
1150 cursor->next_ = nullptr;
1151 instructions_.last_instruction_ = cursor;
1152
1153 new_block->instructions_.SetBlockOfInstructions(new_block);
1154 for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) {
1155 HBasicBlock* successor = GetSuccessors().Get(i);
1156 new_block->successors_.Add(successor);
1157 successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block);
1158 }
1159 successors_.Reset();
1160
1161 for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) {
1162 HBasicBlock* dominated = GetDominatedBlocks().Get(i);
1163 dominated->dominator_ = new_block;
1164 new_block->dominated_blocks_.Add(dominated);
1165 }
1166 dominated_blocks_.Reset();
1167 return new_block;
1168}
1169
David Brazdilec16f792015-08-19 15:04:01 +01001170const HTryBoundary* HBasicBlock::ComputeTryEntryOfSuccessors() const {
David Brazdilffee3d32015-07-06 11:48:53 +01001171 if (EndsWithTryBoundary()) {
1172 HTryBoundary* try_boundary = GetLastInstruction()->AsTryBoundary();
1173 if (try_boundary->IsEntry()) {
David Brazdilec16f792015-08-19 15:04:01 +01001174 DCHECK(!IsTryBlock());
David Brazdilffee3d32015-07-06 11:48:53 +01001175 return try_boundary;
1176 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001177 DCHECK(IsTryBlock());
1178 DCHECK(try_catch_information_->GetTryEntry().HasSameExceptionHandlersAs(*try_boundary));
David Brazdilffee3d32015-07-06 11:48:53 +01001179 return nullptr;
1180 }
David Brazdilec16f792015-08-19 15:04:01 +01001181 } else if (IsTryBlock()) {
1182 return &try_catch_information_->GetTryEntry();
David Brazdilffee3d32015-07-06 11:48:53 +01001183 } else {
David Brazdilec16f792015-08-19 15:04:01 +01001184 return nullptr;
David Brazdilffee3d32015-07-06 11:48:53 +01001185 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00001186}
1187
1188static bool HasOnlyOneInstruction(const HBasicBlock& block) {
1189 return block.GetPhis().IsEmpty()
1190 && !block.GetInstructions().IsEmpty()
1191 && block.GetFirstInstruction() == block.GetLastInstruction();
1192}
1193
David Brazdil46e2a392015-03-16 17:31:52 +00001194bool HBasicBlock::IsSingleGoto() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001195 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsGoto();
1196}
1197
1198bool HBasicBlock::IsSingleTryBoundary() const {
1199 return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsTryBoundary();
David Brazdil46e2a392015-03-16 17:31:52 +00001200}
1201
David Brazdil8d5b8b22015-03-24 10:51:52 +00001202bool HBasicBlock::EndsWithControlFlowInstruction() const {
1203 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow();
1204}
1205
David Brazdilb2bd1c52015-03-25 11:17:37 +00001206bool HBasicBlock::EndsWithIf() const {
1207 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf();
1208}
1209
David Brazdilffee3d32015-07-06 11:48:53 +01001210bool HBasicBlock::EndsWithTryBoundary() const {
1211 return !GetInstructions().IsEmpty() && GetLastInstruction()->IsTryBoundary();
1212}
1213
David Brazdilb2bd1c52015-03-25 11:17:37 +00001214bool HBasicBlock::HasSinglePhi() const {
1215 return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr;
1216}
1217
David Brazdilffee3d32015-07-06 11:48:53 +01001218bool HTryBoundary::HasSameExceptionHandlersAs(const HTryBoundary& other) const {
1219 if (GetBlock()->GetSuccessors().Size() != other.GetBlock()->GetSuccessors().Size()) {
1220 return false;
1221 }
1222
David Brazdilb618ade2015-07-29 10:31:29 +01001223 // Exception handlers need to be stored in the same order.
1224 for (HExceptionHandlerIterator it1(*this), it2(other);
1225 !it1.Done();
1226 it1.Advance(), it2.Advance()) {
1227 DCHECK(!it2.Done());
1228 if (it1.Current() != it2.Current()) {
David Brazdilffee3d32015-07-06 11:48:53 +01001229 return false;
1230 }
1231 }
1232 return true;
1233}
1234
David Brazdil2d7352b2015-04-20 14:52:42 +01001235size_t HInstructionList::CountSize() const {
1236 size_t size = 0;
1237 HInstruction* current = first_instruction_;
1238 for (; current != nullptr; current = current->GetNext()) {
1239 size++;
1240 }
1241 return size;
1242}
1243
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001244void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const {
1245 for (HInstruction* current = first_instruction_;
1246 current != nullptr;
1247 current = current->GetNext()) {
1248 current->SetBlock(block);
1249 }
1250}
1251
1252void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) {
1253 DCHECK(Contains(cursor));
1254 if (!instruction_list.IsEmpty()) {
1255 if (cursor == last_instruction_) {
1256 last_instruction_ = instruction_list.last_instruction_;
1257 } else {
1258 cursor->next_->previous_ = instruction_list.last_instruction_;
1259 }
1260 instruction_list.last_instruction_->next_ = cursor->next_;
1261 cursor->next_ = instruction_list.first_instruction_;
1262 instruction_list.first_instruction_->previous_ = cursor;
1263 }
1264}
1265
1266void HInstructionList::Add(const HInstructionList& instruction_list) {
David Brazdil46e2a392015-03-16 17:31:52 +00001267 if (IsEmpty()) {
1268 first_instruction_ = instruction_list.first_instruction_;
1269 last_instruction_ = instruction_list.last_instruction_;
1270 } else {
1271 AddAfter(last_instruction_, instruction_list);
1272 }
1273}
1274
David Brazdil2d7352b2015-04-20 14:52:42 +01001275void HBasicBlock::DisconnectAndDelete() {
1276 // Dominators must be removed after all the blocks they dominate. This way
1277 // a loop header is removed last, a requirement for correct loop information
1278 // iteration.
1279 DCHECK(dominated_blocks_.IsEmpty());
David Brazdil46e2a392015-03-16 17:31:52 +00001280
David Brazdil2d7352b2015-04-20 14:52:42 +01001281 // Remove the block from all loops it is included in.
1282 for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) {
1283 HLoopInformation* loop_info = it.Current();
1284 loop_info->Remove(this);
1285 if (loop_info->IsBackEdge(*this)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001286 // If this was the last back edge of the loop, we deliberately leave the
1287 // loop in an inconsistent state and will fail SSAChecker unless the
1288 // entire loop is removed during the pass.
David Brazdil2d7352b2015-04-20 14:52:42 +01001289 loop_info->RemoveBackEdge(this);
1290 }
1291 }
1292
1293 // Disconnect the block from its predecessors and update their control-flow
1294 // instructions.
David Brazdil46e2a392015-03-16 17:31:52 +00001295 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001296 HBasicBlock* predecessor = predecessors_.Get(i);
1297 HInstruction* last_instruction = predecessor->GetLastInstruction();
1298 predecessor->RemoveInstruction(last_instruction);
1299 predecessor->RemoveSuccessor(this);
1300 if (predecessor->GetSuccessors().Size() == 1u) {
1301 DCHECK(last_instruction->IsIf());
1302 predecessor->AddInstruction(new (graph_->GetArena()) HGoto());
1303 } else {
1304 // The predecessor has no remaining successors and therefore must be dead.
1305 // We deliberately leave it without a control-flow instruction so that the
1306 // SSAChecker fails unless it is not removed during the pass too.
1307 DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u);
1308 }
David Brazdil46e2a392015-03-16 17:31:52 +00001309 }
David Brazdil46e2a392015-03-16 17:31:52 +00001310 predecessors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001311
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001312 // Disconnect the block from its successors and update their phis.
David Brazdil2d7352b2015-04-20 14:52:42 +01001313 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
1314 HBasicBlock* successor = successors_.Get(i);
1315 // Delete this block from the list of predecessors.
1316 size_t this_index = successor->GetPredecessorIndexOf(this);
1317 successor->predecessors_.DeleteAt(this_index);
1318
1319 // Check that `successor` has other predecessors, otherwise `this` is the
1320 // dominator of `successor` which violates the order DCHECKed at the top.
1321 DCHECK(!successor->predecessors_.IsEmpty());
1322
David Brazdil2d7352b2015-04-20 14:52:42 +01001323 // Remove this block's entries in the successor's phis.
1324 if (successor->predecessors_.Size() == 1u) {
1325 // The successor has just one predecessor left. Replace phis with the only
1326 // remaining input.
1327 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1328 HPhi* phi = phi_it.Current()->AsPhi();
1329 phi->ReplaceWith(phi->InputAt(1 - this_index));
1330 successor->RemovePhi(phi);
1331 }
1332 } else {
1333 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
1334 phi_it.Current()->AsPhi()->RemoveInputAt(this_index);
1335 }
1336 }
1337 }
David Brazdil46e2a392015-03-16 17:31:52 +00001338 successors_.Reset();
David Brazdil2d7352b2015-04-20 14:52:42 +01001339
1340 // Disconnect from the dominator.
1341 dominator_->RemoveDominatedBlock(this);
1342 SetDominator(nullptr);
1343
1344 // Delete from the graph. The function safely deletes remaining instructions
1345 // and updates the reverse post order.
1346 graph_->DeleteDeadBlock(this);
1347 SetGraph(nullptr);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001348}
1349
1350void HBasicBlock::MergeWith(HBasicBlock* other) {
David Brazdil2d7352b2015-04-20 14:52:42 +01001351 DCHECK_EQ(GetGraph(), other->GetGraph());
1352 DCHECK(GetDominatedBlocks().Contains(other));
1353 DCHECK_EQ(GetSuccessors().Size(), 1u);
1354 DCHECK_EQ(GetSuccessors().Get(0), other);
1355 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1356 DCHECK_EQ(other->GetPredecessors().Get(0), this);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001357 DCHECK(other->GetPhis().IsEmpty());
1358
David Brazdil2d7352b2015-04-20 14:52:42 +01001359 // Move instructions from `other` to `this`.
1360 DCHECK(EndsWithControlFlowInstruction());
1361 RemoveInstruction(GetLastInstruction());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001362 instructions_.Add(other->GetInstructions());
David Brazdil2d7352b2015-04-20 14:52:42 +01001363 other->instructions_.SetBlockOfInstructions(this);
1364 other->instructions_.Clear();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001365
David Brazdil2d7352b2015-04-20 14:52:42 +01001366 // Remove `other` from the loops it is included in.
1367 for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) {
1368 HLoopInformation* loop_info = it.Current();
1369 loop_info->Remove(other);
1370 if (loop_info->IsBackEdge(*other)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001371 loop_info->ReplaceBackEdge(other, this);
David Brazdil2d7352b2015-04-20 14:52:42 +01001372 }
1373 }
1374
1375 // Update links to the successors of `other`.
1376 successors_.Reset();
1377 while (!other->successors_.IsEmpty()) {
1378 HBasicBlock* successor = other->successors_.Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001379 successor->ReplacePredecessor(other, this);
1380 }
1381
David Brazdil2d7352b2015-04-20 14:52:42 +01001382 // Update the dominator tree.
1383 dominated_blocks_.Delete(other);
1384 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1385 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1386 dominated_blocks_.Add(dominated);
1387 dominated->SetDominator(this);
1388 }
1389 other->dominated_blocks_.Reset();
1390 other->dominator_ = nullptr;
1391
1392 // Clear the list of predecessors of `other` in preparation of deleting it.
1393 other->predecessors_.Reset();
1394
1395 // Delete `other` from the graph. The function updates reverse post order.
1396 graph_->DeleteDeadBlock(other);
1397 other->SetGraph(nullptr);
1398}
1399
1400void HBasicBlock::MergeWithInlined(HBasicBlock* other) {
1401 DCHECK_NE(GetGraph(), other->GetGraph());
1402 DCHECK(GetDominatedBlocks().IsEmpty());
1403 DCHECK(GetSuccessors().IsEmpty());
1404 DCHECK(!EndsWithControlFlowInstruction());
1405 DCHECK_EQ(other->GetPredecessors().Size(), 1u);
1406 DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock());
1407 DCHECK(other->GetPhis().IsEmpty());
1408 DCHECK(!other->IsInLoop());
1409
1410 // Move instructions from `other` to `this`.
1411 instructions_.Add(other->GetInstructions());
1412 other->instructions_.SetBlockOfInstructions(this);
1413
1414 // Update links to the successors of `other`.
1415 successors_.Reset();
1416 while (!other->successors_.IsEmpty()) {
1417 HBasicBlock* successor = other->successors_.Get(0);
1418 successor->ReplacePredecessor(other, this);
1419 }
1420
1421 // Update the dominator tree.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001422 for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) {
1423 HBasicBlock* dominated = other->GetDominatedBlocks().Get(i);
1424 dominated_blocks_.Add(dominated);
1425 dominated->SetDominator(this);
1426 }
1427 other->dominated_blocks_.Reset();
1428 other->dominator_ = nullptr;
1429 other->graph_ = nullptr;
1430}
1431
1432void HBasicBlock::ReplaceWith(HBasicBlock* other) {
1433 while (!GetPredecessors().IsEmpty()) {
1434 HBasicBlock* predecessor = GetPredecessors().Get(0);
1435 predecessor->ReplaceSuccessor(this, other);
1436 }
1437 while (!GetSuccessors().IsEmpty()) {
1438 HBasicBlock* successor = GetSuccessors().Get(0);
1439 successor->ReplacePredecessor(this, other);
1440 }
1441 for (size_t i = 0; i < dominated_blocks_.Size(); ++i) {
1442 other->AddDominatedBlock(dominated_blocks_.Get(i));
1443 }
1444 GetDominator()->ReplaceDominatedBlock(this, other);
1445 other->SetDominator(GetDominator());
1446 dominator_ = nullptr;
1447 graph_ = nullptr;
1448}
1449
1450// Create space in `blocks` for adding `number_of_new_blocks` entries
1451// starting at location `at`. Blocks after `at` are moved accordingly.
1452static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks,
1453 size_t number_of_new_blocks,
1454 size_t at) {
1455 size_t old_size = blocks->Size();
1456 size_t new_size = old_size + number_of_new_blocks;
1457 blocks->SetSize(new_size);
1458 for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) {
1459 blocks->Put(j, blocks->Get(i));
1460 }
1461}
1462
David Brazdil2d7352b2015-04-20 14:52:42 +01001463void HGraph::DeleteDeadBlock(HBasicBlock* block) {
1464 DCHECK_EQ(block->GetGraph(), this);
1465 DCHECK(block->GetSuccessors().IsEmpty());
1466 DCHECK(block->GetPredecessors().IsEmpty());
1467 DCHECK(block->GetDominatedBlocks().IsEmpty());
1468 DCHECK(block->GetDominator() == nullptr);
1469
1470 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1471 block->RemoveInstruction(it.Current());
1472 }
1473 for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1474 block->RemovePhi(it.Current()->AsPhi());
1475 }
1476
David Brazdilc7af85d2015-05-26 12:05:55 +01001477 if (block->IsExitBlock()) {
1478 exit_block_ = nullptr;
1479 }
1480
David Brazdil2d7352b2015-04-20 14:52:42 +01001481 reverse_post_order_.Delete(block);
1482 blocks_.Put(block->GetBlockId(), nullptr);
1483}
1484
Calin Juravle2e768302015-07-28 14:41:11 +00001485HInstruction* HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) {
David Brazdilc7af85d2015-05-26 12:05:55 +01001486 DCHECK(HasExitBlock()) << "Unimplemented scenario";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001487 // Update the environments in this graph to have the invoke's environment
1488 // as parent.
1489 {
1490 HReversePostOrderIterator it(*this);
1491 it.Advance(); // Skip the entry block, we do not need to update the entry's suspend check.
1492 for (; !it.Done(); it.Advance()) {
1493 HBasicBlock* block = it.Current();
1494 for (HInstructionIterator instr_it(block->GetInstructions());
1495 !instr_it.Done();
1496 instr_it.Advance()) {
1497 HInstruction* current = instr_it.Current();
1498 if (current->NeedsEnvironment()) {
1499 current->GetEnvironment()->SetAndCopyParentChain(
1500 outer_graph->GetArena(), invoke->GetEnvironment());
1501 }
1502 }
1503 }
1504 }
1505 outer_graph->UpdateMaximumNumberOfOutVRegs(GetMaximumNumberOfOutVRegs());
1506 if (HasBoundsChecks()) {
1507 outer_graph->SetHasBoundsChecks(true);
1508 }
1509
Calin Juravle2e768302015-07-28 14:41:11 +00001510 HInstruction* return_value = nullptr;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001511 if (GetBlocks().Size() == 3) {
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001512 // Simple case of an entry block, a body block, and an exit block.
1513 // Put the body block's instruction into `invoke`'s block.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001514 HBasicBlock* body = GetBlocks().Get(1);
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001515 DCHECK(GetBlocks().Get(0)->IsEntryBlock());
1516 DCHECK(GetBlocks().Get(2)->IsExitBlock());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001517 DCHECK(!body->IsExitBlock());
1518 HInstruction* last = body->GetLastInstruction();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001519
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001520 invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions());
1521 body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001522
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001523 // Replace the invoke with the return value of the inlined graph.
1524 if (last->IsReturn()) {
Calin Juravle2e768302015-07-28 14:41:11 +00001525 return_value = last->InputAt(0);
1526 invoke->ReplaceWith(return_value);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001527 } else {
1528 DCHECK(last->IsReturnVoid());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001529 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001530
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001531 invoke->GetBlock()->RemoveInstruction(last);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001532 } else {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001533 // Need to inline multiple blocks. We split `invoke`'s block
1534 // into two blocks, merge the first block of the inlined graph into
Nicolas Geoffraybe31ff92015-02-04 14:52:20 +00001535 // the first half, and replace the exit block of the inlined graph
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001536 // with the second half.
1537 ArenaAllocator* allocator = outer_graph->GetArena();
1538 HBasicBlock* at = invoke->GetBlock();
1539 HBasicBlock* to = at->SplitAfter(invoke);
1540
1541 HBasicBlock* first = entry_block_->GetSuccessors().Get(0);
1542 DCHECK(!first->IsInLoop());
David Brazdil2d7352b2015-04-20 14:52:42 +01001543 at->MergeWithInlined(first);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001544 exit_block_->ReplaceWith(to);
1545
1546 // Update all predecessors of the exit block (now the `to` block)
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001547 // to not `HReturn` but `HGoto` instead.
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001548 bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
1549 if (to->GetPredecessors().Size() == 1) {
1550 HBasicBlock* predecessor = to->GetPredecessors().Get(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001551 HInstruction* last = predecessor->GetLastInstruction();
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001552 if (!returns_void) {
1553 return_value = last->InputAt(0);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001554 }
1555 predecessor->AddInstruction(new (allocator) HGoto());
1556 predecessor->RemoveInstruction(last);
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001557 } else {
1558 if (!returns_void) {
1559 // There will be multiple returns.
Nicolas Geoffray4f1a3842015-03-12 10:34:11 +00001560 return_value = new (allocator) HPhi(
1561 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()));
Nicolas Geoffray817bce72015-02-24 13:35:38 +00001562 to->AddPhi(return_value->AsPhi());
1563 }
1564 for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
1565 HBasicBlock* predecessor = to->GetPredecessors().Get(i);
1566 HInstruction* last = predecessor->GetLastInstruction();
1567 if (!returns_void) {
1568 return_value->AsPhi()->AddInput(last->InputAt(0));
1569 }
1570 predecessor->AddInstruction(new (allocator) HGoto());
1571 predecessor->RemoveInstruction(last);
1572 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001573 }
1574
1575 if (return_value != nullptr) {
1576 invoke->ReplaceWith(return_value);
1577 }
1578
1579 // Update the meta information surrounding blocks:
1580 // (1) the graph they are now in,
1581 // (2) the reverse post order of that graph,
1582 // (3) the potential loop information they are now in.
1583
1584 // We don't add the entry block, the exit block, and the first block, which
1585 // has been merged with `at`.
1586 static constexpr int kNumberOfSkippedBlocksInCallee = 3;
1587
1588 // We add the `to` block.
1589 static constexpr int kNumberOfNewBlocksInCaller = 1;
1590 size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee)
1591 + kNumberOfNewBlocksInCaller;
1592
1593 // Find the location of `at` in the outer graph's reverse post order. The new
1594 // blocks will be added after it.
1595 size_t index_of_at = 0;
1596 while (outer_graph->reverse_post_order_.Get(index_of_at) != at) {
1597 index_of_at++;
1598 }
1599 MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at);
1600
1601 // Do a reverse post order of the blocks in the callee and do (1), (2),
1602 // and (3) to the blocks that apply.
1603 HLoopInformation* info = at->GetLoopInformation();
1604 for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) {
1605 HBasicBlock* current = it.Current();
1606 if (current != exit_block_ && current != entry_block_ && current != first) {
1607 DCHECK(!current->IsInLoop());
1608 DCHECK(current->GetGraph() == this);
1609 current->SetGraph(outer_graph);
1610 outer_graph->AddBlock(current);
1611 outer_graph->reverse_post_order_.Put(++index_of_at, current);
1612 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001613 current->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001614 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1615 loop_it.Current()->Add(current);
1616 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001617 }
1618 }
1619 }
1620
1621 // Do (1), (2), and (3) to `to`.
1622 to->SetGraph(outer_graph);
1623 outer_graph->AddBlock(to);
1624 outer_graph->reverse_post_order_.Put(++index_of_at, to);
1625 if (info != nullptr) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001626 to->SetLoopInformation(info);
David Brazdil7d275372015-04-21 16:36:35 +01001627 for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) {
1628 loop_it.Current()->Add(to);
1629 }
David Brazdil46e2a392015-03-16 17:31:52 +00001630 if (info->IsBackEdge(*at)) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001631 // Only `to` can become a back edge, as the inlined blocks
1632 // are predecessors of `to`.
1633 info->ReplaceBackEdge(at, to);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001634 }
1635 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001636 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001637
David Brazdil05144f42015-04-16 15:18:00 +01001638 // Update the next instruction id of the outer graph, so that instructions
1639 // added later get bigger ids than those in the inner graph.
1640 outer_graph->SetCurrentInstructionId(GetNextInstructionId());
1641
1642 // Walk over the entry block and:
1643 // - Move constants from the entry block to the outer_graph's entry block,
1644 // - Replace HParameterValue instructions with their real value.
1645 // - Remove suspend checks, that hold an environment.
1646 // We must do this after the other blocks have been inlined, otherwise ids of
1647 // constants could overlap with the inner graph.
Roland Levillain4c0eb422015-04-24 16:43:49 +01001648 size_t parameter_index = 0;
David Brazdil05144f42015-04-16 15:18:00 +01001649 for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) {
1650 HInstruction* current = it.Current();
1651 if (current->IsNullConstant()) {
1652 current->ReplaceWith(outer_graph->GetNullConstant());
1653 } else if (current->IsIntConstant()) {
1654 current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue()));
1655 } else if (current->IsLongConstant()) {
1656 current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue()));
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00001657 } else if (current->IsFloatConstant()) {
1658 current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue()));
1659 } else if (current->IsDoubleConstant()) {
1660 current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue()));
David Brazdil05144f42015-04-16 15:18:00 +01001661 } else if (current->IsParameterValue()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001662 if (kIsDebugBuild
1663 && invoke->IsInvokeStaticOrDirect()
1664 && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) {
1665 // Ensure we do not use the last input of `invoke`, as it
1666 // contains a clinit check which is not an actual argument.
1667 size_t last_input_index = invoke->InputCount() - 1;
1668 DCHECK(parameter_index != last_input_index);
1669 }
David Brazdil05144f42015-04-16 15:18:00 +01001670 current->ReplaceWith(invoke->InputAt(parameter_index++));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001671 } else if (current->IsCurrentMethod()) {
1672 current->ReplaceWith(outer_graph->GetCurrentMethod());
David Brazdil05144f42015-04-16 15:18:00 +01001673 } else {
1674 DCHECK(current->IsGoto() || current->IsSuspendCheck());
1675 entry_block_->RemoveInstruction(current);
1676 }
1677 }
1678
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +00001679 // Finally remove the invoke from the caller.
1680 invoke->GetBlock()->RemoveInstruction(invoke);
Calin Juravle2e768302015-07-28 14:41:11 +00001681
1682 return return_value;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001683}
1684
Mingyao Yang3584bce2015-05-19 16:01:59 -07001685/*
1686 * Loop will be transformed to:
1687 * old_pre_header
1688 * |
1689 * if_block
1690 * / \
1691 * dummy_block deopt_block
1692 * \ /
1693 * new_pre_header
1694 * |
1695 * header
1696 */
1697void HGraph::TransformLoopHeaderForBCE(HBasicBlock* header) {
1698 DCHECK(header->IsLoopHeader());
1699 HBasicBlock* pre_header = header->GetDominator();
1700
1701 // Need this to avoid critical edge.
1702 HBasicBlock* if_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1703 // Need this to avoid critical edge.
1704 HBasicBlock* dummy_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1705 HBasicBlock* deopt_block = new (arena_) HBasicBlock(this, header->GetDexPc());
1706 HBasicBlock* new_pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
1707 AddBlock(if_block);
1708 AddBlock(dummy_block);
1709 AddBlock(deopt_block);
1710 AddBlock(new_pre_header);
1711
1712 header->ReplacePredecessor(pre_header, new_pre_header);
1713 pre_header->successors_.Reset();
1714 pre_header->dominated_blocks_.Reset();
1715
1716 pre_header->AddSuccessor(if_block);
1717 if_block->AddSuccessor(dummy_block); // True successor
1718 if_block->AddSuccessor(deopt_block); // False successor
1719 dummy_block->AddSuccessor(new_pre_header);
1720 deopt_block->AddSuccessor(new_pre_header);
1721
1722 pre_header->dominated_blocks_.Add(if_block);
1723 if_block->SetDominator(pre_header);
1724 if_block->dominated_blocks_.Add(dummy_block);
1725 dummy_block->SetDominator(if_block);
1726 if_block->dominated_blocks_.Add(deopt_block);
1727 deopt_block->SetDominator(if_block);
1728 if_block->dominated_blocks_.Add(new_pre_header);
1729 new_pre_header->SetDominator(if_block);
1730 new_pre_header->dominated_blocks_.Add(header);
1731 header->SetDominator(new_pre_header);
1732
1733 size_t index_of_header = 0;
1734 while (reverse_post_order_.Get(index_of_header) != header) {
1735 index_of_header++;
1736 }
1737 MakeRoomFor(&reverse_post_order_, 4, index_of_header - 1);
1738 reverse_post_order_.Put(index_of_header++, if_block);
1739 reverse_post_order_.Put(index_of_header++, dummy_block);
1740 reverse_post_order_.Put(index_of_header++, deopt_block);
1741 reverse_post_order_.Put(index_of_header++, new_pre_header);
1742
1743 HLoopInformation* info = pre_header->GetLoopInformation();
1744 if (info != nullptr) {
1745 if_block->SetLoopInformation(info);
1746 dummy_block->SetLoopInformation(info);
1747 deopt_block->SetLoopInformation(info);
1748 new_pre_header->SetLoopInformation(info);
1749 for (HLoopInformationOutwardIterator loop_it(*pre_header);
1750 !loop_it.Done();
1751 loop_it.Advance()) {
1752 loop_it.Current()->Add(if_block);
1753 loop_it.Current()->Add(dummy_block);
1754 loop_it.Current()->Add(deopt_block);
1755 loop_it.Current()->Add(new_pre_header);
1756 }
1757 }
1758}
1759
Calin Juravle2e768302015-07-28 14:41:11 +00001760void HInstruction::SetReferenceTypeInfo(ReferenceTypeInfo rti) {
1761 if (kIsDebugBuild) {
1762 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1763 ScopedObjectAccess soa(Thread::Current());
1764 DCHECK(rti.IsValid()) << "Invalid RTI for " << DebugName();
1765 if (IsBoundType()) {
1766 // Having the test here spares us from making the method virtual just for
1767 // the sake of a DCHECK.
1768 ReferenceTypeInfo upper_bound_rti = AsBoundType()->GetUpperBound();
1769 DCHECK(upper_bound_rti.IsSupertypeOf(rti))
1770 << " upper_bound_rti: " << upper_bound_rti
1771 << " rti: " << rti;
1772 DCHECK(!upper_bound_rti.GetTypeHandle()->IsFinal() || rti.IsExact());
1773 }
1774 }
1775 reference_type_info_ = rti;
1776}
1777
1778ReferenceTypeInfo::ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
1779
1780ReferenceTypeInfo::ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
1781 : type_handle_(type_handle), is_exact_(is_exact) {
1782 if (kIsDebugBuild) {
1783 ScopedObjectAccess soa(Thread::Current());
1784 DCHECK(IsValidHandle(type_handle));
1785 }
1786}
1787
Calin Juravleacf735c2015-02-12 15:25:22 +00001788std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) {
1789 ScopedObjectAccess soa(Thread::Current());
1790 os << "["
Calin Juravle2e768302015-07-28 14:41:11 +00001791 << " is_valid=" << rhs.IsValid()
1792 << " type=" << (!rhs.IsValid() ? "?" : PrettyClass(rhs.GetTypeHandle().Get()))
Calin Juravleacf735c2015-02-12 15:25:22 +00001793 << " is_exact=" << rhs.IsExact()
1794 << " ]";
1795 return os;
1796}
1797
Mark Mendellc4701932015-04-10 13:18:51 -04001798bool HInstruction::HasAnyEnvironmentUseBefore(HInstruction* other) {
1799 // For now, assume that instructions in different blocks may use the
1800 // environment.
1801 // TODO: Use the control flow to decide if this is true.
1802 if (GetBlock() != other->GetBlock()) {
1803 return true;
1804 }
1805
1806 // We know that we are in the same block. Walk from 'this' to 'other',
1807 // checking to see if there is any instruction with an environment.
1808 HInstruction* current = this;
1809 for (; current != other && current != nullptr; current = current->GetNext()) {
1810 // This is a conservative check, as the instruction result may not be in
1811 // the referenced environment.
1812 if (current->HasEnvironment()) {
1813 return true;
1814 }
1815 }
1816
1817 // We should have been called with 'this' before 'other' in the block.
1818 // Just confirm this.
1819 DCHECK(current != nullptr);
1820 return false;
1821}
1822
1823void HInstruction::RemoveEnvironmentUsers() {
1824 for (HUseIterator<HEnvironment*> use_it(GetEnvUses()); !use_it.Done(); use_it.Advance()) {
1825 HUseListNode<HEnvironment*>* user_node = use_it.Current();
1826 HEnvironment* user = user_node->GetUser();
1827 user->SetRawEnvAt(user_node->GetIndex(), nullptr);
1828 }
1829 env_uses_.Clear();
1830}
1831
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001832} // namespace art