Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include "sharpening.h" |
| 18 | |
| 19 | #include "code_generator.h" |
| 20 | #include "utils/dex_cache_arrays_layout-inl.h" |
| 21 | #include "driver/compiler_driver.h" |
| 22 | #include "nodes.h" |
Vladimir Marko | d1eaf0d | 2015-10-29 12:18:29 +0000 | [diff] [blame] | 23 | #include "runtime.h" |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | void HSharpening::Run() { |
| 28 | // We don't care about the order of the blocks here. |
| 29 | for (HBasicBlock* block : graph_->GetReversePostOrder()) { |
| 30 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 31 | HInstruction* instruction = it.Current(); |
| 32 | if (instruction->IsInvokeStaticOrDirect()) { |
| 33 | ProcessInvokeStaticOrDirect(instruction->AsInvokeStaticOrDirect()); |
| 34 | } |
| 35 | // TODO: Move the sharpening of invoke-virtual/-interface/-super from HGraphBuilder |
| 36 | // here. Rewrite it to avoid the CompilerDriver's reliance on verifier data |
| 37 | // because we know the type better when inlining. |
| 38 | // TODO: HLoadClass, HLoadString - select PC relative dex cache array access if |
| 39 | // available. |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void HSharpening::ProcessInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) { |
| 45 | if (invoke->IsStringInit()) { |
| 46 | // Not using the dex cache arrays. But we could still try to use a better dispatch... |
| 47 | // TODO: Use direct_method and direct_code for the appropriate StringFactory method. |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | // TODO: Avoid CompilerDriver. |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 52 | InvokeType original_invoke_type = invoke->GetOriginalInvokeType(); |
| 53 | InvokeType optimized_invoke_type = original_invoke_type; |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 54 | MethodReference target_method(&graph_->GetDexFile(), invoke->GetDexMethodIndex()); |
| 55 | int vtable_idx; |
| 56 | uintptr_t direct_code, direct_method; |
| 57 | bool success = compiler_driver_->ComputeInvokeInfo( |
| 58 | &compilation_unit_, |
| 59 | invoke->GetDexPc(), |
| 60 | false /* update_stats: already updated in builder */, |
| 61 | true /* enable_devirtualization */, |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 62 | &optimized_invoke_type, |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 63 | &target_method, |
| 64 | &vtable_idx, |
| 65 | &direct_code, |
| 66 | &direct_method); |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 67 | if (!success) { |
| 68 | // TODO: try using kDexCachePcRelative. It's always a valid method load |
| 69 | // kind as long as it's supported by the codegen |
| 70 | return; |
| 71 | } |
| 72 | invoke->SetOptimizedInvokeType(optimized_invoke_type); |
| 73 | invoke->SetTargetMethod(target_method); |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 74 | |
| 75 | HInvokeStaticOrDirect::MethodLoadKind method_load_kind; |
| 76 | HInvokeStaticOrDirect::CodePtrLocation code_ptr_location; |
| 77 | uint64_t method_load_data = 0u; |
| 78 | uint64_t direct_code_ptr = 0u; |
| 79 | |
| 80 | HGraph* outer_graph = codegen_->GetGraph(); |
| 81 | if (target_method.dex_file == &outer_graph->GetDexFile() && |
| 82 | target_method.dex_method_index == outer_graph->GetMethodIdx()) { |
| 83 | method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive; |
| 84 | code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf; |
| 85 | } else { |
Vladimir Marko | d1eaf0d | 2015-10-29 12:18:29 +0000 | [diff] [blame] | 86 | bool use_pc_relative_instructions = |
| 87 | ((direct_method == 0u || direct_code == static_cast<uintptr_t>(-1))) && |
| 88 | ContainsElement(compiler_driver_->GetDexFilesForOatFile(), target_method.dex_file); |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 89 | if (direct_method != 0u) { // Should we use a direct pointer to the method? |
Vladimir Marko | d1eaf0d | 2015-10-29 12:18:29 +0000 | [diff] [blame] | 90 | // Note: For JIT, kDirectAddressWithFixup doesn't make sense at all and while |
| 91 | // kDirectAddress would be fine for image methods, we don't support it at the moment. |
| 92 | DCHECK(!Runtime::Current()->UseJit()); |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 93 | if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now? |
| 94 | method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress; |
| 95 | method_load_data = direct_method; |
| 96 | } else { // The direct pointer will be known at link time. |
| 97 | method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup; |
| 98 | } |
| 99 | } else { // Use dex cache. |
| 100 | DCHECK_EQ(target_method.dex_file, &graph_->GetDexFile()); |
Vladimir Marko | d1eaf0d | 2015-10-29 12:18:29 +0000 | [diff] [blame] | 101 | if (use_pc_relative_instructions) { // Can we use PC-relative access to the dex cache arrays? |
| 102 | DCHECK(!Runtime::Current()->UseJit()); |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 103 | method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative; |
Vladimir Marko | d1eaf0d | 2015-10-29 12:18:29 +0000 | [diff] [blame] | 104 | DexCacheArraysLayout layout(GetInstructionSetPointerSize(codegen_->GetInstructionSet()), |
| 105 | &graph_->GetDexFile()); |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 106 | method_load_data = layout.MethodOffset(target_method.dex_method_index); |
| 107 | } else { // We must go through the ArtMethod's pointer to resolved methods. |
| 108 | method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod; |
| 109 | } |
| 110 | } |
| 111 | if (direct_code != 0u) { // Should we use a direct pointer to the code? |
Vladimir Marko | d1eaf0d | 2015-10-29 12:18:29 +0000 | [diff] [blame] | 112 | // Note: For JIT, kCallPCRelative and kCallDirectWithFixup don't make sense at all and |
| 113 | // while kCallDirect would be fine for image methods, we don't support it at the moment. |
| 114 | DCHECK(!Runtime::Current()->UseJit()); |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 115 | if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now? |
| 116 | code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect; |
| 117 | direct_code_ptr = direct_code; |
Vladimir Marko | d1eaf0d | 2015-10-29 12:18:29 +0000 | [diff] [blame] | 118 | } else if (use_pc_relative_instructions) { |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 119 | // Use PC-relative calls for invokes within a multi-dex oat file. |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 120 | code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative; |
| 121 | } else { // The direct pointer will be known at link time. |
| 122 | // NOTE: This is used for app->boot calls when compiling an app against |
| 123 | // a relocatable but not yet relocated image. |
| 124 | code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup; |
| 125 | } |
| 126 | } else { // We must use the code pointer from the ArtMethod. |
| 127 | code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (graph_->IsDebuggable()) { |
| 132 | // For debuggable apps always use the code pointer from ArtMethod |
| 133 | // so that we don't circumvent instrumentation stubs if installed. |
| 134 | code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod; |
| 135 | } |
| 136 | |
| 137 | HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = { |
| 138 | method_load_kind, code_ptr_location, method_load_data, direct_code_ptr |
| 139 | }; |
| 140 | HInvokeStaticOrDirect::DispatchInfo dispatch_info = |
| 141 | codegen_->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, |
| 142 | invoke->GetTargetMethod()); |
| 143 | invoke->SetDispatchInfo(dispatch_info); |
| 144 | } |
| 145 | |
| 146 | } // namespace art |