blob: df98a1cf259c69d07bd6371a5bb8658f9e2b96db [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"
22#include "optimization.h"
23
24namespace art {
25
Vladimir Markodc151b22015-10-15 18:02:30 +010026class CodeGenerator;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000027class CompilerDriver;
28class DexCompilationUnit;
29class HGraph;
30class HInvoke;
31class OptimizingCompilerStats;
32
33class HInliner : public HOptimization {
34 public:
35 HInliner(HGraph* outer_graph,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010036 HGraph* outermost_graph,
Vladimir Markodc151b22015-10-15 18:02:30 +010037 CodeGenerator* codegen,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000038 const DexCompilationUnit& outer_compilation_unit,
Nicolas Geoffray9437b782015-03-25 10:08:51 +000039 const DexCompilationUnit& caller_compilation_unit,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000040 CompilerDriver* compiler_driver,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070041 VariableSizedHandleScope* handles,
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,
44 size_t depth)
David Brazdil69ba7b72015-06-23 18:27:30 +010045 : HOptimization(outer_graph, kInlinerPassName, stats),
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010046 outermost_graph_(outermost_graph),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000047 outer_compilation_unit_(outer_compilation_unit),
Nicolas Geoffray9437b782015-03-25 10:08:51 +000048 caller_compilation_unit_(caller_compilation_unit),
Vladimir Markodc151b22015-10-15 18:02:30 +010049 codegen_(codegen),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000050 compiler_driver_(compiler_driver),
Nicolas Geoffray5949fa02015-12-18 10:57:10 +000051 total_number_of_dex_registers_(total_number_of_dex_registers),
Nicolas Geoffray454a4812015-06-09 10:37:32 +010052 depth_(depth),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -070053 number_of_inlined_instructions_(0),
Vladimir Marko438709f2017-02-23 18:56:13 +000054 handles_(handles),
55 inline_stats_(nullptr) {}
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000056
57 void Run() OVERRIDE;
58
Andreas Gampe7c3952f2015-02-19 18:21:24 -080059 static constexpr const char* kInlinerPassName = "inliner";
60
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000061 private:
Nicolas Geoffraye418dda2015-08-11 20:03:09 -070062 bool TryInline(HInvoke* invoke_instruction);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010063
64 // Try to inline `resolved_method` in place of `invoke_instruction`. `do_rtp` is whether
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000065 // reference type propagation can run after the inlining. If the inlining is successful, this
Mingyao Yang063fc772016-08-02 11:02:54 -070066 // method will replace and remove the `invoke_instruction`. If `cha_devirtualize` is true,
67 // a CHA guard needs to be added for the inlining.
68 bool TryInlineAndReplace(HInvoke* invoke_instruction,
69 ArtMethod* resolved_method,
Nicolas Geoffray0f001b72017-01-04 16:46:23 +000070 ReferenceTypeInfo receiver_type,
Mingyao Yang063fc772016-08-02 11:02:54 -070071 bool do_rtp,
72 bool cha_devirtualize)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070073 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010074
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000075 bool TryBuildAndInline(HInvoke* invoke_instruction,
76 ArtMethod* resolved_method,
Nicolas Geoffray0f001b72017-01-04 16:46:23 +000077 ReferenceTypeInfo receiver_type,
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000078 HInstruction** return_replacement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070079 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000080
81 bool TryBuildAndInlineHelper(HInvoke* invoke_instruction,
82 ArtMethod* resolved_method,
Nicolas Geoffray0f001b72017-01-04 16:46:23 +000083 ReferenceTypeInfo receiver_type,
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000084 bool same_dex_file,
85 HInstruction** return_replacement);
86
Roland Levillaina3aef2e2016-04-06 17:45:58 +010087 // Run simple optimizations on `callee_graph`.
88 // Returns the number of inlined instructions.
89 size_t RunOptimizations(HGraph* callee_graph,
90 const DexFile::CodeItem* code_item,
91 const DexCompilationUnit& dex_compilation_unit);
92
Vladimir Markobe10e8e2016-01-22 12:09:44 +000093 // Try to recognize known simple patterns and replace invoke call with appropriate instructions.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000094 bool TryPatternSubstitution(HInvoke* invoke_instruction,
95 ArtMethod* resolved_method,
96 HInstruction** return_replacement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070097 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markobe10e8e2016-01-22 12:09:44 +000098
99 // Create a new HInstanceFieldGet.
Vladimir Marko1aea3512016-12-08 11:39:42 +0000100 HInstanceFieldGet* CreateInstanceFieldGet(uint32_t field_index,
101 ArtMethod* referrer,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000102 HInstruction* obj);
103 // Create a new HInstanceFieldSet.
Vladimir Marko1aea3512016-12-08 11:39:42 +0000104 HInstanceFieldSet* CreateInstanceFieldSet(uint32_t field_index,
105 ArtMethod* referrer,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000106 HInstruction* obj,
Vladimir Marko1aea3512016-12-08 11:39:42 +0000107 HInstruction* value,
108 bool* is_final = nullptr);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000109
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100110 // Try to inline the target of a monomorphic call. If successful, the code
111 // in the graph will look like:
112 // if (receiver.getClass() != ic.GetMonomorphicType()) deopt
113 // ... // inlined code
114 bool TryInlineMonomorphicCall(HInvoke* invoke_instruction,
115 ArtMethod* resolved_method,
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000116 Handle<mirror::ObjectArray<mirror::Class>> classes)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700117 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100118
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000119 // Try to inline targets of a polymorphic call.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100120 bool TryInlinePolymorphicCall(HInvoke* invoke_instruction,
121 ArtMethod* resolved_method,
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000122 Handle<mirror::ObjectArray<mirror::Class>> classes)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700123 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100124
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000125 bool TryInlinePolymorphicCallToSameTarget(HInvoke* invoke_instruction,
126 ArtMethod* resolved_method,
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000127 Handle<mirror::ObjectArray<mirror::Class>> classes)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700128 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000129
Mingyao Yang063fc772016-08-02 11:02:54 -0700130 // Try CHA-based devirtualization to change virtual method calls into
131 // direct calls.
132 // Returns the actual method that resolved_method can be devirtualized to.
133 ArtMethod* TryCHADevirtualization(ArtMethod* resolved_method)
134 REQUIRES_SHARED(Locks::mutator_lock_);
135
136 // Add a CHA guard for a CHA-based devirtualized call. A CHA guard checks a
137 // should_deoptimize flag and if it's true, does deoptimization.
138 void AddCHAGuard(HInstruction* invoke_instruction,
139 uint32_t dex_pc,
140 HInstruction* cursor,
141 HBasicBlock* bb_cursor);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000142
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000143 HInstanceFieldGet* BuildGetReceiverClass(ClassLinker* class_linker,
144 HInstruction* receiver,
145 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700146 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000147
David Brazdil94ab38f2016-06-21 17:48:19 +0100148 void FixUpReturnReferenceType(ArtMethod* resolved_method, HInstruction* return_replacement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700149 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdil94ab38f2016-06-21 17:48:19 +0100150
151 // Creates an instance of ReferenceTypeInfo from `klass` if `klass` is
152 // admissible (see ReferenceTypePropagation::IsAdmissible for details).
153 // Otherwise returns inexact Object RTI.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700154 ReferenceTypeInfo GetClassRTI(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdil94ab38f2016-06-21 17:48:19 +0100155
156 bool ArgumentTypesMoreSpecific(HInvoke* invoke_instruction, ArtMethod* resolved_method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700157 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdil94ab38f2016-06-21 17:48:19 +0100158
159 bool ReturnTypeMoreSpecific(HInvoke* invoke_instruction, HInstruction* return_replacement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700160 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000161
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000162 // Add a type guard on the given `receiver`. This will add to the graph:
163 // i0 = HFieldGet(receiver, klass)
164 // i1 = HLoadClass(class_index, is_referrer)
165 // i2 = HNotEqual(i0, i1)
166 //
167 // And if `with_deoptimization` is true:
168 // HDeoptimize(i2)
169 //
170 // The method returns the `HNotEqual`, that will be used for polymorphic inlining.
171 HInstruction* AddTypeGuard(HInstruction* receiver,
172 HInstruction* cursor,
173 HBasicBlock* bb_cursor,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800174 dex::TypeIndex class_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000175 Handle<mirror::Class> klass,
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000176 HInstruction* invoke_instruction,
177 bool with_deoptimization)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700178 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000179
180 /*
181 * Ad-hoc implementation for implementing a diamond pattern in the graph for
182 * polymorphic inlining:
183 * 1) `compare` becomes the input of the new `HIf`.
184 * 2) Everything up until `invoke_instruction` is in the then branch (could
185 * contain multiple blocks).
186 * 3) `invoke_instruction` is moved to the otherwise block.
187 * 4) If `return_replacement` is not null, the merge block will have
188 * a phi whose inputs are `return_replacement` and `invoke_instruction`.
189 *
190 * Before:
191 * Block1
192 * compare
193 * ...
194 * invoke_instruction
195 *
196 * After:
197 * Block1
198 * compare
199 * if
200 * / \
201 * / \
202 * Then block Otherwise block
203 * ... invoke_instruction
204 * \ /
205 * \ /
206 * Merge block
207 * phi(return_replacement, invoke_instruction)
208 */
209 void CreateDiamondPatternForPolymorphicInline(HInstruction* compare,
210 HInstruction* return_replacement,
211 HInstruction* invoke_instruction);
212
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100213 HGraph* const outermost_graph_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000214 const DexCompilationUnit& outer_compilation_unit_;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000215 const DexCompilationUnit& caller_compilation_unit_;
Vladimir Markodc151b22015-10-15 18:02:30 +0100216 CodeGenerator* const codegen_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000217 CompilerDriver* const compiler_driver_;
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000218 const size_t total_number_of_dex_registers_;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000219 const size_t depth_;
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700220 size_t number_of_inlined_instructions_;
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700221 VariableSizedHandleScope* const handles_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000222
Vladimir Marko438709f2017-02-23 18:56:13 +0000223 // Used to record stats about optimizations on the inlined graph.
224 // If the inlining is successful, these stats are merged to the caller graph's stats.
225 OptimizingCompilerStats* inline_stats_;
226
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000227 DISALLOW_COPY_AND_ASSIGN(HInliner);
228};
229
230} // namespace art
231
232#endif // ART_COMPILER_OPTIMIZING_INLINER_H_