blob: 617833c697abeb2838f074e66d80a8562ef00f07 [file] [log] [blame]
Aart Bik96fd51d2016-11-28 11:22:35 -08001/*
2 * Copyright (C) 2016 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 "escape.h"
18
19#include "nodes.h"
20
Vladimir Marko0a516052019-10-14 13:00:44 +000021namespace art {
Aart Bik96fd51d2016-11-28 11:22:35 -080022
Alex Light5ba66992020-09-23 15:04:36 -070023void VisitEscapes(HInstruction* reference, EscapeVisitor& escape_visitor) {
24 // References not allocated in the method are intrinsically escaped.
25 // Finalizable references are always escaping since they end up in FinalizerQueues.
26 if ((!reference->IsNewInstance() && !reference->IsNewArray()) ||
27 (reference->IsNewInstance() && reference->AsNewInstance()->IsFinalizable())) {
28 if (!escape_visitor(reference)) {
29 return;
30 }
31 }
32
33 // Visit all uses to determine if this reference can escape into the heap,
34 // a method call, an alias, etc.
35 for (const HUseListNode<HInstruction*>& use : reference->GetUses()) {
36 HInstruction* user = use.GetUser();
37 if (user->IsBoundType() || user->IsNullCheck()) {
38 // BoundType shouldn't normally be necessary for an allocation. Just be conservative
39 // for the uncommon cases. Similarly, null checks are eventually eliminated for explicit
40 // allocations, but if we see one before it is simplified, assume an alias.
41 if (!escape_visitor(user)) {
42 return;
43 }
Alex Lightbb550e42021-04-21 17:04:13 -070044 } else if (user->IsCheckCast() || user->IsInstanceOf()) {
45 // TODO Currently we'll just be conservative for Partial LSE and avoid
46 // optimizing check-cast things since we'd need to add blocks otherwise.
47 // Normally the simplifier should be able to just get rid of them
48 if (!escape_visitor(user)) {
49 return;
50 }
Alex Light5ba66992020-09-23 15:04:36 -070051 } else if (user->IsPhi() ||
52 user->IsSelect() ||
53 (user->IsInvoke() && user->GetSideEffects().DoesAnyWrite()) ||
54 (user->IsInstanceFieldSet() && (reference == user->InputAt(1))) ||
55 (user->IsUnresolvedInstanceFieldSet() && (reference == user->InputAt(1))) ||
56 (user->IsStaticFieldSet() && (reference == user->InputAt(1))) ||
57 (user->IsUnresolvedStaticFieldSet() && (reference == user->InputAt(0))) ||
58 (user->IsArraySet() && (reference == user->InputAt(2)))) {
59 // The reference is merged to HPhi/HSelect, passed to a callee, or stored to heap.
60 // Hence, the reference is no longer the only name that can refer to its value.
61 if (!escape_visitor(user)) {
62 return;
63 }
64 } else if ((user->IsUnresolvedInstanceFieldGet() && (reference == user->InputAt(0))) ||
65 (user->IsUnresolvedInstanceFieldSet() && (reference == user->InputAt(0)))) {
66 // The field is accessed in an unresolved way. We mark the object as a non-singleton.
67 // Note that we could optimize this case and still perform some optimizations until
68 // we hit the unresolved access, but the conservative assumption is the simplest.
69 if (!escape_visitor(user)) {
70 return;
71 }
72 } else if (user->IsReturn()) {
73 if (!escape_visitor(user)) {
74 return;
75 }
76 }
77 }
78
79 // Look at the environment uses if it's for HDeoptimize. Other environment uses are fine,
80 // as long as client optimizations that rely on this information are disabled for debuggable.
81 for (const HUseListNode<HEnvironment*>& use : reference->GetEnvUses()) {
82 HEnvironment* user = use.GetUser();
83 if (user->GetHolder()->IsDeoptimize()) {
84 if (!escape_visitor(user->GetHolder())) {
85 return;
86 }
87 }
88 }
89}
90
Aart Bik96fd51d2016-11-28 11:22:35 -080091void CalculateEscape(HInstruction* reference,
Alex Light5ba66992020-09-23 15:04:36 -070092 NoEscapeCheck& no_escape,
Aart Bik96fd51d2016-11-28 11:22:35 -080093 /*out*/ bool* is_singleton,
Aart Bik71bf7b42016-11-16 10:17:46 -080094 /*out*/ bool* is_singleton_and_not_returned,
95 /*out*/ bool* is_singleton_and_not_deopt_visible) {
Aart Bik96fd51d2016-11-28 11:22:35 -080096 // For references not allocated in the method, don't assume anything.
97 if (!reference->IsNewInstance() && !reference->IsNewArray()) {
98 *is_singleton = false;
Aart Bik71bf7b42016-11-16 10:17:46 -080099 *is_singleton_and_not_returned = false;
100 *is_singleton_and_not_deopt_visible = false;
Aart Bik96fd51d2016-11-28 11:22:35 -0800101 return;
102 }
103 // Assume the best until proven otherwise.
104 *is_singleton = true;
Aart Bik71bf7b42016-11-16 10:17:46 -0800105 *is_singleton_and_not_returned = true;
106 *is_singleton_and_not_deopt_visible = true;
Mingyao Yang025c1a62017-10-30 11:19:57 -0700107
108 if (reference->IsNewInstance() && reference->AsNewInstance()->IsFinalizable()) {
109 // Finalizable reference is treated as being returned in the end.
110 *is_singleton_and_not_returned = false;
111 }
112
Alex Light5ba66992020-09-23 15:04:36 -0700113 LambdaEscapeVisitor visitor([&](HInstruction* escape) -> bool {
114 if (escape == reference || no_escape(reference, escape)) {
115 // Ignore already known inherent escapes and escapes client supplied
116 // analysis knows is safe. Continue on.
117 return true;
Alex Lightbb550e42021-04-21 17:04:13 -0700118 } else if (escape->IsInstanceOf() || escape->IsCheckCast()) {
119 // Ignore since these are not relevant for regular LSE.
120 return true;
Alex Light5ba66992020-09-23 15:04:36 -0700121 } else if (escape->IsReturn()) {
122 // value is returned but might still be singleton. Continue on.
123 *is_singleton_and_not_returned = false;
124 return true;
125 } else if (escape->IsDeoptimize()) {
126 // value escapes through deopt but might still be singleton. Continue on.
127 *is_singleton_and_not_deopt_visible = false;
128 return true;
129 } else {
130 // Real escape. All knowledge about what happens to the value lost. We can
131 // stop here.
Aart Bik96fd51d2016-11-28 11:22:35 -0800132 *is_singleton = false;
Aart Bik71bf7b42016-11-16 10:17:46 -0800133 *is_singleton_and_not_returned = false;
134 *is_singleton_and_not_deopt_visible = false;
Alex Light5ba66992020-09-23 15:04:36 -0700135 return false;
Aart Bik96fd51d2016-11-28 11:22:35 -0800136 }
Alex Light5ba66992020-09-23 15:04:36 -0700137 });
138 VisitEscapes(reference, visitor);
Aart Bik96fd51d2016-11-28 11:22:35 -0800139}
140
Alex Light5ba66992020-09-23 15:04:36 -0700141bool DoesNotEscape(HInstruction* reference, NoEscapeCheck& no_escape) {
Aart Bik71bf7b42016-11-16 10:17:46 -0800142 bool is_singleton = false;
143 bool is_singleton_and_not_returned = false;
144 bool is_singleton_and_not_deopt_visible = false; // not relevant for escape
145 CalculateEscape(reference,
146 no_escape,
147 &is_singleton,
148 &is_singleton_and_not_returned,
149 &is_singleton_and_not_deopt_visible);
150 return is_singleton_and_not_returned;
Aart Bik96fd51d2016-11-28 11:22:35 -0800151}
152
153} // namespace art