blob: af067dae735acd83139114bb72a6c4a931f7d88f [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
Vladimír Marko434d9682022-11-04 14:04:17 +000020#include "base/macros.h"
David Sehr9e734c72018-01-04 17:56:19 -080021#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080022#include "dex/invoke_type.h"
Vladimir Markoe9fb3dc2021-03-10 12:17:53 +000023#include "jit/profiling_info.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "optimization.h"
David Sehr82d046e2018-04-23 08:14:19 -070025#include "profile/profile_compilation_info.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026
Vladimír Marko434d9682022-11-04 14:04:17 +000027namespace art HIDDEN {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000028
Vladimir Markodc151b22015-10-15 18:02:30 +010029class CodeGenerator;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000030class DexCompilationUnit;
31class HGraph;
32class HInvoke;
33class OptimizingCompilerStats;
34
35class HInliner : public HOptimization {
36 public:
37 HInliner(HGraph* outer_graph,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010038 HGraph* outermost_graph,
Vladimir Markodc151b22015-10-15 18:02:30 +010039 CodeGenerator* codegen,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000040 const DexCompilationUnit& outer_compilation_unit,
Nicolas Geoffray9437b782015-03-25 10:08:51 +000041 const DexCompilationUnit& caller_compilation_unit,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000042 OptimizingCompilerStats* stats,
Nicolas Geoffray5949fa02015-12-18 10:57:10 +000043 size_t total_number_of_dex_registers,
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +000044 size_t total_number_of_instructions,
45 HInliner* parent,
Santiago Aboy Solanes8efb1a62022-06-24 11:16:35 +010046 size_t depth,
47 bool try_catch_inlining_allowed,
Aart Bik2ca10eb2017-11-15 15:17:53 -080048 const char* name = kInlinerPassName)
49 : HOptimization(outer_graph, name, stats),
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010050 outermost_graph_(outermost_graph),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000051 outer_compilation_unit_(outer_compilation_unit),
Nicolas Geoffray9437b782015-03-25 10:08:51 +000052 caller_compilation_unit_(caller_compilation_unit),
Vladimir Markodc151b22015-10-15 18:02:30 +010053 codegen_(codegen),
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),
Santiago Aboy Solanes8efb1a62022-06-24 11:16:35 +010059 try_catch_inlining_allowed_(try_catch_inlining_allowed),
Vladimir Marko438709f2017-02-23 18:56:13 +000060 inline_stats_(nullptr) {}
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000061
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010062 bool Run() override;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000063
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
Santiago Aboy Solanes8ed39492022-07-20 11:13:39 +010076 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
Nicolas Geoffraye1e0e0f2021-04-29 08:57:13 +000080 // method will replace and remove the `invoke_instruction`.
Mingyao Yang063fc772016-08-02 11:02:54 -070081 bool TryInlineAndReplace(HInvoke* invoke_instruction,
82 ArtMethod* resolved_method,
Nicolas Geoffray0f001b72017-01-04 16:46:23 +000083 ReferenceTypeInfo receiver_type,
Santiago Aboy Solanes629c0652022-10-06 20:31:46 +010084 bool do_rtp,
85 bool is_speculative)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070086 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010087
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000088 bool TryBuildAndInline(HInvoke* invoke_instruction,
89 ArtMethod* resolved_method,
Nicolas Geoffray0f001b72017-01-04 16:46:23 +000090 ReferenceTypeInfo receiver_type,
Santiago Aboy Solanes629c0652022-10-06 20:31:46 +010091 HInstruction** return_replacement,
92 bool is_speculative)
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,
Santiago Aboy Solanes629c0652022-10-06 20:31:46 +010098 HInstruction** return_replacement,
99 bool is_speculative)
100 REQUIRES_SHARED(Locks::mutator_lock_);
Eric Holk1868de92020-02-12 09:10:21 -0800101
102 // Substitutes parameters in the callee graph with their values from the caller.
103 void SubstituteArguments(HGraph* callee_graph,
104 HInvoke* invoke_instruction,
105 ReferenceTypeInfo receiver_type,
106 const DexCompilationUnit& dex_compilation_unit)
107 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000108
Roland Levillaina3aef2e2016-04-06 17:45:58 +0100109 // Run simple optimizations on `callee_graph`.
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000110 void RunOptimizations(HGraph* callee_graph,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800111 const dex::CodeItem* code_item,
Santiago Aboy Solanes8efb1a62022-06-24 11:16:35 +0100112 const DexCompilationUnit& dex_compilation_unit,
113 bool try_catch_inlining_allowed_for_recursive_inline)
114 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillaina3aef2e2016-04-06 17:45:58 +0100115
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000116 // Try to recognize known simple patterns and replace invoke call with appropriate instructions.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000117 bool TryPatternSubstitution(HInvoke* invoke_instruction,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +0100118 ArtMethod* method,
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000119 HInstruction** return_replacement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700120 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000121
Eric Holk1868de92020-02-12 09:10:21 -0800122 // Returns whether inlining is allowed based on ART semantics.
123 bool IsInliningAllowed(art::ArtMethod* method, const CodeItemDataAccessor& accessor) const
124 REQUIRES_SHARED(Locks::mutator_lock_);
125
126
127 // Returns whether ART supports inlining this method.
128 //
129 // Some methods are not supported because they have features for which inlining
130 // is not implemented. For example, we do not currently support inlining throw
131 // instructions into a try block.
132 bool IsInliningSupported(const HInvoke* invoke_instruction,
133 art::ArtMethod* method,
134 const CodeItemDataAccessor& accessor) const
135 REQUIRES_SHARED(Locks::mutator_lock_);
136
Santiago Aboy Solanesa669df32022-10-31 11:27:24 +0000137 // Returns whether inlining is encouraged.
Eric Holk1868de92020-02-12 09:10:21 -0800138 //
139 // For example, this checks whether the function has grown too large and
140 // inlining should be prevented.
Santiago Aboy Solanesa669df32022-10-31 11:27:24 +0000141 bool IsInliningEncouraged(const HInvoke* invoke_instruction,
142 art::ArtMethod* method,
143 const CodeItemDataAccessor& accessor) const
144 REQUIRES_SHARED(Locks::mutator_lock_);
Eric Holk1868de92020-02-12 09:10:21 -0800145
146 // Inspects the body of a method (callee_graph) and returns whether it can be
147 // inlined.
148 //
149 // This checks for instructions and constructs that we do not support
150 // inlining, such as inlining a throw instruction into a try block.
151 bool CanInlineBody(const HGraph* callee_graph,
Santiago Aboy Solanes8ed39492022-07-20 11:13:39 +0100152 HInvoke* invoke,
Santiago Aboy Solanes629c0652022-10-06 20:31:46 +0100153 size_t* out_number_of_instructions,
154 bool is_speculative) const
Eric Holk1868de92020-02-12 09:10:21 -0800155 REQUIRES_SHARED(Locks::mutator_lock_);
156
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000157 // Create a new HInstanceFieldGet.
Vladimir Markof44d36c2017-03-14 14:18:46 +0000158 HInstanceFieldGet* CreateInstanceFieldGet(uint32_t field_index,
159 ArtMethod* referrer,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000160 HInstruction* obj);
161 // Create a new HInstanceFieldSet.
Vladimir Markof44d36c2017-03-14 14:18:46 +0000162 HInstanceFieldSet* CreateInstanceFieldSet(uint32_t field_index,
163 ArtMethod* referrer,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000164 HInstruction* obj,
Vladimir Markof44d36c2017-03-14 14:18:46 +0000165 HInstruction* value,
166 bool* is_final = nullptr);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000167
Calin Juravle13439f02017-02-21 01:17:21 -0800168 // Try inlining the invoke instruction using inline caches.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +0100169 bool TryInlineFromInlineCache(HInvoke* invoke_instruction)
Calin Juravle13439f02017-02-21 01:17:21 -0800170 REQUIRES_SHARED(Locks::mutator_lock_);
171
Nicolas Geoffraye1e0e0f2021-04-29 08:57:13 +0000172 // Try inlining the invoke instruction using CHA.
173 bool TryInlineFromCHA(HInvoke* invoke_instruction)
174 REQUIRES_SHARED(Locks::mutator_lock_);
175
176 // When we fail inlining `invoke_instruction`, we will try to devirtualize the
177 // call.
178 bool TryDevirtualize(HInvoke* invoke_instruction,
179 ArtMethod* method,
180 HInvoke** replacement)
181 REQUIRES_SHARED(Locks::mutator_lock_);
182
Calin Juravle13439f02017-02-21 01:17:21 -0800183 // Try getting the inline cache from JIT code cache.
184 // Return true if the inline cache was successfully allocated and the
185 // invoke info was found in the profile info.
186 InlineCacheType GetInlineCacheJIT(
187 HInvoke* invoke_instruction,
Vladimir Markoe9fb3dc2021-03-10 12:17:53 +0000188 /*out*/StackHandleScope<InlineCache::kIndividualCacheSize>* classes)
Calin Juravle13439f02017-02-21 01:17:21 -0800189 REQUIRES_SHARED(Locks::mutator_lock_);
190
191 // Try getting the inline cache from AOT offline profile.
192 // Return true if the inline cache was successfully allocated and the
193 // invoke info was found in the profile info.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +0100194 InlineCacheType GetInlineCacheAOT(
Calin Juravle13439f02017-02-21 01:17:21 -0800195 HInvoke* invoke_instruction,
Vladimir Markoe9fb3dc2021-03-10 12:17:53 +0000196 /*out*/StackHandleScope<InlineCache::kIndividualCacheSize>* classes)
Calin Juravle13439f02017-02-21 01:17:21 -0800197 REQUIRES_SHARED(Locks::mutator_lock_);
198
Calin Juravle13439f02017-02-21 01:17:21 -0800199 // Compute the inline cache type.
Vladimir Markoe9fb3dc2021-03-10 12:17:53 +0000200 static InlineCacheType GetInlineCacheType(
201 const StackHandleScope<InlineCache::kIndividualCacheSize>& classes)
Calin Juravle13439f02017-02-21 01:17:21 -0800202 REQUIRES_SHARED(Locks::mutator_lock_);
203
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100204 // Try to inline the target of a monomorphic call. If successful, the code
205 // in the graph will look like:
206 // if (receiver.getClass() != ic.GetMonomorphicType()) deopt
207 // ... // inlined code
208 bool TryInlineMonomorphicCall(HInvoke* invoke_instruction,
Vladimir Markoe9fb3dc2021-03-10 12:17:53 +0000209 const StackHandleScope<InlineCache::kIndividualCacheSize>& classes)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100211
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000212 // Try to inline targets of a polymorphic call.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100213 bool TryInlinePolymorphicCall(HInvoke* invoke_instruction,
Vladimir Markoe9fb3dc2021-03-10 12:17:53 +0000214 const StackHandleScope<InlineCache::kIndividualCacheSize>& classes)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100216
Vladimir Markoe9fb3dc2021-03-10 12:17:53 +0000217 bool TryInlinePolymorphicCallToSameTarget(
218 HInvoke* invoke_instruction,
219 const StackHandleScope<InlineCache::kIndividualCacheSize>& classes)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000221
Calin Juravleaf44e6c2017-05-23 14:24:55 -0700222 // Returns whether or not we should use only polymorphic inlining with no deoptimizations.
223 bool UseOnlyPolymorphicInliningWithNoDeopt();
224
Mingyao Yang063fc772016-08-02 11:02:54 -0700225 // Try CHA-based devirtualization to change virtual method calls into
226 // direct calls.
227 // Returns the actual method that resolved_method can be devirtualized to.
Nicolas Geoffraye1e0e0f2021-04-29 08:57:13 +0000228 ArtMethod* FindMethodFromCHA(ArtMethod* resolved_method)
Mingyao Yang063fc772016-08-02 11:02:54 -0700229 REQUIRES_SHARED(Locks::mutator_lock_);
230
231 // Add a CHA guard for a CHA-based devirtualized call. A CHA guard checks a
232 // should_deoptimize flag and if it's true, does deoptimization.
233 void AddCHAGuard(HInstruction* invoke_instruction,
234 uint32_t dex_pc,
235 HInstruction* cursor,
236 HBasicBlock* bb_cursor);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000237
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000238 HInstanceFieldGet* BuildGetReceiverClass(ClassLinker* class_linker,
239 HInstruction* receiver,
240 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000242
Nicolas Geoffraye1e0e0f2021-04-29 08:57:13 +0000243 void MaybeRunReferenceTypePropagation(HInstruction* replacement,
244 HInvoke* invoke_instruction)
245 REQUIRES_SHARED(Locks::mutator_lock_);
246
David Brazdil94ab38f2016-06-21 17:48:19 +0100247 void FixUpReturnReferenceType(ArtMethod* resolved_method, HInstruction* return_replacement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700248 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdil94ab38f2016-06-21 17:48:19 +0100249
David Brazdil94ab38f2016-06-21 17:48:19 +0100250 bool ArgumentTypesMoreSpecific(HInvoke* invoke_instruction, ArtMethod* resolved_method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdil94ab38f2016-06-21 17:48:19 +0100252
Nicolas Geoffraye1e0e0f2021-04-29 08:57:13 +0000253 bool ReturnTypeMoreSpecific(HInstruction* return_replacement, HInvoke* invoke_instruction)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000255
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000256 // Add a type guard on the given `receiver`. This will add to the graph:
257 // i0 = HFieldGet(receiver, klass)
258 // i1 = HLoadClass(class_index, is_referrer)
259 // i2 = HNotEqual(i0, i1)
260 //
261 // And if `with_deoptimization` is true:
262 // HDeoptimize(i2)
263 //
264 // The method returns the `HNotEqual`, that will be used for polymorphic inlining.
265 HInstruction* AddTypeGuard(HInstruction* receiver,
266 HInstruction* cursor,
267 HBasicBlock* bb_cursor,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800268 dex::TypeIndex class_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000269 Handle<mirror::Class> klass,
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000270 HInstruction* invoke_instruction,
271 bool with_deoptimization)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700272 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000273
274 /*
275 * Ad-hoc implementation for implementing a diamond pattern in the graph for
276 * polymorphic inlining:
277 * 1) `compare` becomes the input of the new `HIf`.
278 * 2) Everything up until `invoke_instruction` is in the then branch (could
279 * contain multiple blocks).
280 * 3) `invoke_instruction` is moved to the otherwise block.
281 * 4) If `return_replacement` is not null, the merge block will have
282 * a phi whose inputs are `return_replacement` and `invoke_instruction`.
283 *
284 * Before:
285 * Block1
286 * compare
287 * ...
288 * invoke_instruction
289 *
290 * After:
291 * Block1
292 * compare
293 * if
294 * / \
295 * / \
296 * Then block Otherwise block
297 * ... invoke_instruction
298 * \ /
299 * \ /
300 * Merge block
301 * phi(return_replacement, invoke_instruction)
302 */
303 void CreateDiamondPatternForPolymorphicInline(HInstruction* compare,
304 HInstruction* return_replacement,
305 HInstruction* invoke_instruction);
306
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000307 // Update the inlining budget based on `total_number_of_instructions_`.
308 void UpdateInliningBudget();
309
310 // Count the number of calls of `method` being inlined recursively.
311 size_t CountRecursiveCallsOf(ArtMethod* method) const;
312
313 // Pretty-print for spaces during logging.
314 std::string DepthString(int line) const;
315
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100316 HGraph* const outermost_graph_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000317 const DexCompilationUnit& outer_compilation_unit_;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000318 const DexCompilationUnit& caller_compilation_unit_;
Vladimir Markodc151b22015-10-15 18:02:30 +0100319 CodeGenerator* const codegen_;
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000320 const size_t total_number_of_dex_registers_;
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000321 size_t total_number_of_instructions_;
322
323 // The 'parent' inliner, that means the inlinigng optimization that requested
324 // `graph_` to be inlined.
325 const HInliner* const parent_;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000326 const size_t depth_;
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000327
328 // The budget left for inlining, in number of instructions.
329 size_t inlining_budget_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000330
Santiago Aboy Solanes8efb1a62022-06-24 11:16:35 +0100331 // States if we are allowing try catch inlining to occur at this particular instance of inlining.
332 bool try_catch_inlining_allowed_;
333
Vladimir Marko438709f2017-02-23 18:56:13 +0000334 // Used to record stats about optimizations on the inlined graph.
335 // If the inlining is successful, these stats are merged to the caller graph's stats.
336 OptimizingCompilerStats* inline_stats_;
337
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000338 DISALLOW_COPY_AND_ASSIGN(HInliner);
339};
340
341} // namespace art
342
343#endif // ART_COMPILER_OPTIMIZING_INLINER_H_