blob: 3b284fbf43a2889fa327798d8102aaaa1d2a58e8 [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#ifndef ART_COMPILER_OPTIMIZING_ESCAPE_H_
18#define ART_COMPILER_OPTIMIZING_ESCAPE_H_
19
VladimĂ­r Marko434d9682022-11-04 14:04:17 +000020#include "base/macros.h"
21
22namespace art HIDDEN {
Aart Bik96fd51d2016-11-28 11:22:35 -080023
24class HInstruction;
25
26/*
27 * Methods related to escape analysis, i.e. determining whether an object
28 * allocation is visible outside ('escapes') its immediate method context.
29 */
30
Alex Light5ba66992020-09-23 15:04:36 -070031// A visitor for seeing all instructions escape analysis considers escaping.
32// Called with each user of the reference passed to 'VisitEscapes'. Return true
33// to continue iteration and false to stop.
34class EscapeVisitor {
35 public:
36 virtual ~EscapeVisitor() {}
37 virtual bool Visit(HInstruction* escape) = 0;
38 bool operator()(HInstruction* user) {
39 return Visit(user);
40 }
41};
42
43// An explicit EscapeVisitor for lambdas
44template <typename F>
45class LambdaEscapeVisitor final : public EscapeVisitor {
46 public:
47 explicit LambdaEscapeVisitor(F f) : func_(f) {}
48 bool Visit(HInstruction* escape) override {
49 return func_(escape);
50 }
51
52 private:
53 F func_;
54};
55
56// This functor is used with the escape-checking functions. If the NoEscape
57// function returns true escape analysis will consider 'user' to not have
58// escaped 'reference'. This allows clients with additional information to
59// supplement the escape-analysis. If the NoEscape function returns false then
60// the normal escape-checking code will be used to determine whether or not
61// 'reference' escapes.
62class NoEscapeCheck {
63 public:
64 virtual ~NoEscapeCheck() {}
65 virtual bool NoEscape(HInstruction* reference, HInstruction* user) = 0;
66 bool operator()(HInstruction* ref, HInstruction* user) {
67 return NoEscape(ref, user);
68 }
69};
70
71// An explicit NoEscapeCheck for use with c++ lambdas.
72template <typename F>
73class LambdaNoEscapeCheck final : public NoEscapeCheck {
74 public:
75 explicit LambdaNoEscapeCheck(F f) : func_(f) {}
76 bool NoEscape(HInstruction* ref, HInstruction* user) override {
77 return func_(ref, user);
78 }
79
80 private:
81 F func_;
82};
83
Aart Bik96fd51d2016-11-28 11:22:35 -080084/*
85 * Performs escape analysis on the given instruction, typically a reference to an
86 * allocation. The method assigns true to parameter 'is_singleton' if the reference
87 * is the only name that can refer to its value during the lifetime of the method,
88 * meaning that the reference is not aliased with something else, is not stored to
Aart Bik71bf7b42016-11-16 10:17:46 -080089 * heap memory, and not passed to another method. In addition, the method assigns
90 * true to parameter 'is_singleton_and_not_returned' if the reference is a singleton
91 * and not returned to the caller and to parameter 'is_singleton_and_not_deopt_visible'
92 * if the reference is a singleton and not used as an environment local of an
93 * HDeoptimize instruction (clients of the final value must run after BCE to ensure
94 * all such instructions have been introduced already).
95 *
96 * Note that being visible to a HDeoptimize instruction does not count for ordinary
97 * escape analysis, since switching between compiled code and interpreted code keeps
98 * non escaping references restricted to the lifetime of the method and the thread
99 * executing it. This property only concerns optimizations that are interested in
100 * escape analysis with respect to the *compiled* code (such as LSE).
Aart Bik96fd51d2016-11-28 11:22:35 -0800101 *
102 * When set, the no_escape function is applied to any use of the allocation instruction
103 * prior to any built-in escape analysis. This allows clients to define better escape
104 * analysis in certain case-specific circumstances. If 'no_escape(reference, user)'
105 * returns true, the user is assumed *not* to cause any escape right away. The return
106 * value false means the client cannot provide a definite answer and built-in escape
107 * analysis is applied to the user instead.
108 */
109void CalculateEscape(HInstruction* reference,
Alex Light5ba66992020-09-23 15:04:36 -0700110 NoEscapeCheck& no_escape,
Aart Bik96fd51d2016-11-28 11:22:35 -0800111 /*out*/ bool* is_singleton,
Aart Bik71bf7b42016-11-16 10:17:46 -0800112 /*out*/ bool* is_singleton_and_not_returned,
113 /*out*/ bool* is_singleton_and_not_deopt_visible);
Aart Bik96fd51d2016-11-28 11:22:35 -0800114
Alex Light5ba66992020-09-23 15:04:36 -0700115inline void CalculateEscape(HInstruction* reference,
116 bool (*no_escape_fn)(HInstruction*, HInstruction*),
117 /*out*/ bool* is_singleton,
118 /*out*/ bool* is_singleton_and_not_returned,
119 /*out*/ bool* is_singleton_and_not_deopt_visible) {
120 LambdaNoEscapeCheck esc(no_escape_fn);
121 LambdaNoEscapeCheck noop_esc([](HInstruction*, HInstruction*) { return false; });
122 CalculateEscape(reference,
123 no_escape_fn == nullptr ? static_cast<NoEscapeCheck&>(noop_esc) : esc,
124 is_singleton,
125 is_singleton_and_not_returned,
126 is_singleton_and_not_deopt_visible);
127}
128
129/*
130 * Performs escape analysis and visits each escape of the reference. Does not try to calculate any
131 * overall information about the method. Escapes are calculated in the same way as CalculateEscape.
132 *
133 * The escape_visitor should return true to continue visiting, false otherwise.
134 */
135void VisitEscapes(HInstruction* reference, EscapeVisitor& escape_visitor);
136
Aart Bik96fd51d2016-11-28 11:22:35 -0800137/*
Aart Bik71bf7b42016-11-16 10:17:46 -0800138 * Convenience method for testing the singleton and not returned properties at once.
Aart Bik96fd51d2016-11-28 11:22:35 -0800139 * Callers should be aware that this method invokes the full analysis at each call.
140 */
Alex Light5ba66992020-09-23 15:04:36 -0700141bool DoesNotEscape(HInstruction* reference, NoEscapeCheck& no_escape);
142
143inline bool DoesNotEscape(HInstruction* reference,
144 bool (*no_escape_fn)(HInstruction*, HInstruction*)) {
145 LambdaNoEscapeCheck<typeof(no_escape_fn)> esc(no_escape_fn);
146 return DoesNotEscape(reference, esc);
147}
Aart Bik96fd51d2016-11-28 11:22:35 -0800148
149} // namespace art
150
151#endif // ART_COMPILER_OPTIMIZING_ESCAPE_H_