Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1 | /* |
| 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 Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 20 | #include "dex_file_types.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 21 | #include "invoke_type.h" |
| 22 | #include "optimization.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 26 | class CodeGenerator; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 27 | class CompilerDriver; |
| 28 | class DexCompilationUnit; |
| 29 | class HGraph; |
| 30 | class HInvoke; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 31 | class InlineCache; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 32 | class OptimizingCompilerStats; |
| 33 | |
| 34 | class HInliner : public HOptimization { |
| 35 | public: |
| 36 | HInliner(HGraph* outer_graph, |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 37 | HGraph* outermost_graph, |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 38 | CodeGenerator* codegen, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 39 | const DexCompilationUnit& outer_compilation_unit, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 40 | const DexCompilationUnit& caller_compilation_unit, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 41 | CompilerDriver* compiler_driver, |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 42 | VariableSizedHandleScope* handles, |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 43 | OptimizingCompilerStats* stats, |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 44 | size_t total_number_of_dex_registers, |
| 45 | size_t depth) |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 46 | : HOptimization(outer_graph, kInlinerPassName, stats), |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 47 | outermost_graph_(outermost_graph), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 48 | outer_compilation_unit_(outer_compilation_unit), |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 49 | caller_compilation_unit_(caller_compilation_unit), |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 50 | codegen_(codegen), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 51 | compiler_driver_(compiler_driver), |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 52 | total_number_of_dex_registers_(total_number_of_dex_registers), |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 53 | depth_(depth), |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 54 | number_of_inlined_instructions_(0), |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 55 | handles_(handles) {} |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 56 | |
| 57 | void Run() OVERRIDE; |
| 58 | |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 59 | static constexpr const char* kInlinerPassName = "inliner"; |
| 60 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 61 | private: |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 62 | bool TryInline(HInvoke* invoke_instruction); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 63 | |
| 64 | // Try to inline `resolved_method` in place of `invoke_instruction`. `do_rtp` is whether |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 65 | // 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 Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 68 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 69 | |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 70 | bool TryBuildAndInline(HInvoke* invoke_instruction, |
| 71 | ArtMethod* resolved_method, |
| 72 | HInstruction** return_replacement) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 73 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 74 | |
| 75 | bool TryBuildAndInlineHelper(HInvoke* invoke_instruction, |
| 76 | ArtMethod* resolved_method, |
| 77 | bool same_dex_file, |
| 78 | HInstruction** return_replacement); |
| 79 | |
Roland Levillain | a3aef2e | 2016-04-06 17:45:58 +0100 | [diff] [blame] | 80 | // 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 Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 86 | // Try to recognize known simple patterns and replace invoke call with appropriate instructions. |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 87 | bool TryPatternSubstitution(HInvoke* invoke_instruction, |
| 88 | ArtMethod* resolved_method, |
| 89 | HInstruction** return_replacement) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 90 | REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 91 | |
| 92 | // Create a new HInstanceFieldGet. |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 93 | HInstanceFieldGet* CreateInstanceFieldGet(Handle<mirror::DexCache> dex_cache, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 94 | uint32_t field_index, |
| 95 | HInstruction* obj); |
| 96 | // Create a new HInstanceFieldSet. |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 97 | HInstanceFieldSet* CreateInstanceFieldSet(Handle<mirror::DexCache> dex_cache, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 98 | uint32_t field_index, |
| 99 | HInstruction* obj, |
| 100 | HInstruction* value); |
| 101 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 102 | // 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 Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 109 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 110 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 111 | // Try to inline targets of a polymorphic call. |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 112 | bool TryInlinePolymorphicCall(HInvoke* invoke_instruction, |
| 113 | ArtMethod* resolved_method, |
| 114 | const InlineCache& ic) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 115 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 116 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 117 | bool TryInlinePolymorphicCallToSameTarget(HInvoke* invoke_instruction, |
| 118 | ArtMethod* resolved_method, |
| 119 | const InlineCache& ic) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 120 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 121 | |
| 122 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 123 | HInstanceFieldGet* BuildGetReceiverClass(ClassLinker* class_linker, |
| 124 | HInstruction* receiver, |
| 125 | uint32_t dex_pc) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 126 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 127 | |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 128 | void FixUpReturnReferenceType(ArtMethod* resolved_method, HInstruction* return_replacement) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 129 | REQUIRES_SHARED(Locks::mutator_lock_); |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 130 | |
| 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 Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 134 | ReferenceTypeInfo GetClassRTI(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_); |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 135 | |
| 136 | bool ArgumentTypesMoreSpecific(HInvoke* invoke_instruction, ArtMethod* resolved_method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 137 | REQUIRES_SHARED(Locks::mutator_lock_); |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 138 | |
| 139 | bool ReturnTypeMoreSpecific(HInvoke* invoke_instruction, HInstruction* return_replacement) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 140 | REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 141 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 142 | // 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 Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 154 | dex::TypeIndex class_index, |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 155 | bool is_referrer, |
| 156 | HInstruction* invoke_instruction, |
| 157 | bool with_deoptimization) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 158 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 159 | |
| 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 Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 193 | HGraph* const outermost_graph_; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 194 | const DexCompilationUnit& outer_compilation_unit_; |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 195 | const DexCompilationUnit& caller_compilation_unit_; |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 196 | CodeGenerator* const codegen_; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 197 | CompilerDriver* const compiler_driver_; |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 198 | const size_t total_number_of_dex_registers_; |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 199 | const size_t depth_; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 200 | size_t number_of_inlined_instructions_; |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 201 | VariableSizedHandleScope* const handles_; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 202 | |
| 203 | DISALLOW_COPY_AND_ASSIGN(HInliner); |
| 204 | }; |
| 205 | |
| 206 | } // namespace art |
| 207 | |
| 208 | #endif // ART_COMPILER_OPTIMIZING_INLINER_H_ |