blob: a35e97fa1156e4d5de3ce02d5df6e16cfc950b6b [file] [log] [blame]
Vladimir Markoe3e02602014-03-12 15:42:41 +00001/*
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
Andreas Gampeb95c74b2017-04-20 19:43:21 -070017#ifndef ART_COMPILER_DEX_INLINE_METHOD_ANALYSER_H_
18#define ART_COMPILER_DEX_INLINE_METHOD_ANALYSER_H_
Vladimir Markoe3e02602014-03-12 15:42:41 +000019
20#include "base/macros.h"
21#include "base/mutex.h"
22#include "dex_file.h"
23#include "dex_instruction.h"
Vladimir Markoc8f60a62014-04-02 15:24:05 +010024#include "method_reference.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000025
26/*
27 * NOTE: This code is part of the quick compiler. It lives in the runtime
28 * only to allow the debugger to check whether a method has been inlined.
29 */
30
31namespace art {
32
33namespace verifier {
34class MethodVerifier;
35} // namespace verifier
David Sehr9323e6e2016-09-13 08:58:35 -070036class ArtMethod;
Vladimir Markoe3e02602014-03-12 15:42:41 +000037
38enum InlineMethodOpcode : uint16_t {
Vladimir Markoe3e02602014-03-12 15:42:41 +000039 kInlineOpNop,
40 kInlineOpReturnArg,
41 kInlineOpNonWideConst,
42 kInlineOpIGet,
43 kInlineOpIPut,
Vladimir Marko354efa62016-02-04 19:46:56 +000044 kInlineOpConstructor,
Vladimir Markoe3e02602014-03-12 15:42:41 +000045};
46
47struct InlineIGetIPutData {
Vladimir Markoaf6925b2014-10-31 16:37:32 +000048 // The op_variant below is DexMemAccessType but the runtime doesn't know that enumeration.
Vladimir Markoe3e02602014-03-12 15:42:41 +000049 uint16_t op_variant : 3;
Vladimir Markoe1fced12014-04-04 14:52:53 +010050 uint16_t method_is_static : 1;
Vladimir Markoe3e02602014-03-12 15:42:41 +000051 uint16_t object_arg : 4;
52 uint16_t src_arg : 4; // iput only
Vladimir Markoe1fced12014-04-04 14:52:53 +010053 uint16_t return_arg_plus1 : 4; // iput only, method argument to return + 1, 0 = return void.
Vladimir Markoe3e02602014-03-12 15:42:41 +000054 uint16_t field_idx;
55 uint32_t is_volatile : 1;
56 uint32_t field_offset : 31;
57};
Andreas Gampe575e78c2014-11-03 23:41:03 -080058static_assert(sizeof(InlineIGetIPutData) == sizeof(uint64_t), "Invalid size of InlineIGetIPutData");
Vladimir Markoe3e02602014-03-12 15:42:41 +000059
60struct InlineReturnArgData {
61 uint16_t arg;
62 uint16_t is_wide : 1;
63 uint16_t is_object : 1;
64 uint16_t reserved : 14;
65 uint32_t reserved2;
66};
Andreas Gampe575e78c2014-11-03 23:41:03 -080067static_assert(sizeof(InlineReturnArgData) == sizeof(uint64_t),
68 "Invalid size of InlineReturnArgData");
Vladimir Markoe3e02602014-03-12 15:42:41 +000069
Vladimir Marko354efa62016-02-04 19:46:56 +000070struct InlineConstructorData {
71 // There can be up to 3 IPUTs, unused fields are marked with kNoDexIndex16.
72 uint16_t iput0_field_index;
73 uint16_t iput1_field_index;
74 uint16_t iput2_field_index;
75 uint16_t iput0_arg : 4;
76 uint16_t iput1_arg : 4;
77 uint16_t iput2_arg : 4;
78 uint16_t reserved : 4;
79};
80static_assert(sizeof(InlineConstructorData) == sizeof(uint64_t),
81 "Invalid size of InlineConstructorData");
82
Vladimir Markoe3e02602014-03-12 15:42:41 +000083struct InlineMethod {
84 InlineMethodOpcode opcode;
Vladimir Markoe3e02602014-03-12 15:42:41 +000085 union {
86 uint64_t data;
87 InlineIGetIPutData ifield_data;
88 InlineReturnArgData return_data;
Vladimir Marko354efa62016-02-04 19:46:56 +000089 InlineConstructorData constructor_data;
Vladimir Markoe3e02602014-03-12 15:42:41 +000090 } d;
91};
92
93class InlineMethodAnalyser {
94 public:
95 /**
96 * Analyse method code to determine if the method is a candidate for inlining.
97 * If it is, record the inlining data.
98 *
Vladimir Markoe3e02602014-03-12 15:42:41 +000099 * @return true if the method is a candidate for inlining, false otherwise.
100 */
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000101 static bool AnalyseMethodCode(ArtMethod* method, InlineMethod* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700102 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000103
104 static constexpr bool IsInstructionIGet(Instruction::Code opcode) {
105 return Instruction::IGET <= opcode && opcode <= Instruction::IGET_SHORT;
106 }
107
108 static constexpr bool IsInstructionIPut(Instruction::Code opcode) {
109 return Instruction::IPUT <= opcode && opcode <= Instruction::IPUT_SHORT;
110 }
111
112 static constexpr uint16_t IGetVariant(Instruction::Code opcode) {
113 return opcode - Instruction::IGET;
114 }
115
116 static constexpr uint16_t IPutVariant(Instruction::Code opcode) {
117 return opcode - Instruction::IPUT;
118 }
119
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100120 // Determines whether the method is a synthetic accessor (method name starts with "access$").
121 static bool IsSyntheticAccessor(MethodReference ref);
122
Vladimir Markoe3e02602014-03-12 15:42:41 +0000123 private:
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000124 static bool AnalyseMethodCode(const DexFile::CodeItem* code_item,
125 const MethodReference& method_ref,
126 bool is_static,
127 ArtMethod* method,
128 InlineMethod* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700129 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000130 static bool AnalyseReturnMethod(const DexFile::CodeItem* code_item, InlineMethod* result);
131 static bool AnalyseConstMethod(const DexFile::CodeItem* code_item, InlineMethod* result);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000132 static bool AnalyseIGetMethod(const DexFile::CodeItem* code_item,
133 const MethodReference& method_ref,
134 bool is_static,
135 ArtMethod* method,
136 InlineMethod* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700137 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000138 static bool AnalyseIPutMethod(const DexFile::CodeItem* code_item,
139 const MethodReference& method_ref,
140 bool is_static,
141 ArtMethod* method,
142 InlineMethod* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700143 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000144
145 // Can we fast path instance field access in a verified accessor?
146 // If yes, computes field's offset and volatility and whether the method is static or not.
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000147 static bool ComputeSpecialAccessorInfo(ArtMethod* method,
148 uint32_t field_idx,
149 bool is_put,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000150 InlineIGetIPutData* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700151 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000152};
153
154} // namespace art
155
Andreas Gampeb95c74b2017-04-20 19:43:21 -0700156#endif // ART_COMPILER_DEX_INLINE_METHOD_ANALYSER_H_