blob: 2b578c1cc88ecbc14b2caaeb5c6291b5507d0727 [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
21namespace art {
22
23void CalculateEscape(HInstruction* reference,
24 bool (*no_escape)(HInstruction*, HInstruction*),
25 /*out*/ bool* is_singleton,
Aart Bik71bf7b42016-11-16 10:17:46 -080026 /*out*/ bool* is_singleton_and_not_returned,
27 /*out*/ bool* is_singleton_and_not_deopt_visible) {
Aart Bik96fd51d2016-11-28 11:22:35 -080028 // For references not allocated in the method, don't assume anything.
29 if (!reference->IsNewInstance() && !reference->IsNewArray()) {
30 *is_singleton = false;
Aart Bik71bf7b42016-11-16 10:17:46 -080031 *is_singleton_and_not_returned = false;
32 *is_singleton_and_not_deopt_visible = false;
Aart Bik96fd51d2016-11-28 11:22:35 -080033 return;
34 }
35 // Assume the best until proven otherwise.
36 *is_singleton = true;
Aart Bik71bf7b42016-11-16 10:17:46 -080037 *is_singleton_and_not_returned = true;
38 *is_singleton_and_not_deopt_visible = true;
Mingyao Yang025c1a62017-10-30 11:19:57 -070039
40 if (reference->IsNewInstance() && reference->AsNewInstance()->IsFinalizable()) {
41 // Finalizable reference is treated as being returned in the end.
42 *is_singleton_and_not_returned = false;
43 }
44
Aart Bik96fd51d2016-11-28 11:22:35 -080045 // Visit all uses to determine if this reference can escape into the heap,
46 // a method call, an alias, etc.
47 for (const HUseListNode<HInstruction*>& use : reference->GetUses()) {
48 HInstruction* user = use.GetUser();
49 if (no_escape != nullptr && (*no_escape)(reference, user)) {
50 // Client supplied analysis says there is no escape.
51 continue;
52 } else if (user->IsBoundType() || user->IsNullCheck()) {
53 // BoundType shouldn't normally be necessary for an allocation. Just be conservative
54 // for the uncommon cases. Similarly, null checks are eventually eliminated for explicit
55 // allocations, but if we see one before it is simplified, assume an alias.
56 *is_singleton = false;
Aart Bik71bf7b42016-11-16 10:17:46 -080057 *is_singleton_and_not_returned = false;
58 *is_singleton_and_not_deopt_visible = false;
Aart Bik96fd51d2016-11-28 11:22:35 -080059 return;
Mingyao Yang293f1c02017-11-08 15:22:17 -080060 } else if (user->IsPhi() ||
61 user->IsSelect() ||
62 (user->IsInvoke() && user->GetSideEffects().DoesAnyWrite()) ||
Aart Bik96fd51d2016-11-28 11:22:35 -080063 (user->IsInstanceFieldSet() && (reference == user->InputAt(1))) ||
64 (user->IsUnresolvedInstanceFieldSet() && (reference == user->InputAt(1))) ||
65 (user->IsStaticFieldSet() && (reference == user->InputAt(1))) ||
66 (user->IsUnresolvedStaticFieldSet() && (reference == user->InputAt(0))) ||
67 (user->IsArraySet() && (reference == user->InputAt(2)))) {
68 // The reference is merged to HPhi/HSelect, passed to a callee, or stored to heap.
69 // Hence, the reference is no longer the only name that can refer to its value.
70 *is_singleton = false;
Aart Bik71bf7b42016-11-16 10:17:46 -080071 *is_singleton_and_not_returned = false;
72 *is_singleton_and_not_deopt_visible = false;
Aart Bik96fd51d2016-11-28 11:22:35 -080073 return;
74 } else if ((user->IsUnresolvedInstanceFieldGet() && (reference == user->InputAt(0))) ||
75 (user->IsUnresolvedInstanceFieldSet() && (reference == user->InputAt(0)))) {
76 // The field is accessed in an unresolved way. We mark the object as a non-singleton.
77 // Note that we could optimize this case and still perform some optimizations until
78 // we hit the unresolved access, but the conservative assumption is the simplest.
79 *is_singleton = false;
Aart Bik71bf7b42016-11-16 10:17:46 -080080 *is_singleton_and_not_returned = false;
81 *is_singleton_and_not_deopt_visible = false;
Aart Bik96fd51d2016-11-28 11:22:35 -080082 return;
83 } else if (user->IsReturn()) {
Aart Bik71bf7b42016-11-16 10:17:46 -080084 *is_singleton_and_not_returned = false;
Aart Bik96fd51d2016-11-28 11:22:35 -080085 }
86 }
87
Aart Bik71bf7b42016-11-16 10:17:46 -080088 // Look at the environment uses if it's for HDeoptimize. Other environment uses are fine,
89 // as long as client optimizations that rely on this information are disabled for debuggable.
Aart Bik96fd51d2016-11-28 11:22:35 -080090 for (const HUseListNode<HEnvironment*>& use : reference->GetEnvUses()) {
91 HEnvironment* user = use.GetUser();
92 if (user->GetHolder()->IsDeoptimize()) {
Aart Bik71bf7b42016-11-16 10:17:46 -080093 *is_singleton_and_not_deopt_visible = false;
Aart Bik96fd51d2016-11-28 11:22:35 -080094 break;
95 }
96 }
97}
98
Aart Bik71bf7b42016-11-16 10:17:46 -080099bool DoesNotEscape(HInstruction* reference, bool (*no_escape)(HInstruction*, HInstruction*)) {
100 bool is_singleton = false;
101 bool is_singleton_and_not_returned = false;
102 bool is_singleton_and_not_deopt_visible = false; // not relevant for escape
103 CalculateEscape(reference,
104 no_escape,
105 &is_singleton,
106 &is_singleton_and_not_returned,
107 &is_singleton_and_not_deopt_visible);
108 return is_singleton_and_not_returned;
Aart Bik96fd51d2016-11-28 11:22:35 -0800109}
110
111} // namespace art