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 | |
| 20 | #include "invoke_type.h" |
| 21 | #include "optimization.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 25 | class CodeGenerator; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 26 | class CompilerDriver; |
| 27 | class DexCompilationUnit; |
| 28 | class HGraph; |
| 29 | class HInvoke; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 30 | class InlineCache; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 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, |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 41 | StackHandleScopeCollection* 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), |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 54 | handles_(handles) {} |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 55 | |
| 56 | void Run() OVERRIDE; |
| 57 | |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 58 | static constexpr const char* kInlinerPassName = "inliner"; |
| 59 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 60 | private: |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 61 | bool TryInline(HInvoke* invoke_instruction); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 62 | |
| 63 | // 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] | 64 | // 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 Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 67 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 68 | |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 69 | 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 Levillain | a3aef2e | 2016-04-06 17:45:58 +0100 | [diff] [blame] | 79 | // 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 Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 85 | // 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] | 86 | bool TryPatternSubstitution(HInvoke* invoke_instruction, |
| 87 | ArtMethod* resolved_method, |
| 88 | HInstruction** return_replacement) |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 89 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 90 | |
| 91 | // Create a new HInstanceFieldGet. |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 92 | HInstanceFieldGet* CreateInstanceFieldGet(Handle<mirror::DexCache> dex_cache, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 93 | uint32_t field_index, |
| 94 | HInstruction* obj); |
| 95 | // Create a new HInstanceFieldSet. |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 96 | HInstanceFieldSet* CreateInstanceFieldSet(Handle<mirror::DexCache> dex_cache, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 97 | uint32_t field_index, |
| 98 | HInstruction* obj, |
| 99 | HInstruction* value); |
| 100 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 101 | // 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 Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 110 | // Try to inline targets of a polymorphic call. |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 111 | bool TryInlinePolymorphicCall(HInvoke* invoke_instruction, |
| 112 | ArtMethod* resolved_method, |
| 113 | const InlineCache& ic) |
| 114 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 115 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 116 | bool TryInlinePolymorphicCallToSameTarget(HInvoke* invoke_instruction, |
| 117 | ArtMethod* resolved_method, |
| 118 | const InlineCache& ic) |
| 119 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 120 | |
| 121 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 122 | HInstanceFieldGet* BuildGetReceiverClass(ClassLinker* class_linker, |
| 123 | HInstruction* receiver, |
| 124 | uint32_t dex_pc) const |
| 125 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 126 | |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 127 | void FixUpReturnReferenceType(ArtMethod* resolved_method, HInstruction* return_replacement) |
| 128 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 129 | |
| 130 | // Creates an instance of ReferenceTypeInfo from `klass` if `klass` is |
| 131 | // admissible (see ReferenceTypePropagation::IsAdmissible for details). |
| 132 | // Otherwise returns inexact Object RTI. |
| 133 | ReferenceTypeInfo GetClassRTI(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_); |
| 134 | |
| 135 | bool ArgumentTypesMoreSpecific(HInvoke* invoke_instruction, ArtMethod* resolved_method) |
| 136 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 137 | |
| 138 | bool ReturnTypeMoreSpecific(HInvoke* invoke_instruction, HInstruction* return_replacement) |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 139 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 140 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 141 | // Add a type guard on the given `receiver`. This will add to the graph: |
| 142 | // i0 = HFieldGet(receiver, klass) |
| 143 | // i1 = HLoadClass(class_index, is_referrer) |
| 144 | // i2 = HNotEqual(i0, i1) |
| 145 | // |
| 146 | // And if `with_deoptimization` is true: |
| 147 | // HDeoptimize(i2) |
| 148 | // |
| 149 | // The method returns the `HNotEqual`, that will be used for polymorphic inlining. |
| 150 | HInstruction* AddTypeGuard(HInstruction* receiver, |
| 151 | HInstruction* cursor, |
| 152 | HBasicBlock* bb_cursor, |
| 153 | uint32_t class_index, |
| 154 | bool is_referrer, |
| 155 | HInstruction* invoke_instruction, |
| 156 | bool with_deoptimization) |
| 157 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 158 | |
| 159 | /* |
| 160 | * Ad-hoc implementation for implementing a diamond pattern in the graph for |
| 161 | * polymorphic inlining: |
| 162 | * 1) `compare` becomes the input of the new `HIf`. |
| 163 | * 2) Everything up until `invoke_instruction` is in the then branch (could |
| 164 | * contain multiple blocks). |
| 165 | * 3) `invoke_instruction` is moved to the otherwise block. |
| 166 | * 4) If `return_replacement` is not null, the merge block will have |
| 167 | * a phi whose inputs are `return_replacement` and `invoke_instruction`. |
| 168 | * |
| 169 | * Before: |
| 170 | * Block1 |
| 171 | * compare |
| 172 | * ... |
| 173 | * invoke_instruction |
| 174 | * |
| 175 | * After: |
| 176 | * Block1 |
| 177 | * compare |
| 178 | * if |
| 179 | * / \ |
| 180 | * / \ |
| 181 | * Then block Otherwise block |
| 182 | * ... invoke_instruction |
| 183 | * \ / |
| 184 | * \ / |
| 185 | * Merge block |
| 186 | * phi(return_replacement, invoke_instruction) |
| 187 | */ |
| 188 | void CreateDiamondPatternForPolymorphicInline(HInstruction* compare, |
| 189 | HInstruction* return_replacement, |
| 190 | HInstruction* invoke_instruction); |
| 191 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 192 | HGraph* const outermost_graph_; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 193 | const DexCompilationUnit& outer_compilation_unit_; |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 194 | const DexCompilationUnit& caller_compilation_unit_; |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 195 | CodeGenerator* const codegen_; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 196 | CompilerDriver* const compiler_driver_; |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 197 | const size_t total_number_of_dex_registers_; |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 198 | const size_t depth_; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 199 | size_t number_of_inlined_instructions_; |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 200 | StackHandleScopeCollection* const handles_; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 201 | |
| 202 | DISALLOW_COPY_AND_ASSIGN(HInliner); |
| 203 | }; |
| 204 | |
| 205 | } // namespace art |
| 206 | |
| 207 | #endif // ART_COMPILER_OPTIMIZING_INLINER_H_ |