blob: 479b87fea0ea81c32c271ebddf09d3077a36ddf9 [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
Calin Juravleacf735c2015-02-12 15:25:22 +000019#include "class_linker.h"
20#include "mirror/class-inl.h"
21#include "mirror/dex_cache.h"
22#include "scoped_thread_state_change.h"
23
Calin Juravle10e244f2015-01-26 18:54:32 +000024namespace art {
25
Calin Juravleacf735c2015-02-12 15:25:22 +000026void ReferenceTypePropagation::Run() {
27 // To properly propagate type info we need to visit in the dominator-based order.
Calin Juravle10e244f2015-01-26 18:54:32 +000028 // Reverse post order guarantees a node's dominators are visited first.
29 // We take advantage of this order in `VisitBasicBlock`.
30 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
31 VisitBasicBlock(it.Current());
32 }
33 ProcessWorklist();
34}
35
Calin Juravleb1498f62015-02-16 13:13:29 +000036void ReferenceTypePropagation::VisitBasicBlock(HBasicBlock* block) {
37 // TODO: handle other instructions that give type info
38 // (NewArray/Call/Field accesses/array accesses)
Calin Juravle10e244f2015-01-26 18:54:32 +000039
Calin Juravleb1498f62015-02-16 13:13:29 +000040 // Initialize exact types first for faster convergence.
41 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
42 HInstruction* instr = it.Current();
43 if (instr->IsNewInstance()) {
44 VisitNewInstance(instr->AsNewInstance());
45 } else if (instr->IsLoadClass()) {
46 VisitLoadClass(instr->AsLoadClass());
47 }
48 }
49
50 // Handle Phis.
51 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
52 VisitPhi(it.Current()->AsPhi());
53 }
54
55 // Add extra nodes to bound types.
Calin Juravle61d544b2015-02-23 16:46:57 +000056 BoundTypeForIfNotNull(block);
Calin Juravleb1498f62015-02-16 13:13:29 +000057 BoundTypeForIfInstanceOf(block);
Calin Juravle10e244f2015-01-26 18:54:32 +000058}
59
Calin Juravle61d544b2015-02-23 16:46:57 +000060void ReferenceTypePropagation::BoundTypeForIfNotNull(HBasicBlock* block) {
61 HInstruction* lastInstruction = block->GetLastInstruction();
62 if (!lastInstruction->IsIf()) {
63 return;
64 }
65 HInstruction* ifInput = lastInstruction->InputAt(0);
66 if (!ifInput->IsNotEqual() && !ifInput->IsEqual()) {
67 return;
68 }
69 HInstruction* input0 = ifInput->InputAt(0);
70 HInstruction* input1 = ifInput->InputAt(1);
71 HInstruction* obj;
72
73 if ((input0->GetType() == Primitive::kPrimNot) && input1->ActAsNullConstant()) {
74 obj = input0;
75 } else if ((input1->GetType() == Primitive::kPrimNot) && input0->ActAsNullConstant()) {
76 obj = input1;
77 } else {
78 return;
79 }
80
81 HBoundType* bound_type =
82 new (graph_->GetArena()) HBoundType(obj, ReferenceTypeInfo::CreateTop(false));
83
84 block->InsertInstructionBefore(bound_type, lastInstruction);
85 HBasicBlock* notNullBlock = ifInput->IsNotEqual()
86 ? lastInstruction->AsIf()->IfTrueSuccessor()
87 : lastInstruction->AsIf()->IfFalseSuccessor();
88 for (HUseIterator<HInstruction*> it(obj->GetUses()); !it.Done(); it.Advance()) {
89 HInstruction* user = it.Current()->GetUser();
90 if (notNullBlock->Dominates(user->GetBlock())) {
91 user->ReplaceInput(bound_type, it.Current()->GetIndex());
92 }
93 }
94}
95
Calin Juravleb1498f62015-02-16 13:13:29 +000096// Detects if `block` is the True block for the pattern
97// `if (x instanceof ClassX) { }`
98// If that's the case insert an HBoundType instruction to bound the type of `x`
99// to `ClassX` in the scope of the dominated blocks.
100void ReferenceTypePropagation::BoundTypeForIfInstanceOf(HBasicBlock* block) {
101 HInstruction* lastInstruction = block->GetLastInstruction();
102 if (!lastInstruction->IsIf()) {
103 return;
104 }
105 HInstruction* ifInput = lastInstruction->InputAt(0);
106 // TODO: Handle more patterns here: HIf(bool) HIf(HNotEqual).
107 if (!ifInput->IsEqual()) {
108 return;
109 }
110 HInstruction* instanceOf = ifInput->InputAt(0);
111 HInstruction* comp_value = ifInput->InputAt(1);
112 if (!instanceOf->IsInstanceOf() || !comp_value->IsIntConstant()) {
113 return;
Calin Juravleacf735c2015-02-12 15:25:22 +0000114 }
115
Calin Juravleb1498f62015-02-16 13:13:29 +0000116 HInstruction* obj = instanceOf->InputAt(0);
117 HLoadClass* load_class = instanceOf->InputAt(1)->AsLoadClass();
Calin Juravleacf735c2015-02-12 15:25:22 +0000118
Calin Juravleb1498f62015-02-16 13:13:29 +0000119 ReferenceTypeInfo obj_rti = obj->GetReferenceTypeInfo();
120 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
121 HBoundType* bound_type = new (graph_->GetArena()) HBoundType(obj, class_rti);
Calin Juravleacf735c2015-02-12 15:25:22 +0000122
Calin Juravleb1498f62015-02-16 13:13:29 +0000123 // Narrow the type as much as possible.
124 {
125 ScopedObjectAccess soa(Thread::Current());
126 if (!load_class->IsResolved() || class_rti.IsSupertypeOf(obj_rti)) {
127 bound_type->SetReferenceTypeInfo(obj_rti);
Calin Juravleacf735c2015-02-12 15:25:22 +0000128 } else {
Calin Juravleb1498f62015-02-16 13:13:29 +0000129 bound_type->SetReferenceTypeInfo(
130 ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ false));
Calin Juravleacf735c2015-02-12 15:25:22 +0000131 }
132 }
133
Calin Juravleb1498f62015-02-16 13:13:29 +0000134 block->InsertInstructionBefore(bound_type, lastInstruction);
135 // Pick the right successor based on the value we compare against.
136 HIntConstant* comp_value_int = comp_value->AsIntConstant();
137 HBasicBlock* instanceOfTrueBlock = comp_value_int->GetValue() == 0
138 ? lastInstruction->AsIf()->IfFalseSuccessor()
139 : lastInstruction->AsIf()->IfTrueSuccessor();
140
141 for (HUseIterator<HInstruction*> it(obj->GetUses()); !it.Done(); it.Advance()) {
142 HInstruction* user = it.Current()->GetUser();
143 if (instanceOfTrueBlock->Dominates(user->GetBlock())) {
144 user->ReplaceInput(bound_type, it.Current()->GetIndex());
145 }
146 }
Calin Juravleacf735c2015-02-12 15:25:22 +0000147}
148
149void ReferenceTypePropagation::VisitNewInstance(HNewInstance* instr) {
150 ScopedObjectAccess soa(Thread::Current());
151 mirror::DexCache* dex_cache = dex_compilation_unit_.GetClassLinker()->FindDexCache(dex_file_);
152 // Get type from dex cache assuming it was populated by the verifier.
153 mirror::Class* resolved_class = dex_cache->GetResolvedType(instr->GetTypeIndex());
154 if (resolved_class != nullptr) {
155 MutableHandle<mirror::Class> handle = handles_->NewHandle(resolved_class);
Calin Juravleb1498f62015-02-16 13:13:29 +0000156 instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(handle, true));
Calin Juravleacf735c2015-02-12 15:25:22 +0000157 }
158}
159
160void ReferenceTypePropagation::VisitLoadClass(HLoadClass* instr) {
161 ScopedObjectAccess soa(Thread::Current());
162 mirror::DexCache* dex_cache = dex_compilation_unit_.GetClassLinker()->FindDexCache(dex_file_);
163 // Get type from dex cache assuming it was populated by the verifier.
164 mirror::Class* resolved_class = dex_cache->GetResolvedType(instr->GetTypeIndex());
165 if (resolved_class != nullptr) {
166 Handle<mirror::Class> handle = handles_->NewHandle(resolved_class);
Calin Juravleb1498f62015-02-16 13:13:29 +0000167 instr->SetLoadedClassRTI(ReferenceTypeInfo::Create(handle, /* is_exact */ true));
Calin Juravleacf735c2015-02-12 15:25:22 +0000168 }
169 Handle<mirror::Class> class_handle = handles_->NewHandle(mirror::Class::GetJavaLangClass());
Calin Juravleb1498f62015-02-16 13:13:29 +0000170 instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(class_handle, /* is_exact */ true));
Calin Juravleacf735c2015-02-12 15:25:22 +0000171}
Calin Juravle10e244f2015-01-26 18:54:32 +0000172
Calin Juravleb1498f62015-02-16 13:13:29 +0000173void ReferenceTypePropagation::VisitPhi(HPhi* phi) {
174 if (phi->GetType() != Primitive::kPrimNot) {
175 return;
Calin Juravleacf735c2015-02-12 15:25:22 +0000176 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000177
178 if (phi->GetBlock()->IsLoopHeader()) {
179 // Set the initial type for the phi. Use the non back edge input for reaching
180 // a fixed point faster.
181 AddToWorklist(phi);
182 phi->SetCanBeNull(phi->InputAt(0)->CanBeNull());
183 phi->SetReferenceTypeInfo(phi->InputAt(0)->GetReferenceTypeInfo());
Calin Juravle10e244f2015-01-26 18:54:32 +0000184 } else {
Calin Juravleb1498f62015-02-16 13:13:29 +0000185 // Eagerly compute the type of the phi, for quicker convergence. Note
186 // that we don't need to add users to the worklist because we are
187 // doing a reverse post-order visit, therefore either the phi users are
188 // non-loop phi and will be visited later in the visit, or are loop-phis,
189 // and they are already in the work list.
190 UpdateNullability(phi);
191 UpdateReferenceTypeInfo(phi);
192 }
193}
194
195ReferenceTypeInfo ReferenceTypePropagation::MergeTypes(const ReferenceTypeInfo& a,
196 const ReferenceTypeInfo& b) {
197 bool is_exact = a.IsExact() && b.IsExact();
198 bool is_top = a.IsTop() || b.IsTop();
199 Handle<mirror::Class> type_handle;
200
201 if (!is_top) {
202 if (a.GetTypeHandle().Get() == b.GetTypeHandle().Get()) {
203 type_handle = a.GetTypeHandle();
204 } else if (a.IsSupertypeOf(b)) {
205 type_handle = a.GetTypeHandle();
206 is_exact = false;
207 } else if (b.IsSupertypeOf(a)) {
208 type_handle = b.GetTypeHandle();
209 is_exact = false;
210 } else {
211 // TODO: Find a common super class.
212 is_top = true;
213 is_exact = false;
214 }
215 }
216
217 return is_top
218 ? ReferenceTypeInfo::CreateTop(is_exact)
219 : ReferenceTypeInfo::Create(type_handle, is_exact);
220}
221
222bool ReferenceTypePropagation::UpdateReferenceTypeInfo(HInstruction* instr) {
223 ScopedObjectAccess soa(Thread::Current());
224
225 ReferenceTypeInfo previous_rti = instr->GetReferenceTypeInfo();
226 if (instr->IsBoundType()) {
227 UpdateBoundType(instr->AsBoundType());
228 } else if (instr->IsPhi()) {
229 UpdatePhi(instr->AsPhi());
230 } else {
231 LOG(FATAL) << "Invalid instruction (should not get here)";
232 }
233
234 return !previous_rti.IsEqual(instr->GetReferenceTypeInfo());
235}
236
237void ReferenceTypePropagation::UpdateBoundType(HBoundType* instr) {
238 ReferenceTypeInfo new_rti = instr->InputAt(0)->GetReferenceTypeInfo();
239 // Be sure that we don't go over the bounded type.
240 ReferenceTypeInfo bound_rti = instr->GetBoundType();
241 if (!bound_rti.IsSupertypeOf(new_rti)) {
242 new_rti = bound_rti;
243 }
244 instr->SetReferenceTypeInfo(new_rti);
245}
246
247void ReferenceTypePropagation::UpdatePhi(HPhi* instr) {
248 ReferenceTypeInfo new_rti = instr->InputAt(0)->GetReferenceTypeInfo();
249 if (new_rti.IsTop() && !new_rti.IsExact()) {
250 // Early return if we are Top and inexact.
251 instr->SetReferenceTypeInfo(new_rti);
252 return;
253 }
254 for (size_t i = 1; i < instr->InputCount(); i++) {
255 new_rti = MergeTypes(new_rti, instr->InputAt(i)->GetReferenceTypeInfo());
256 if (new_rti.IsTop()) {
257 if (!new_rti.IsExact()) {
258 break;
259 } else {
260 continue;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000261 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000262 }
263 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000264 instr->SetReferenceTypeInfo(new_rti);
265}
266
267// Re-computes and updates the nullability of the instruction. Returns whether or
268// not the nullability was changed.
269bool ReferenceTypePropagation::UpdateNullability(HInstruction* instr) {
270 DCHECK(instr->IsPhi() || instr->IsBoundType());
271
272 if (!instr->IsPhi()) {
273 return false;
274 }
275
276 HPhi* phi = instr->AsPhi();
277 bool existing_can_be_null = phi->CanBeNull();
278 bool new_can_be_null = false;
279 for (size_t i = 0; i < phi->InputCount(); i++) {
280 new_can_be_null |= phi->InputAt(i)->CanBeNull();
281 }
282 phi->SetCanBeNull(new_can_be_null);
283
284 return existing_can_be_null != new_can_be_null;
Calin Juravle10e244f2015-01-26 18:54:32 +0000285}
286
287void ReferenceTypePropagation::ProcessWorklist() {
288 while (!worklist_.IsEmpty()) {
Calin Juravleb1498f62015-02-16 13:13:29 +0000289 HInstruction* instruction = worklist_.Pop();
Calin Juravleacf735c2015-02-12 15:25:22 +0000290 if (UpdateNullability(instruction) || UpdateReferenceTypeInfo(instruction)) {
Calin Juravle10e244f2015-01-26 18:54:32 +0000291 AddDependentInstructionsToWorklist(instruction);
292 }
293 }
294}
295
Calin Juravleb1498f62015-02-16 13:13:29 +0000296void ReferenceTypePropagation::AddToWorklist(HInstruction* instruction) {
297 DCHECK_EQ(instruction->GetType(), Primitive::kPrimNot) << instruction->GetType();
Calin Juravle10e244f2015-01-26 18:54:32 +0000298 worklist_.Add(instruction);
299}
300
Calin Juravleb1498f62015-02-16 13:13:29 +0000301void ReferenceTypePropagation::AddDependentInstructionsToWorklist(HInstruction* instruction) {
Calin Juravle10e244f2015-01-26 18:54:32 +0000302 for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) {
Calin Juravleb1498f62015-02-16 13:13:29 +0000303 HInstruction* user = it.Current()->GetUser();
304 if (user->IsPhi() || user->IsBoundType()) {
305 AddToWorklist(user);
Calin Juravle10e244f2015-01-26 18:54:32 +0000306 }
307 }
308}
Calin Juravle10e244f2015-01-26 18:54:32 +0000309} // namespace art