blob: a128079cdb58a6914ba917d2ae3aee6802747d65 [file] [log] [blame]
Vladimir Markodc151b22015-10-15 18:02:30 +01001/*
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 Markod1eaf0d2015-10-29 12:18:29 +000023#include "runtime.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010024
25namespace art {
26
27void 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
44void 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.
52 InvokeType invoke_type = invoke->GetOriginalInvokeType();
53 MethodReference target_method(&graph_->GetDexFile(), invoke->GetDexMethodIndex());
54 int vtable_idx;
55 uintptr_t direct_code, direct_method;
56 bool success = compiler_driver_->ComputeInvokeInfo(
57 &compilation_unit_,
58 invoke->GetDexPc(),
59 false /* update_stats: already updated in builder */,
60 true /* enable_devirtualization */,
61 &invoke_type,
62 &target_method,
63 &vtable_idx,
64 &direct_code,
65 &direct_method);
66 DCHECK(success);
67 DCHECK_EQ(invoke_type, invoke->GetInvokeType());
68 DCHECK_EQ(target_method.dex_file, invoke->GetTargetMethod().dex_file);
69 DCHECK_EQ(target_method.dex_method_index, invoke->GetTargetMethod().dex_method_index);
70
71 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
72 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
73 uint64_t method_load_data = 0u;
74 uint64_t direct_code_ptr = 0u;
75
76 HGraph* outer_graph = codegen_->GetGraph();
77 if (target_method.dex_file == &outer_graph->GetDexFile() &&
78 target_method.dex_method_index == outer_graph->GetMethodIdx()) {
79 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
80 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
81 } else {
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000082 bool use_pc_relative_instructions =
83 ((direct_method == 0u || direct_code == static_cast<uintptr_t>(-1))) &&
84 ContainsElement(compiler_driver_->GetDexFilesForOatFile(), target_method.dex_file);
Vladimir Markodc151b22015-10-15 18:02:30 +010085 if (direct_method != 0u) { // Should we use a direct pointer to the method?
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000086 // Note: For JIT, kDirectAddressWithFixup doesn't make sense at all and while
87 // kDirectAddress would be fine for image methods, we don't support it at the moment.
88 DCHECK(!Runtime::Current()->UseJit());
Vladimir Markodc151b22015-10-15 18:02:30 +010089 if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now?
90 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
91 method_load_data = direct_method;
92 } else { // The direct pointer will be known at link time.
93 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup;
94 }
95 } else { // Use dex cache.
96 DCHECK_EQ(target_method.dex_file, &graph_->GetDexFile());
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000097 if (use_pc_relative_instructions) { // Can we use PC-relative access to the dex cache arrays?
98 DCHECK(!Runtime::Current()->UseJit());
Vladimir Markodc151b22015-10-15 18:02:30 +010099 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000100 DexCacheArraysLayout layout(GetInstructionSetPointerSize(codegen_->GetInstructionSet()),
101 &graph_->GetDexFile());
Vladimir Markodc151b22015-10-15 18:02:30 +0100102 method_load_data = layout.MethodOffset(target_method.dex_method_index);
103 } else { // We must go through the ArtMethod's pointer to resolved methods.
104 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
105 }
106 }
107 if (direct_code != 0u) { // Should we use a direct pointer to the code?
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000108 // Note: For JIT, kCallPCRelative and kCallDirectWithFixup don't make sense at all and
109 // while kCallDirect would be fine for image methods, we don't support it at the moment.
110 DCHECK(!Runtime::Current()->UseJit());
Vladimir Markodc151b22015-10-15 18:02:30 +0100111 if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now?
112 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect;
113 direct_code_ptr = direct_code;
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000114 } else if (use_pc_relative_instructions) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100115 // Use PC-relative calls for invokes within a multi-dex oat file.
Vladimir Markodc151b22015-10-15 18:02:30 +0100116 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative;
117 } else { // The direct pointer will be known at link time.
118 // NOTE: This is used for app->boot calls when compiling an app against
119 // a relocatable but not yet relocated image.
120 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup;
121 }
122 } else { // We must use the code pointer from the ArtMethod.
123 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
124 }
125 }
126
127 if (graph_->IsDebuggable()) {
128 // For debuggable apps always use the code pointer from ArtMethod
129 // so that we don't circumvent instrumentation stubs if installed.
130 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
131 }
132
133 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
134 method_load_kind, code_ptr_location, method_load_data, direct_code_ptr
135 };
136 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
137 codegen_->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info,
138 invoke->GetTargetMethod());
139 invoke->SetDispatchInfo(dispatch_info);
140}
141
142} // namespace art