summaryrefslogtreecommitdiff
path: root/compiler/optimizing/profiling_info_builder.cc
blob: 905d8d0f2f5fd4dd9a4589fbb6e80e5f8ea33ac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "profiling_info_builder.h"

#include "art_method-inl.h"
#include "code_generator.h"
#include "driver/compiler_options.h"
#include "dex/code_item_accessors-inl.h"
#include "inliner.h"
#include "jit/profiling_info.h"
#include "optimizing_compiler_stats.h"
#include "scoped_thread_state_change-inl.h"

namespace art HIDDEN {

void ProfilingInfoBuilder::Run() {
  DCHECK_EQ(GetGraph()->GetProfilingInfo(), nullptr);
  // Order does not matter.
  for (HBasicBlock* block : GetGraph()->GetReversePostOrder()) {
    // No need to visit the phis.
    VisitNonPhiInstructions(block);
  }

  ScopedObjectAccess soa(Thread::Current());
  GetGraph()->SetProfilingInfo(
      ProfilingInfo::Create(soa.Self(), GetGraph()->GetArtMethod(), inline_caches_));
}


uint32_t ProfilingInfoBuilder::EncodeInlinedDexPc(const HInliner* inliner,
                                                  const CompilerOptions& compiler_options,
                                                  HInvoke* invoke) {
  DCHECK(inliner->GetCallerEnvironment() != nullptr);
  DCHECK(inliner->GetParent() != nullptr);
  std::vector<uint32_t> temp_vector;
  temp_vector.push_back(invoke->GetDexPc());
  while (inliner->GetCallerEnvironment() != nullptr) {
    temp_vector.push_back(inliner->GetCallerEnvironment()->GetDexPc());
    inliner = inliner->GetParent();
  }

  DCHECK_EQ(inliner->GetOutermostGraph(), inliner->GetGraph());
  return InlineCache::EncodeDexPc(
      inliner->GetOutermostGraph()->GetArtMethod(),
      temp_vector,
      compiler_options.GetInlineMaxCodeUnits());
}

static uint32_t EncodeDexPc(HInvoke* invoke, const CompilerOptions& compiler_options) {
  std::vector<uint32_t> dex_pcs;
  ArtMethod* outer_method = nullptr;
  for (HEnvironment* environment = invoke->GetEnvironment();
       environment != nullptr;
       environment = environment->GetParent()) {
    outer_method = environment->GetMethod();
    dex_pcs.push_back(environment->GetDexPc());
  }

  ScopedObjectAccess soa(Thread::Current());
  return InlineCache::EncodeDexPc(
      outer_method,
      dex_pcs,
      compiler_options.GetInlineMaxCodeUnits());
}

void ProfilingInfoBuilder::HandleInvoke(HInvoke* invoke) {
  if (IsInlineCacheUseful(invoke, codegen_)) {
    uint32_t dex_pc = EncodeDexPc(invoke, compiler_options_);
    if (dex_pc != kNoDexPc) {
      inline_caches_.push_back(dex_pc);
    } else {
      ScopedObjectAccess soa(Thread::Current());
      LOG(WARNING) << "Could not encode dex pc for "
                   << invoke->GetResolvedMethod()->PrettyMethod();
    }
  }
}

void ProfilingInfoBuilder::VisitInvokeInterface(HInvokeInterface* invoke) {
  HandleInvoke(invoke);
}

void ProfilingInfoBuilder::VisitInvokeVirtual(HInvokeVirtual* invoke) {
  HandleInvoke(invoke);
}

bool ProfilingInfoBuilder::IsInlineCacheUseful(HInvoke* invoke, CodeGenerator* codegen) {
  DCHECK(invoke->IsInvokeVirtual() || invoke->IsInvokeInterface());
  if (codegen->IsImplementedIntrinsic(invoke)) {
    return false;
  }
  if (!invoke->GetBlock()->GetGraph()->IsCompilingBaseline()) {
    return false;
  }
  if (Runtime::Current()->IsAotCompiler()) {
    return false;
  }
  if (invoke->InputAt(0)->GetReferenceTypeInfo().IsExact()) {
    return false;
  }
  if (invoke->GetResolvedMethod() != nullptr) {
    ScopedObjectAccess soa(Thread::Current());
    if (invoke->GetResolvedMethod()->IsFinal() ||
        invoke->GetResolvedMethod()->GetDeclaringClass()->IsFinal()) {
      return false;
    }
  }
  return true;
}

InlineCache* ProfilingInfoBuilder::GetInlineCache(ProfilingInfo* info,
                                                  const CompilerOptions& compiler_options,
                                                  HInvoke* instruction) {
  ScopedObjectAccess soa(Thread::Current());
  uint32_t dex_pc = EncodeDexPc(instruction, compiler_options);
  if (dex_pc == kNoDexPc) {
    return nullptr;
  }
  return info->GetInlineCache(dex_pc);
}

}  // namespace art