blob: 682393e697f17413310450e974effcb32a9ff9ba [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;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010031class InlineCache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032class 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,
45 size_t depth)
David Brazdil69ba7b72015-06-23 18:27:30 +010046 : HOptimization(outer_graph, kInlinerPassName, stats),
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010047 outermost_graph_(outermost_graph),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000048 outer_compilation_unit_(outer_compilation_unit),
Nicolas Geoffray9437b782015-03-25 10:08:51 +000049 caller_compilation_unit_(caller_compilation_unit),
Vladimir Markodc151b22015-10-15 18:02:30 +010050 codegen_(codegen),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000051 compiler_driver_(compiler_driver),
Nicolas Geoffray5949fa02015-12-18 10:57:10 +000052 total_number_of_dex_registers_(total_number_of_dex_registers),
Nicolas Geoffray454a4812015-06-09 10:37:32 +010053 depth_(depth),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -070054 number_of_inlined_instructions_(0),
Nicolas Geoffray454a4812015-06-09 10:37:32 +010055 handles_(handles) {}
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
66 // method will replace and remove the `invoke_instruction`.
67 bool TryInlineAndReplace(HInvoke* invoke_instruction, ArtMethod* resolved_method, bool do_rtp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070068 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010069
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000070 bool TryBuildAndInline(HInvoke* invoke_instruction,
71 ArtMethod* resolved_method,
72 HInstruction** return_replacement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070073 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000074
75 bool TryBuildAndInlineHelper(HInvoke* invoke_instruction,
76 ArtMethod* resolved_method,
77 bool same_dex_file,
78 HInstruction** return_replacement);
79
Roland Levillaina3aef2e2016-04-06 17:45:58 +010080 // Run simple optimizations on `callee_graph`.
81 // Returns the number of inlined instructions.
82 size_t RunOptimizations(HGraph* callee_graph,
83 const DexFile::CodeItem* code_item,
84 const DexCompilationUnit& dex_compilation_unit);
85
Vladimir Markobe10e8e2016-01-22 12:09:44 +000086 // Try to recognize known simple patterns and replace invoke call with appropriate instructions.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000087 bool TryPatternSubstitution(HInvoke* invoke_instruction,
88 ArtMethod* resolved_method,
89 HInstruction** return_replacement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070090 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markobe10e8e2016-01-22 12:09:44 +000091
92 // Create a new HInstanceFieldGet.
Vladimir Marko354efa62016-02-04 19:46:56 +000093 HInstanceFieldGet* CreateInstanceFieldGet(Handle<mirror::DexCache> dex_cache,
Vladimir Markobe10e8e2016-01-22 12:09:44 +000094 uint32_t field_index,
95 HInstruction* obj);
96 // Create a new HInstanceFieldSet.
Vladimir Marko354efa62016-02-04 19:46:56 +000097 HInstanceFieldSet* CreateInstanceFieldSet(Handle<mirror::DexCache> dex_cache,
Vladimir Markobe10e8e2016-01-22 12:09:44 +000098 uint32_t field_index,
99 HInstruction* obj,
100 HInstruction* value);
101
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100102 // Try to inline the target of a monomorphic call. If successful, the code
103 // in the graph will look like:
104 // if (receiver.getClass() != ic.GetMonomorphicType()) deopt
105 // ... // inlined code
106 bool TryInlineMonomorphicCall(HInvoke* invoke_instruction,
107 ArtMethod* resolved_method,
108 const InlineCache& ic)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700109 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100110
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000111 // Try to inline targets of a polymorphic call.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100112 bool TryInlinePolymorphicCall(HInvoke* invoke_instruction,
113 ArtMethod* resolved_method,
114 const InlineCache& ic)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700115 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100116
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000117 bool TryInlinePolymorphicCallToSameTarget(HInvoke* invoke_instruction,
118 ArtMethod* resolved_method,
119 const InlineCache& ic)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700120 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000121
122
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000123 HInstanceFieldGet* BuildGetReceiverClass(ClassLinker* class_linker,
124 HInstruction* receiver,
125 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700126 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000127
David Brazdil94ab38f2016-06-21 17:48:19 +0100128 void FixUpReturnReferenceType(ArtMethod* resolved_method, HInstruction* return_replacement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700129 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdil94ab38f2016-06-21 17:48:19 +0100130
131 // Creates an instance of ReferenceTypeInfo from `klass` if `klass` is
132 // admissible (see ReferenceTypePropagation::IsAdmissible for details).
133 // Otherwise returns inexact Object RTI.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700134 ReferenceTypeInfo GetClassRTI(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdil94ab38f2016-06-21 17:48:19 +0100135
136 bool ArgumentTypesMoreSpecific(HInvoke* invoke_instruction, ArtMethod* resolved_method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700137 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdil94ab38f2016-06-21 17:48:19 +0100138
139 bool ReturnTypeMoreSpecific(HInvoke* invoke_instruction, HInstruction* return_replacement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700140 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000141
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000142 // Add a type guard on the given `receiver`. This will add to the graph:
143 // i0 = HFieldGet(receiver, klass)
144 // i1 = HLoadClass(class_index, is_referrer)
145 // i2 = HNotEqual(i0, i1)
146 //
147 // And if `with_deoptimization` is true:
148 // HDeoptimize(i2)
149 //
150 // The method returns the `HNotEqual`, that will be used for polymorphic inlining.
151 HInstruction* AddTypeGuard(HInstruction* receiver,
152 HInstruction* cursor,
153 HBasicBlock* bb_cursor,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800154 dex::TypeIndex class_index,
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000155 bool is_referrer,
156 HInstruction* invoke_instruction,
157 bool with_deoptimization)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700158 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000159
160 /*
161 * Ad-hoc implementation for implementing a diamond pattern in the graph for
162 * polymorphic inlining:
163 * 1) `compare` becomes the input of the new `HIf`.
164 * 2) Everything up until `invoke_instruction` is in the then branch (could
165 * contain multiple blocks).
166 * 3) `invoke_instruction` is moved to the otherwise block.
167 * 4) If `return_replacement` is not null, the merge block will have
168 * a phi whose inputs are `return_replacement` and `invoke_instruction`.
169 *
170 * Before:
171 * Block1
172 * compare
173 * ...
174 * invoke_instruction
175 *
176 * After:
177 * Block1
178 * compare
179 * if
180 * / \
181 * / \
182 * Then block Otherwise block
183 * ... invoke_instruction
184 * \ /
185 * \ /
186 * Merge block
187 * phi(return_replacement, invoke_instruction)
188 */
189 void CreateDiamondPatternForPolymorphicInline(HInstruction* compare,
190 HInstruction* return_replacement,
191 HInstruction* invoke_instruction);
192
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100193 HGraph* const outermost_graph_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000194 const DexCompilationUnit& outer_compilation_unit_;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000195 const DexCompilationUnit& caller_compilation_unit_;
Vladimir Markodc151b22015-10-15 18:02:30 +0100196 CodeGenerator* const codegen_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000197 CompilerDriver* const compiler_driver_;
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000198 const size_t total_number_of_dex_registers_;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000199 const size_t depth_;
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700200 size_t number_of_inlined_instructions_;
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700201 VariableSizedHandleScope* const handles_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000202
203 DISALLOW_COPY_AND_ASSIGN(HInliner);
204};
205
206} // namespace art
207
208#endif // ART_COMPILER_OPTIMIZING_INLINER_H_