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