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