1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2023 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "profiling_info_builder.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
20*795d594fSAndroid Build Coastguard Worker #include "code_generator.h"
21*795d594fSAndroid Build Coastguard Worker #include "driver/compiler_options.h"
22*795d594fSAndroid Build Coastguard Worker #include "dex/code_item_accessors-inl.h"
23*795d594fSAndroid Build Coastguard Worker #include "inliner.h"
24*795d594fSAndroid Build Coastguard Worker #include "jit/profiling_info.h"
25*795d594fSAndroid Build Coastguard Worker #include "optimizing_compiler_stats.h"
26*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
27*795d594fSAndroid Build Coastguard Worker
28*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
29*795d594fSAndroid Build Coastguard Worker
Run()30*795d594fSAndroid Build Coastguard Worker void ProfilingInfoBuilder::Run() {
31*795d594fSAndroid Build Coastguard Worker DCHECK(GetGraph()->IsUsefulOptimizing());
32*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(GetGraph()->GetProfilingInfo(), nullptr);
33*795d594fSAndroid Build Coastguard Worker // Order does not matter.
34*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* block : GetGraph()->GetReversePostOrder()) {
35*795d594fSAndroid Build Coastguard Worker // No need to visit the phis.
36*795d594fSAndroid Build Coastguard Worker VisitNonPhiInstructions(block);
37*795d594fSAndroid Build Coastguard Worker }
38*795d594fSAndroid Build Coastguard Worker
39*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
40*795d594fSAndroid Build Coastguard Worker GetGraph()->SetProfilingInfo(
41*795d594fSAndroid Build Coastguard Worker ProfilingInfo::Create(soa.Self(), GetGraph()->GetArtMethod(), inline_caches_));
42*795d594fSAndroid Build Coastguard Worker }
43*795d594fSAndroid Build Coastguard Worker
44*795d594fSAndroid Build Coastguard Worker
EncodeInlinedDexPc(const HInliner * inliner,const CompilerOptions & compiler_options,HInvoke * invoke)45*795d594fSAndroid Build Coastguard Worker uint32_t ProfilingInfoBuilder::EncodeInlinedDexPc(const HInliner* inliner,
46*795d594fSAndroid Build Coastguard Worker const CompilerOptions& compiler_options,
47*795d594fSAndroid Build Coastguard Worker HInvoke* invoke) {
48*795d594fSAndroid Build Coastguard Worker DCHECK(inliner->GetCallerEnvironment() != nullptr);
49*795d594fSAndroid Build Coastguard Worker DCHECK(inliner->GetParent() != nullptr);
50*795d594fSAndroid Build Coastguard Worker std::vector<uint32_t> temp_vector;
51*795d594fSAndroid Build Coastguard Worker temp_vector.push_back(invoke->GetDexPc());
52*795d594fSAndroid Build Coastguard Worker while (inliner->GetCallerEnvironment() != nullptr) {
53*795d594fSAndroid Build Coastguard Worker temp_vector.push_back(inliner->GetCallerEnvironment()->GetDexPc());
54*795d594fSAndroid Build Coastguard Worker inliner = inliner->GetParent();
55*795d594fSAndroid Build Coastguard Worker }
56*795d594fSAndroid Build Coastguard Worker
57*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(inliner->GetOutermostGraph(), inliner->GetGraph());
58*795d594fSAndroid Build Coastguard Worker return InlineCache::EncodeDexPc(
59*795d594fSAndroid Build Coastguard Worker inliner->GetOutermostGraph()->GetArtMethod(),
60*795d594fSAndroid Build Coastguard Worker temp_vector,
61*795d594fSAndroid Build Coastguard Worker compiler_options.GetInlineMaxCodeUnits());
62*795d594fSAndroid Build Coastguard Worker }
63*795d594fSAndroid Build Coastguard Worker
EncodeDexPc(HInvoke * invoke,const CompilerOptions & compiler_options)64*795d594fSAndroid Build Coastguard Worker static uint32_t EncodeDexPc(HInvoke* invoke, const CompilerOptions& compiler_options) {
65*795d594fSAndroid Build Coastguard Worker std::vector<uint32_t> dex_pcs;
66*795d594fSAndroid Build Coastguard Worker ArtMethod* outer_method = nullptr;
67*795d594fSAndroid Build Coastguard Worker for (HEnvironment* environment = invoke->GetEnvironment();
68*795d594fSAndroid Build Coastguard Worker environment != nullptr;
69*795d594fSAndroid Build Coastguard Worker environment = environment->GetParent()) {
70*795d594fSAndroid Build Coastguard Worker outer_method = environment->GetMethod();
71*795d594fSAndroid Build Coastguard Worker dex_pcs.push_back(environment->GetDexPc());
72*795d594fSAndroid Build Coastguard Worker }
73*795d594fSAndroid Build Coastguard Worker
74*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
75*795d594fSAndroid Build Coastguard Worker return InlineCache::EncodeDexPc(
76*795d594fSAndroid Build Coastguard Worker outer_method,
77*795d594fSAndroid Build Coastguard Worker dex_pcs,
78*795d594fSAndroid Build Coastguard Worker compiler_options.GetInlineMaxCodeUnits());
79*795d594fSAndroid Build Coastguard Worker }
80*795d594fSAndroid Build Coastguard Worker
HandleInvoke(HInvoke * invoke)81*795d594fSAndroid Build Coastguard Worker void ProfilingInfoBuilder::HandleInvoke(HInvoke* invoke) {
82*795d594fSAndroid Build Coastguard Worker if (IsInlineCacheUseful(invoke, codegen_)) {
83*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc = EncodeDexPc(invoke, compiler_options_);
84*795d594fSAndroid Build Coastguard Worker if (dex_pc != kNoDexPc) {
85*795d594fSAndroid Build Coastguard Worker inline_caches_.push_back(dex_pc);
86*795d594fSAndroid Build Coastguard Worker } else {
87*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
88*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Could not encode dex pc for "
89*795d594fSAndroid Build Coastguard Worker << invoke->GetResolvedMethod()->PrettyMethod();
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker }
92*795d594fSAndroid Build Coastguard Worker }
93*795d594fSAndroid Build Coastguard Worker
VisitInvokeInterface(HInvokeInterface * invoke)94*795d594fSAndroid Build Coastguard Worker void ProfilingInfoBuilder::VisitInvokeInterface(HInvokeInterface* invoke) {
95*795d594fSAndroid Build Coastguard Worker HandleInvoke(invoke);
96*795d594fSAndroid Build Coastguard Worker }
97*795d594fSAndroid Build Coastguard Worker
VisitInvokeVirtual(HInvokeVirtual * invoke)98*795d594fSAndroid Build Coastguard Worker void ProfilingInfoBuilder::VisitInvokeVirtual(HInvokeVirtual* invoke) {
99*795d594fSAndroid Build Coastguard Worker HandleInvoke(invoke);
100*795d594fSAndroid Build Coastguard Worker }
101*795d594fSAndroid Build Coastguard Worker
IsInlineCacheUseful(HInvoke * invoke,CodeGenerator * codegen)102*795d594fSAndroid Build Coastguard Worker bool ProfilingInfoBuilder::IsInlineCacheUseful(HInvoke* invoke, CodeGenerator* codegen) {
103*795d594fSAndroid Build Coastguard Worker DCHECK(invoke->IsInvokeVirtual() || invoke->IsInvokeInterface());
104*795d594fSAndroid Build Coastguard Worker if (codegen->IsImplementedIntrinsic(invoke)) {
105*795d594fSAndroid Build Coastguard Worker return false;
106*795d594fSAndroid Build Coastguard Worker }
107*795d594fSAndroid Build Coastguard Worker if (!invoke->GetBlock()->GetGraph()->IsCompilingBaseline()) {
108*795d594fSAndroid Build Coastguard Worker return false;
109*795d594fSAndroid Build Coastguard Worker }
110*795d594fSAndroid Build Coastguard Worker if (Runtime::Current()->IsAotCompiler()) {
111*795d594fSAndroid Build Coastguard Worker return false;
112*795d594fSAndroid Build Coastguard Worker }
113*795d594fSAndroid Build Coastguard Worker if (invoke->InputAt(0)->GetReferenceTypeInfo().IsExact()) {
114*795d594fSAndroid Build Coastguard Worker return false;
115*795d594fSAndroid Build Coastguard Worker }
116*795d594fSAndroid Build Coastguard Worker if (invoke->GetResolvedMethod() != nullptr) {
117*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
118*795d594fSAndroid Build Coastguard Worker if (invoke->GetResolvedMethod()->IsFinal() ||
119*795d594fSAndroid Build Coastguard Worker invoke->GetResolvedMethod()->GetDeclaringClass()->IsFinal()) {
120*795d594fSAndroid Build Coastguard Worker return false;
121*795d594fSAndroid Build Coastguard Worker }
122*795d594fSAndroid Build Coastguard Worker }
123*795d594fSAndroid Build Coastguard Worker
124*795d594fSAndroid Build Coastguard Worker if (!codegen->GetGraph()->IsUsefulOptimizing()) {
125*795d594fSAndroid Build Coastguard Worker // Earlier pass knew what the calling target was. No need for an inline
126*795d594fSAndroid Build Coastguard Worker // cache.
127*795d594fSAndroid Build Coastguard Worker return false;
128*795d594fSAndroid Build Coastguard Worker }
129*795d594fSAndroid Build Coastguard Worker return true;
130*795d594fSAndroid Build Coastguard Worker }
131*795d594fSAndroid Build Coastguard Worker
GetInlineCache(ProfilingInfo * info,const CompilerOptions & compiler_options,HInvoke * instruction)132*795d594fSAndroid Build Coastguard Worker InlineCache* ProfilingInfoBuilder::GetInlineCache(ProfilingInfo* info,
133*795d594fSAndroid Build Coastguard Worker const CompilerOptions& compiler_options,
134*795d594fSAndroid Build Coastguard Worker HInvoke* instruction) {
135*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
136*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc = EncodeDexPc(instruction, compiler_options);
137*795d594fSAndroid Build Coastguard Worker if (dex_pc == kNoDexPc) {
138*795d594fSAndroid Build Coastguard Worker return nullptr;
139*795d594fSAndroid Build Coastguard Worker }
140*795d594fSAndroid Build Coastguard Worker return info->GetInlineCache(dex_pc);
141*795d594fSAndroid Build Coastguard Worker }
142*795d594fSAndroid Build Coastguard Worker
143*795d594fSAndroid Build Coastguard Worker } // namespace art
144