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; |
| 31 | class OptimizingCompilerStats; |
| 32 | |
| 33 | class HInliner : public HOptimization { |
| 34 | public: |
| 35 | HInliner(HGraph* outer_graph, |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 36 | HGraph* outermost_graph, |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 37 | CodeGenerator* codegen, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 38 | const DexCompilationUnit& outer_compilation_unit, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 39 | const DexCompilationUnit& caller_compilation_unit, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 40 | CompilerDriver* compiler_driver, |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 41 | VariableSizedHandleScope* handles, |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 42 | OptimizingCompilerStats* stats, |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 43 | size_t total_number_of_dex_registers, |
| 44 | size_t depth) |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 45 | : HOptimization(outer_graph, kInlinerPassName, stats), |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 46 | outermost_graph_(outermost_graph), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 47 | outer_compilation_unit_(outer_compilation_unit), |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 48 | caller_compilation_unit_(caller_compilation_unit), |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 49 | codegen_(codegen), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 50 | compiler_driver_(compiler_driver), |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 51 | total_number_of_dex_registers_(total_number_of_dex_registers), |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 52 | depth_(depth), |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 53 | number_of_inlined_instructions_(0), |
Vladimir Marko | 438709f | 2017-02-23 18:56:13 +0000 | [diff] [blame^] | 54 | handles_(handles), |
| 55 | inline_stats_(nullptr) {} |
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 |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 66 | // 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 Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 70 | ReferenceTypeInfo receiver_type, |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 71 | bool do_rtp, |
| 72 | bool cha_devirtualize) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 73 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 74 | |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 75 | bool TryBuildAndInline(HInvoke* invoke_instruction, |
| 76 | ArtMethod* resolved_method, |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 77 | ReferenceTypeInfo receiver_type, |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 78 | HInstruction** return_replacement) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 79 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 80 | |
| 81 | bool TryBuildAndInlineHelper(HInvoke* invoke_instruction, |
| 82 | ArtMethod* resolved_method, |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 83 | ReferenceTypeInfo receiver_type, |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 84 | bool same_dex_file, |
| 85 | HInstruction** return_replacement); |
| 86 | |
Roland Levillain | a3aef2e | 2016-04-06 17:45:58 +0100 | [diff] [blame] | 87 | // 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 Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 93 | // 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] | 94 | bool TryPatternSubstitution(HInvoke* invoke_instruction, |
| 95 | ArtMethod* resolved_method, |
| 96 | HInstruction** return_replacement) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 97 | REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 98 | |
| 99 | // Create a new HInstanceFieldGet. |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 100 | HInstanceFieldGet* CreateInstanceFieldGet(Handle<mirror::DexCache> dex_cache, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 101 | uint32_t field_index, |
| 102 | HInstruction* obj); |
| 103 | // Create a new HInstanceFieldSet. |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 104 | HInstanceFieldSet* CreateInstanceFieldSet(Handle<mirror::DexCache> dex_cache, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 105 | uint32_t field_index, |
| 106 | HInstruction* obj, |
| 107 | HInstruction* value); |
| 108 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 109 | // Try to inline the target of a monomorphic call. If successful, the code |
| 110 | // in the graph will look like: |
| 111 | // if (receiver.getClass() != ic.GetMonomorphicType()) deopt |
| 112 | // ... // inlined code |
| 113 | bool TryInlineMonomorphicCall(HInvoke* invoke_instruction, |
| 114 | ArtMethod* resolved_method, |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 115 | Handle<mirror::ObjectArray<mirror::Class>> classes) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 116 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 117 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 118 | // Try to inline targets of a polymorphic call. |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 119 | bool TryInlinePolymorphicCall(HInvoke* invoke_instruction, |
| 120 | ArtMethod* resolved_method, |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 121 | Handle<mirror::ObjectArray<mirror::Class>> classes) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 122 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 123 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 124 | bool TryInlinePolymorphicCallToSameTarget(HInvoke* invoke_instruction, |
| 125 | ArtMethod* resolved_method, |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 126 | Handle<mirror::ObjectArray<mirror::Class>> classes) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 127 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 128 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 129 | // Try CHA-based devirtualization to change virtual method calls into |
| 130 | // direct calls. |
| 131 | // Returns the actual method that resolved_method can be devirtualized to. |
| 132 | ArtMethod* TryCHADevirtualization(ArtMethod* resolved_method) |
| 133 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 134 | |
| 135 | // Add a CHA guard for a CHA-based devirtualized call. A CHA guard checks a |
| 136 | // should_deoptimize flag and if it's true, does deoptimization. |
| 137 | void AddCHAGuard(HInstruction* invoke_instruction, |
| 138 | uint32_t dex_pc, |
| 139 | HInstruction* cursor, |
| 140 | HBasicBlock* bb_cursor); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 141 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 142 | HInstanceFieldGet* BuildGetReceiverClass(ClassLinker* class_linker, |
| 143 | HInstruction* receiver, |
| 144 | uint32_t dex_pc) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 145 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 146 | |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 147 | void FixUpReturnReferenceType(ArtMethod* resolved_method, HInstruction* return_replacement) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 148 | REQUIRES_SHARED(Locks::mutator_lock_); |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 149 | |
| 150 | // Creates an instance of ReferenceTypeInfo from `klass` if `klass` is |
| 151 | // admissible (see ReferenceTypePropagation::IsAdmissible for details). |
| 152 | // Otherwise returns inexact Object RTI. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 153 | ReferenceTypeInfo GetClassRTI(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_); |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 154 | |
| 155 | bool ArgumentTypesMoreSpecific(HInvoke* invoke_instruction, ArtMethod* resolved_method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 156 | REQUIRES_SHARED(Locks::mutator_lock_); |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 157 | |
| 158 | bool ReturnTypeMoreSpecific(HInvoke* invoke_instruction, HInstruction* return_replacement) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 159 | REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 160 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 161 | // Add a type guard on the given `receiver`. This will add to the graph: |
| 162 | // i0 = HFieldGet(receiver, klass) |
| 163 | // i1 = HLoadClass(class_index, is_referrer) |
| 164 | // i2 = HNotEqual(i0, i1) |
| 165 | // |
| 166 | // And if `with_deoptimization` is true: |
| 167 | // HDeoptimize(i2) |
| 168 | // |
| 169 | // The method returns the `HNotEqual`, that will be used for polymorphic inlining. |
| 170 | HInstruction* AddTypeGuard(HInstruction* receiver, |
| 171 | HInstruction* cursor, |
| 172 | HBasicBlock* bb_cursor, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 173 | dex::TypeIndex class_index, |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 174 | Handle<mirror::Class> klass, |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 175 | HInstruction* invoke_instruction, |
| 176 | bool with_deoptimization) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 177 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * Ad-hoc implementation for implementing a diamond pattern in the graph for |
| 181 | * polymorphic inlining: |
| 182 | * 1) `compare` becomes the input of the new `HIf`. |
| 183 | * 2) Everything up until `invoke_instruction` is in the then branch (could |
| 184 | * contain multiple blocks). |
| 185 | * 3) `invoke_instruction` is moved to the otherwise block. |
| 186 | * 4) If `return_replacement` is not null, the merge block will have |
| 187 | * a phi whose inputs are `return_replacement` and `invoke_instruction`. |
| 188 | * |
| 189 | * Before: |
| 190 | * Block1 |
| 191 | * compare |
| 192 | * ... |
| 193 | * invoke_instruction |
| 194 | * |
| 195 | * After: |
| 196 | * Block1 |
| 197 | * compare |
| 198 | * if |
| 199 | * / \ |
| 200 | * / \ |
| 201 | * Then block Otherwise block |
| 202 | * ... invoke_instruction |
| 203 | * \ / |
| 204 | * \ / |
| 205 | * Merge block |
| 206 | * phi(return_replacement, invoke_instruction) |
| 207 | */ |
| 208 | void CreateDiamondPatternForPolymorphicInline(HInstruction* compare, |
| 209 | HInstruction* return_replacement, |
| 210 | HInstruction* invoke_instruction); |
| 211 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 212 | HGraph* const outermost_graph_; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 213 | const DexCompilationUnit& outer_compilation_unit_; |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 214 | const DexCompilationUnit& caller_compilation_unit_; |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 215 | CodeGenerator* const codegen_; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 216 | CompilerDriver* const compiler_driver_; |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 217 | const size_t total_number_of_dex_registers_; |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 218 | const size_t depth_; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 219 | size_t number_of_inlined_instructions_; |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 220 | VariableSizedHandleScope* const handles_; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 221 | |
Vladimir Marko | 438709f | 2017-02-23 18:56:13 +0000 | [diff] [blame^] | 222 | // Used to record stats about optimizations on the inlined graph. |
| 223 | // If the inlining is successful, these stats are merged to the caller graph's stats. |
| 224 | OptimizingCompilerStats* inline_stats_; |
| 225 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 226 | DISALLOW_COPY_AND_ASSIGN(HInliner); |
| 227 | }; |
| 228 | |
| 229 | } // namespace art |
| 230 | |
| 231 | #endif // ART_COMPILER_OPTIMIZING_INLINER_H_ |