blob: c4b3a32d91084205f5c09981650d3226151d1e90 [file] [log] [blame]
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001/*
2 * Copyright (C) 2014 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_INLINER_H_
18#define ART_COMPILER_OPTIMIZING_INLINER_H_
19
Andreas Gampea5b09a62016-11-17 15:21:22 -080020#include "dex_file_types.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000021#include "invoke_type.h"
Calin Juravle13439f02017-02-21 01:17:21 -080022#include "jit/profile_compilation_info.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "optimization.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000024
25namespace art {
26
Vladimir Markodc151b22015-10-15 18:02:30 +010027class CodeGenerator;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000028class CompilerDriver;
29class DexCompilationUnit;
30class HGraph;
31class HInvoke;
32class OptimizingCompilerStats;
33
34class HInliner : public HOptimization {
35 public:
36 HInliner(HGraph* outer_graph,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010037 HGraph* outermost_graph,
Vladimir Markodc151b22015-10-15 18:02:30 +010038 CodeGenerator* codegen,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000039 const DexCompilationUnit& outer_compilation_unit,
Nicolas Geoffray9437b782015-03-25 10:08:51 +000040 const DexCompilationUnit& caller_compilation_unit,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000041 CompilerDriver* compiler_driver,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070042 VariableSizedHandleScope* handles,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000043 OptimizingCompilerStats* stats,
Nicolas Geoffray5949fa02015-12-18 10:57:10 +000044 size_t total_number_of_dex_registers,
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +000045 size_t total_number_of_instructions,
46 HInliner* parent,
47 size_t depth = 0)
David Brazdil69ba7b72015-06-23 18:27:30 +010048 : HOptimization(outer_graph, kInlinerPassName, stats),
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010049 outermost_graph_(outermost_graph),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000050 outer_compilation_unit_(outer_compilation_unit),
Nicolas Geoffray9437b782015-03-25 10:08:51 +000051 caller_compilation_unit_(caller_compilation_unit),
Vladimir Markodc151b22015-10-15 18:02:30 +010052 codegen_(codegen),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053 compiler_driver_(compiler_driver),
Nicolas Geoffray5949fa02015-12-18 10:57:10 +000054 total_number_of_dex_registers_(total_number_of_dex_registers),
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +000055 total_number_of_instructions_(total_number_of_instructions),
56 parent_(parent),
Nicolas Geoffray454a4812015-06-09 10:37:32 +010057 depth_(depth),
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +000058 inlining_budget_(0),
Vladimir Marko438709f2017-02-23 18:56:13 +000059 handles_(handles),
60 inline_stats_(nullptr) {}
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000061
62 void Run() OVERRIDE;
63
Andreas Gampe7c3952f2015-02-19 18:21:24 -080064 static constexpr const char* kInlinerPassName = "inliner";
65
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000066 private:
Calin Juravle13439f02017-02-21 01:17:21 -080067 enum InlineCacheType {
68 kInlineCacheNoData = 0,
69 kInlineCacheUninitialized = 1,
70 kInlineCacheMonomorphic = 2,
71 kInlineCachePolymorphic = 3,
72 kInlineCacheMegamorphic = 4,
73 kInlineCacheMissingTypes = 5
74 };
75
Nicolas Geoffraye418dda2015-08-11 20:03:09 -070076 bool TryInline(HInvoke* invoke_instruction);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010077
78 // Try to inline `resolved_method` in place of `invoke_instruction`. `do_rtp` is whether
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000079 // reference type propagation can run after the inlining. If the inlining is successful, this
Mingyao Yang063fc772016-08-02 11:02:54 -070080 // method will replace and remove the `invoke_instruction`. If `cha_devirtualize` is true,
81 // a CHA guard needs to be added for the inlining.
82 bool TryInlineAndReplace(HInvoke* invoke_instruction,
83 ArtMethod* resolved_method,
Nicolas Geoffray0f001b72017-01-04 16:46:23 +000084 ReferenceTypeInfo receiver_type,
Mingyao Yang063fc772016-08-02 11:02:54 -070085 bool do_rtp,
86 bool cha_devirtualize)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070087 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010088
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000089 bool TryBuildAndInline(HInvoke* invoke_instruction,
90 ArtMethod* resolved_method,
Nicolas Geoffray0f001b72017-01-04 16:46:23 +000091 ReferenceTypeInfo receiver_type,
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000092 HInstruction** return_replacement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070093 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000094
95 bool TryBuildAndInlineHelper(HInvoke* invoke_instruction,
96 ArtMethod* resolved_method,
Nicolas Geoffray0f001b72017-01-04 16:46:23 +000097 ReferenceTypeInfo receiver_type,
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000098 bool same_dex_file,
99 HInstruction** return_replacement);
100
Roland Levillaina3aef2e2016-04-06 17:45:58 +0100101 // Run simple optimizations on `callee_graph`.
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000102 void RunOptimizations(HGraph* callee_graph,
103 const DexFile::CodeItem* code_item,
104 const DexCompilationUnit& dex_compilation_unit)
105 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillaina3aef2e2016-04-06 17:45:58 +0100106
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000107 // Try to recognize known simple patterns and replace invoke call with appropriate instructions.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000108 bool TryPatternSubstitution(HInvoke* invoke_instruction,
109 ArtMethod* resolved_method,
110 HInstruction** return_replacement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700111 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000112
113 // Create a new HInstanceFieldGet.
Vladimir Markof44d36c2017-03-14 14:18:46 +0000114 HInstanceFieldGet* CreateInstanceFieldGet(uint32_t field_index,
115 ArtMethod* referrer,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000116 HInstruction* obj);
117 // Create a new HInstanceFieldSet.
Vladimir Markof44d36c2017-03-14 14:18:46 +0000118 HInstanceFieldSet* CreateInstanceFieldSet(uint32_t field_index,
119 ArtMethod* referrer,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000120 HInstruction* obj,
Vladimir Markof44d36c2017-03-14 14:18:46 +0000121 HInstruction* value,
122 bool* is_final = nullptr);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000123
Calin Juravle13439f02017-02-21 01:17:21 -0800124 // Try inlining the invoke instruction using inline caches.
125 bool TryInlineFromInlineCache(
126 const DexFile& caller_dex_file,
127 HInvoke* invoke_instruction,
128 ArtMethod* resolved_method)
129 REQUIRES_SHARED(Locks::mutator_lock_);
130
131 // Try getting the inline cache from JIT code cache.
132 // Return true if the inline cache was successfully allocated and the
133 // invoke info was found in the profile info.
134 InlineCacheType GetInlineCacheJIT(
135 HInvoke* invoke_instruction,
136 StackHandleScope<1>* hs,
137 /*out*/Handle<mirror::ObjectArray<mirror::Class>>* inline_cache)
138 REQUIRES_SHARED(Locks::mutator_lock_);
139
140 // Try getting the inline cache from AOT offline profile.
141 // Return true if the inline cache was successfully allocated and the
142 // invoke info was found in the profile info.
143 InlineCacheType GetInlineCacheAOT(const DexFile& caller_dex_file,
144 HInvoke* invoke_instruction,
145 StackHandleScope<1>* hs,
146 /*out*/Handle<mirror::ObjectArray<mirror::Class>>* inline_cache)
147 REQUIRES_SHARED(Locks::mutator_lock_);
148
149 // Extract the mirror classes from the offline profile and add them to the `inline_cache`.
150 // Note that even if we have profile data for the invoke the inline_cache might contain
151 // only null entries if the types cannot be resolved.
152 InlineCacheType ExtractClassesFromOfflineProfile(
153 const HInvoke* invoke_instruction,
154 const ProfileCompilationInfo::OfflineProfileMethodInfo& offline_profile,
155 /*out*/Handle<mirror::ObjectArray<mirror::Class>> inline_cache)
156 REQUIRES_SHARED(Locks::mutator_lock_);
157
158 // Compute the inline cache type.
159 InlineCacheType GetInlineCacheType(
160 const Handle<mirror::ObjectArray<mirror::Class>>& classes)
161 REQUIRES_SHARED(Locks::mutator_lock_);
162
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100163 // Try to inline the target of a monomorphic call. If successful, the code
164 // in the graph will look like:
165 // if (receiver.getClass() != ic.GetMonomorphicType()) deopt
166 // ... // inlined code
167 bool TryInlineMonomorphicCall(HInvoke* invoke_instruction,
168 ArtMethod* resolved_method,
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000169 Handle<mirror::ObjectArray<mirror::Class>> classes)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700170 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100171
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000172 // Try to inline targets of a polymorphic call.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100173 bool TryInlinePolymorphicCall(HInvoke* invoke_instruction,
174 ArtMethod* resolved_method,
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000175 Handle<mirror::ObjectArray<mirror::Class>> classes)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700176 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100177
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000178 bool TryInlinePolymorphicCallToSameTarget(HInvoke* invoke_instruction,
179 ArtMethod* resolved_method,
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000180 Handle<mirror::ObjectArray<mirror::Class>> classes)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700181 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000182
Calin Juravleaf44e6c2017-05-23 14:24:55 -0700183 // Returns whether or not we should use only polymorphic inlining with no deoptimizations.
184 bool UseOnlyPolymorphicInliningWithNoDeopt();
185
Mingyao Yang063fc772016-08-02 11:02:54 -0700186 // Try CHA-based devirtualization to change virtual method calls into
187 // direct calls.
188 // Returns the actual method that resolved_method can be devirtualized to.
189 ArtMethod* TryCHADevirtualization(ArtMethod* resolved_method)
190 REQUIRES_SHARED(Locks::mutator_lock_);
191
192 // Add a CHA guard for a CHA-based devirtualized call. A CHA guard checks a
193 // should_deoptimize flag and if it's true, does deoptimization.
194 void AddCHAGuard(HInstruction* invoke_instruction,
195 uint32_t dex_pc,
196 HInstruction* cursor,
197 HBasicBlock* bb_cursor);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000198
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000199 HInstanceFieldGet* BuildGetReceiverClass(ClassLinker* class_linker,
200 HInstruction* receiver,
201 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700202 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000203
David Brazdil94ab38f2016-06-21 17:48:19 +0100204 void FixUpReturnReferenceType(ArtMethod* resolved_method, HInstruction* return_replacement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700205 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdil94ab38f2016-06-21 17:48:19 +0100206
207 // Creates an instance of ReferenceTypeInfo from `klass` if `klass` is
208 // admissible (see ReferenceTypePropagation::IsAdmissible for details).
209 // Otherwise returns inexact Object RTI.
Vladimir Markob45528c2017-07-27 14:14:28 +0100210 ReferenceTypeInfo GetClassRTI(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdil94ab38f2016-06-21 17:48:19 +0100211
212 bool ArgumentTypesMoreSpecific(HInvoke* invoke_instruction, ArtMethod* resolved_method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700213 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdil94ab38f2016-06-21 17:48:19 +0100214
215 bool ReturnTypeMoreSpecific(HInvoke* invoke_instruction, HInstruction* return_replacement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000217
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000218 // Add a type guard on the given `receiver`. This will add to the graph:
219 // i0 = HFieldGet(receiver, klass)
220 // i1 = HLoadClass(class_index, is_referrer)
221 // i2 = HNotEqual(i0, i1)
222 //
223 // And if `with_deoptimization` is true:
224 // HDeoptimize(i2)
225 //
226 // The method returns the `HNotEqual`, that will be used for polymorphic inlining.
227 HInstruction* AddTypeGuard(HInstruction* receiver,
228 HInstruction* cursor,
229 HBasicBlock* bb_cursor,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800230 dex::TypeIndex class_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000231 Handle<mirror::Class> klass,
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000232 HInstruction* invoke_instruction,
233 bool with_deoptimization)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000235
236 /*
237 * Ad-hoc implementation for implementing a diamond pattern in the graph for
238 * polymorphic inlining:
239 * 1) `compare` becomes the input of the new `HIf`.
240 * 2) Everything up until `invoke_instruction` is in the then branch (could
241 * contain multiple blocks).
242 * 3) `invoke_instruction` is moved to the otherwise block.
243 * 4) If `return_replacement` is not null, the merge block will have
244 * a phi whose inputs are `return_replacement` and `invoke_instruction`.
245 *
246 * Before:
247 * Block1
248 * compare
249 * ...
250 * invoke_instruction
251 *
252 * After:
253 * Block1
254 * compare
255 * if
256 * / \
257 * / \
258 * Then block Otherwise block
259 * ... invoke_instruction
260 * \ /
261 * \ /
262 * Merge block
263 * phi(return_replacement, invoke_instruction)
264 */
265 void CreateDiamondPatternForPolymorphicInline(HInstruction* compare,
266 HInstruction* return_replacement,
267 HInstruction* invoke_instruction);
268
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000269 // Update the inlining budget based on `total_number_of_instructions_`.
270 void UpdateInliningBudget();
271
272 // Count the number of calls of `method` being inlined recursively.
273 size_t CountRecursiveCallsOf(ArtMethod* method) const;
274
275 // Pretty-print for spaces during logging.
276 std::string DepthString(int line) const;
277
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100278 HGraph* const outermost_graph_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000279 const DexCompilationUnit& outer_compilation_unit_;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000280 const DexCompilationUnit& caller_compilation_unit_;
Vladimir Markodc151b22015-10-15 18:02:30 +0100281 CodeGenerator* const codegen_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000282 CompilerDriver* const compiler_driver_;
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000283 const size_t total_number_of_dex_registers_;
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000284 size_t total_number_of_instructions_;
285
286 // The 'parent' inliner, that means the inlinigng optimization that requested
287 // `graph_` to be inlined.
288 const HInliner* const parent_;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000289 const size_t depth_;
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000290
291 // The budget left for inlining, in number of instructions.
292 size_t inlining_budget_;
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700293 VariableSizedHandleScope* const handles_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000294
Vladimir Marko438709f2017-02-23 18:56:13 +0000295 // Used to record stats about optimizations on the inlined graph.
296 // If the inlining is successful, these stats are merged to the caller graph's stats.
297 OptimizingCompilerStats* inline_stats_;
298
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000299 DISALLOW_COPY_AND_ASSIGN(HInliner);
300};
301
302} // namespace art
303
304#endif // ART_COMPILER_OPTIMIZING_INLINER_H_