blob: 4f17b6fc3bddd4470cab1a72527a3e707ad49150 [file] [log] [blame]
Calin Juravle10e244f2015-01-26 18:54:32 +00001/*
2 * Copyright (C) 2015 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 "reference_type_propagation.h"
18
19namespace art {
20
Calin Juravle10e244f2015-01-26 18:54:32 +000021void ReferenceTypePropagation::Run() {
22 // Compute null status for instructions.
23
24 // To properly propagate not-null info we need to visit in the dominator-based order.
25 // Reverse post order guarantees a node's dominators are visited first.
26 // We take advantage of this order in `VisitBasicBlock`.
27 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
28 VisitBasicBlock(it.Current());
29 }
30 ProcessWorklist();
31}
32
33// Re-computes and updates the nullability of the instruction. Returns whether or
34// not the nullability was changed.
35bool ReferenceTypePropagation::UpdateNullability(HPhi* phi) {
36 bool existing_can_be_null = phi->CanBeNull();
37 bool new_can_be_null = false;
38 for (size_t i = 0; i < phi->InputCount(); i++) {
39 new_can_be_null |= phi->InputAt(i)->CanBeNull();
40 }
41 phi->SetCanBeNull(new_can_be_null);
42
43 return existing_can_be_null != new_can_be_null;
44}
45
46
47void ReferenceTypePropagation::VisitBasicBlock(HBasicBlock* block) {
48 if (block->IsLoopHeader()) {
49 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
50 // Set the initial type for the phi. Use the non back edge input for reaching
51 // a fixed point faster.
52 HPhi* phi = it.Current()->AsPhi();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000053 if (phi->GetType() == Primitive::kPrimNot) {
54 AddToWorklist(phi);
55 phi->SetCanBeNull(phi->InputAt(0)->CanBeNull());
56 }
Calin Juravle10e244f2015-01-26 18:54:32 +000057 }
58 } else {
59 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
60 // Eagerly compute the type of the phi, for quicker convergence. Note
61 // that we don't need to add users to the worklist because we are
62 // doing a reverse post-order visit, therefore either the phi users are
63 // non-loop phi and will be visited later in the visit, or are loop-phis,
64 // and they are already in the work list.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000065 HPhi* phi = it.Current()->AsPhi();
66 if (phi->GetType() == Primitive::kPrimNot) {
67 UpdateNullability(phi);
68 }
Calin Juravle10e244f2015-01-26 18:54:32 +000069 }
70 }
71}
72
73void ReferenceTypePropagation::ProcessWorklist() {
74 while (!worklist_.IsEmpty()) {
75 HPhi* instruction = worklist_.Pop();
76 if (UpdateNullability(instruction)) {
77 AddDependentInstructionsToWorklist(instruction);
78 }
79 }
80}
81
82void ReferenceTypePropagation::AddToWorklist(HPhi* instruction) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000083 DCHECK_EQ(instruction->GetType(), Primitive::kPrimNot);
Calin Juravle10e244f2015-01-26 18:54:32 +000084 worklist_.Add(instruction);
85}
86
87void ReferenceTypePropagation::AddDependentInstructionsToWorklist(HPhi* instruction) {
88 for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) {
89 HPhi* phi = it.Current()->GetUser()->AsPhi();
90 if (phi != nullptr) {
91 AddToWorklist(phi);
92 }
93 }
94}
95
96} // namespace art