blob: 7cf1424b6dbf2c2ee3af3b9046c63534b280e9db [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
20#include "invoke_type.h"
21#include "optimization.h"
22
23namespace art {
24
Vladimir Markodc151b22015-10-15 18:02:30 +010025class CodeGenerator;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026class CompilerDriver;
27class DexCompilationUnit;
28class HGraph;
29class HInvoke;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010030class InlineCache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000031class 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,
Nicolas Geoffray454a4812015-06-09 10:37:32 +010041 StackHandleScopeCollection* 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),
Nicolas Geoffray454a4812015-06-09 10:37:32 +010054 handles_(handles) {}
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000055
56 void Run() OVERRIDE;
57
Andreas Gampe7c3952f2015-02-19 18:21:24 -080058 static constexpr const char* kInlinerPassName = "inliner";
59
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000060 private:
Nicolas Geoffraye418dda2015-08-11 20:03:09 -070061 bool TryInline(HInvoke* invoke_instruction);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010062
63 // Try to inline `resolved_method` in place of `invoke_instruction`. `do_rtp` is whether
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000064 // reference type propagation can run after the inlining. If the inlining is successful, this
65 // method will replace and remove the `invoke_instruction`.
66 bool TryInlineAndReplace(HInvoke* invoke_instruction, ArtMethod* resolved_method, bool do_rtp)
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010067 SHARED_REQUIRES(Locks::mutator_lock_);
68
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000069 bool TryBuildAndInline(HInvoke* invoke_instruction,
70 ArtMethod* resolved_method,
71 HInstruction** return_replacement)
72 SHARED_REQUIRES(Locks::mutator_lock_);
73
74 bool TryBuildAndInlineHelper(HInvoke* invoke_instruction,
75 ArtMethod* resolved_method,
76 bool same_dex_file,
77 HInstruction** return_replacement);
78
Roland Levillaina3aef2e2016-04-06 17:45:58 +010079 // Run simple optimizations on `callee_graph`.
80 // Returns the number of inlined instructions.
81 size_t RunOptimizations(HGraph* callee_graph,
82 const DexFile::CodeItem* code_item,
83 const DexCompilationUnit& dex_compilation_unit);
84
Vladimir Markobe10e8e2016-01-22 12:09:44 +000085 // Try to recognize known simple patterns and replace invoke call with appropriate instructions.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +000086 bool TryPatternSubstitution(HInvoke* invoke_instruction,
87 ArtMethod* resolved_method,
88 HInstruction** return_replacement)
Vladimir Markobe10e8e2016-01-22 12:09:44 +000089 SHARED_REQUIRES(Locks::mutator_lock_);
90
91 // Create a new HInstanceFieldGet.
Vladimir Marko354efa62016-02-04 19:46:56 +000092 HInstanceFieldGet* CreateInstanceFieldGet(Handle<mirror::DexCache> dex_cache,
Vladimir Markobe10e8e2016-01-22 12:09:44 +000093 uint32_t field_index,
94 HInstruction* obj);
95 // Create a new HInstanceFieldSet.
Vladimir Marko354efa62016-02-04 19:46:56 +000096 HInstanceFieldSet* CreateInstanceFieldSet(Handle<mirror::DexCache> dex_cache,
Vladimir Markobe10e8e2016-01-22 12:09:44 +000097 uint32_t field_index,
98 HInstruction* obj,
99 HInstruction* value);
100
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100101 // Try to inline the target of a monomorphic call. If successful, the code
102 // in the graph will look like:
103 // if (receiver.getClass() != ic.GetMonomorphicType()) deopt
104 // ... // inlined code
105 bool TryInlineMonomorphicCall(HInvoke* invoke_instruction,
106 ArtMethod* resolved_method,
107 const InlineCache& ic)
108 SHARED_REQUIRES(Locks::mutator_lock_);
109
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000110 // Try to inline targets of a polymorphic call.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100111 bool TryInlinePolymorphicCall(HInvoke* invoke_instruction,
112 ArtMethod* resolved_method,
113 const InlineCache& ic)
114 SHARED_REQUIRES(Locks::mutator_lock_);
115
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000116 bool TryInlinePolymorphicCallToSameTarget(HInvoke* invoke_instruction,
117 ArtMethod* resolved_method,
118 const InlineCache& ic)
119 SHARED_REQUIRES(Locks::mutator_lock_);
120
121
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000122 HInstanceFieldGet* BuildGetReceiverClass(ClassLinker* class_linker,
123 HInstruction* receiver,
124 uint32_t dex_pc) const
125 SHARED_REQUIRES(Locks::mutator_lock_);
126
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000127 void FixUpReturnReferenceType(HInvoke* invoke_instruction,
128 ArtMethod* resolved_method,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000129 HInstruction* return_replacement,
130 bool do_rtp)
131 SHARED_REQUIRES(Locks::mutator_lock_);
132
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000133 // Add a type guard on the given `receiver`. This will add to the graph:
134 // i0 = HFieldGet(receiver, klass)
135 // i1 = HLoadClass(class_index, is_referrer)
136 // i2 = HNotEqual(i0, i1)
137 //
138 // And if `with_deoptimization` is true:
139 // HDeoptimize(i2)
140 //
141 // The method returns the `HNotEqual`, that will be used for polymorphic inlining.
142 HInstruction* AddTypeGuard(HInstruction* receiver,
143 HInstruction* cursor,
144 HBasicBlock* bb_cursor,
145 uint32_t class_index,
146 bool is_referrer,
147 HInstruction* invoke_instruction,
148 bool with_deoptimization)
149 SHARED_REQUIRES(Locks::mutator_lock_);
150
151 /*
152 * Ad-hoc implementation for implementing a diamond pattern in the graph for
153 * polymorphic inlining:
154 * 1) `compare` becomes the input of the new `HIf`.
155 * 2) Everything up until `invoke_instruction` is in the then branch (could
156 * contain multiple blocks).
157 * 3) `invoke_instruction` is moved to the otherwise block.
158 * 4) If `return_replacement` is not null, the merge block will have
159 * a phi whose inputs are `return_replacement` and `invoke_instruction`.
160 *
161 * Before:
162 * Block1
163 * compare
164 * ...
165 * invoke_instruction
166 *
167 * After:
168 * Block1
169 * compare
170 * if
171 * / \
172 * / \
173 * Then block Otherwise block
174 * ... invoke_instruction
175 * \ /
176 * \ /
177 * Merge block
178 * phi(return_replacement, invoke_instruction)
179 */
180 void CreateDiamondPatternForPolymorphicInline(HInstruction* compare,
181 HInstruction* return_replacement,
182 HInstruction* invoke_instruction);
183
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100184 HGraph* const outermost_graph_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000185 const DexCompilationUnit& outer_compilation_unit_;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000186 const DexCompilationUnit& caller_compilation_unit_;
Vladimir Markodc151b22015-10-15 18:02:30 +0100187 CodeGenerator* const codegen_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000188 CompilerDriver* const compiler_driver_;
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000189 const size_t total_number_of_dex_registers_;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000190 const size_t depth_;
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700191 size_t number_of_inlined_instructions_;
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100192 StackHandleScopeCollection* const handles_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000193
194 DISALLOW_COPY_AND_ASSIGN(HInliner);
195};
196
197} // namespace art
198
199#endif // ART_COMPILER_OPTIMIZING_INLINER_H_