blob: 0127d551926a5a310dbfb071ab82789091f06156 [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
64 // reference type propagation can run after the inlining.
65 bool TryInline(HInvoke* invoke_instruction, ArtMethod* resolved_method, bool do_rtp = true)
66 SHARED_REQUIRES(Locks::mutator_lock_);
67
Vladimir Markobe10e8e2016-01-22 12:09:44 +000068 // Try to recognize known simple patterns and replace invoke call with appropriate instructions.
69 bool TryPatternSubstitution(HInvoke* invoke_instruction, ArtMethod* resolved_method, bool do_rtp)
70 SHARED_REQUIRES(Locks::mutator_lock_);
71
72 // Create a new HInstanceFieldGet.
73 HInstanceFieldGet* CreateInstanceFieldGet(ArtMethod* resolved_method,
74 uint32_t field_index,
75 HInstruction* obj);
76 // Create a new HInstanceFieldSet.
77 HInstanceFieldSet* CreateInstanceFieldSet(ArtMethod* resolved_method,
78 uint32_t field_index,
79 HInstruction* obj,
80 HInstruction* value);
81
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010082 // Try to inline the target of a monomorphic call. If successful, the code
83 // in the graph will look like:
84 // if (receiver.getClass() != ic.GetMonomorphicType()) deopt
85 // ... // inlined code
86 bool TryInlineMonomorphicCall(HInvoke* invoke_instruction,
87 ArtMethod* resolved_method,
88 const InlineCache& ic)
89 SHARED_REQUIRES(Locks::mutator_lock_);
90
91 // Try to inline targets of a polymorphic call. Currently unimplemented.
92 bool TryInlinePolymorphicCall(HInvoke* invoke_instruction,
93 ArtMethod* resolved_method,
94 const InlineCache& ic)
95 SHARED_REQUIRES(Locks::mutator_lock_);
96
Mathieu Chartiere401d142015-04-22 13:56:20 -070097 bool TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +000098 HInvoke* invoke_instruction,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010099 bool same_dex_file,
100 bool do_rtp = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000101
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000102 HInstanceFieldGet* BuildGetReceiverClass(ClassLinker* class_linker,
103 HInstruction* receiver,
104 uint32_t dex_pc) const
105 SHARED_REQUIRES(Locks::mutator_lock_);
106
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000107 void FixUpReturnReferenceType(ArtMethod* resolved_method,
108 HInvoke* invoke_instruction,
109 HInstruction* return_replacement,
110 bool do_rtp)
111 SHARED_REQUIRES(Locks::mutator_lock_);
112
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100113 HGraph* const outermost_graph_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000114 const DexCompilationUnit& outer_compilation_unit_;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000115 const DexCompilationUnit& caller_compilation_unit_;
Vladimir Markodc151b22015-10-15 18:02:30 +0100116 CodeGenerator* const codegen_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000117 CompilerDriver* const compiler_driver_;
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000118 const size_t total_number_of_dex_registers_;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000119 const size_t depth_;
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700120 size_t number_of_inlined_instructions_;
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100121 StackHandleScopeCollection* const handles_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000122
123 DISALLOW_COPY_AND_ASSIGN(HInliner);
124};
125
126} // namespace art
127
128#endif // ART_COMPILER_OPTIMIZING_INLINER_H_