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 #include "base/macros.h"
21 
22 namespace art HIDDEN {
23 
24 class 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 
31 // 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.
34 class EscapeVisitor {
35  public:
~EscapeVisitor()36   virtual ~EscapeVisitor() {}
37   virtual bool Visit(HInstruction* escape) = 0;
operator()38   bool operator()(HInstruction* user) {
39     return Visit(user);
40   }
41 };
42 
43 // An explicit EscapeVisitor for lambdas
44 template <typename F>
45 class LambdaEscapeVisitor final : public EscapeVisitor {
46  public:
LambdaEscapeVisitor(F f)47   explicit LambdaEscapeVisitor(F f) : func_(f) {}
Visit(HInstruction * escape)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.
62 class NoEscapeCheck {
63  public:
~NoEscapeCheck()64   virtual ~NoEscapeCheck() {}
65   virtual bool NoEscape(HInstruction* reference, HInstruction* user) = 0;
operator()66   bool operator()(HInstruction* ref, HInstruction* user) {
67     return NoEscape(ref, user);
68   }
69 };
70 
71 // An explicit NoEscapeCheck for use with c++ lambdas.
72 template <typename F>
73 class LambdaNoEscapeCheck final : public NoEscapeCheck {
74  public:
LambdaNoEscapeCheck(F f)75   explicit LambdaNoEscapeCheck(F f) : func_(f) {}
NoEscape(HInstruction * ref,HInstruction * user)76   bool NoEscape(HInstruction* ref, HInstruction* user) override {
77     return func_(ref, user);
78   }
79 
80  private:
81   F func_;
82 };
83 
84 /*
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
89  * 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).
101  *
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  */
109 void CalculateEscape(HInstruction* reference,
110                      NoEscapeCheck& no_escape,
111                      /*out*/ bool* is_singleton,
112                      /*out*/ bool* is_singleton_and_not_returned,
113                      /*out*/ bool* is_singleton_and_not_deopt_visible);
114 
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)115 inline 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  */
135 void VisitEscapes(HInstruction* reference, EscapeVisitor& escape_visitor);
136 
137 /*
138  * Convenience method for testing the singleton and not returned properties at once.
139  * Callers should be aware that this method invokes the full analysis at each call.
140  */
141 bool DoesNotEscape(HInstruction* reference, NoEscapeCheck& no_escape);
142 
DoesNotEscape(HInstruction * reference,bool (* no_escape_fn)(HInstruction *,HInstruction *))143 inline 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 }
148 
149 }  // namespace art
150 
151 #endif  // ART_COMPILER_OPTIMIZING_ESCAPE_H_
152