blob: c80e19ef15188780825f003be993bf886300a386 [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,
26 /*out*/ bool* is_singleton_and_non_escaping) {
27 // For references not allocated in the method, don't assume anything.
28 if (!reference->IsNewInstance() && !reference->IsNewArray()) {
29 *is_singleton = false;
30 *is_singleton_and_non_escaping = false;
31 return;
32 }
33 // Assume the best until proven otherwise.
34 *is_singleton = true;
35 *is_singleton_and_non_escaping = true;
36 // Visit all uses to determine if this reference can escape into the heap,
37 // a method call, an alias, etc.
38 for (const HUseListNode<HInstruction*>& use : reference->GetUses()) {
39 HInstruction* user = use.GetUser();
40 if (no_escape != nullptr && (*no_escape)(reference, user)) {
41 // Client supplied analysis says there is no escape.
42 continue;
43 } else if (user->IsBoundType() || user->IsNullCheck()) {
44 // BoundType shouldn't normally be necessary for an allocation. Just be conservative
45 // for the uncommon cases. Similarly, null checks are eventually eliminated for explicit
46 // allocations, but if we see one before it is simplified, assume an alias.
47 *is_singleton = false;
48 *is_singleton_and_non_escaping = false;
49 return;
50 } else if (user->IsPhi() || user->IsSelect() || user->IsInvoke() ||
51 (user->IsInstanceFieldSet() && (reference == user->InputAt(1))) ||
52 (user->IsUnresolvedInstanceFieldSet() && (reference == user->InputAt(1))) ||
53 (user->IsStaticFieldSet() && (reference == user->InputAt(1))) ||
54 (user->IsUnresolvedStaticFieldSet() && (reference == user->InputAt(0))) ||
55 (user->IsArraySet() && (reference == user->InputAt(2)))) {
56 // The reference is merged to HPhi/HSelect, passed to a callee, or stored to heap.
57 // Hence, the reference is no longer the only name that can refer to its value.
58 *is_singleton = false;
59 *is_singleton_and_non_escaping = false;
60 return;
61 } else if ((user->IsUnresolvedInstanceFieldGet() && (reference == user->InputAt(0))) ||
62 (user->IsUnresolvedInstanceFieldSet() && (reference == user->InputAt(0)))) {
63 // The field is accessed in an unresolved way. We mark the object as a non-singleton.
64 // Note that we could optimize this case and still perform some optimizations until
65 // we hit the unresolved access, but the conservative assumption is the simplest.
66 *is_singleton = false;
67 *is_singleton_and_non_escaping = false;
68 return;
69 } else if (user->IsReturn()) {
70 *is_singleton_and_non_escaping = false;
71 }
72 }
73
74 // Need for further analysis?
75 if (!*is_singleton_and_non_escaping) {
76 return;
77 }
78
79 // Look at the environment uses and if it's for HDeoptimize, it's treated the
80 // same as a return which escapes at the end of executing the compiled code.
81 // Other environment uses are fine, as long as all client optimizations that
82 // rely on this informations are disabled for debuggable.
83 for (const HUseListNode<HEnvironment*>& use : reference->GetEnvUses()) {
84 HEnvironment* user = use.GetUser();
85 if (user->GetHolder()->IsDeoptimize()) {
86 *is_singleton_and_non_escaping = false;
87 break;
88 }
89 }
90}
91
92bool IsNonEscapingSingleton(HInstruction* reference,
93 bool (*no_escape)(HInstruction*, HInstruction*)) {
94 bool is_singleton = true;
95 bool is_singleton_and_non_escaping = true;
96 CalculateEscape(reference, no_escape, &is_singleton, &is_singleton_and_non_escaping);
97 return is_singleton_and_non_escaping;
98}
99
100} // namespace art