blob: 982553d3aff7ddeaa7d02dda75e2c8bdec99f39c [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
17#ifndef ART_RUNTIME_QUICK_INLINE_METHOD_ANALYSER_H_
18#define ART_RUNTIME_QUICK_INLINE_METHOD_ANALYSER_H_
19
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
36
37enum InlineMethodOpcode : uint16_t {
38 kIntrinsicDoubleCvt,
39 kIntrinsicFloatCvt,
Serban Constantinescu23abec92014-07-02 16:13:38 +010040 kIntrinsicReverseBits,
Vladimir Markoe3e02602014-03-12 15:42:41 +000041 kIntrinsicReverseBytes,
42 kIntrinsicAbsInt,
43 kIntrinsicAbsLong,
44 kIntrinsicAbsFloat,
45 kIntrinsicAbsDouble,
46 kIntrinsicMinMaxInt,
Serban Constantinescu23abec92014-07-02 16:13:38 +010047 kIntrinsicMinMaxLong,
48 kIntrinsicMinMaxFloat,
49 kIntrinsicMinMaxDouble,
Vladimir Markoe3e02602014-03-12 15:42:41 +000050 kIntrinsicSqrt,
Fred Shih4ee7a662014-07-11 09:59:27 -070051 kIntrinsicGet,
Vladimir Markoe3e02602014-03-12 15:42:41 +000052 kIntrinsicCharAt,
53 kIntrinsicCompareTo,
54 kIntrinsicIsEmptyOrLength,
55 kIntrinsicIndexOf,
56 kIntrinsicCurrentThread,
57 kIntrinsicPeek,
58 kIntrinsicPoke,
59 kIntrinsicCas,
60 kIntrinsicUnsafeGet,
61 kIntrinsicUnsafePut,
DaniilSokolov70c4f062014-06-24 17:34:00 -070062 kIntrinsicSystemArrayCopyCharArray,
Vladimir Markoe3e02602014-03-12 15:42:41 +000063
64 kInlineOpNop,
65 kInlineOpReturnArg,
66 kInlineOpNonWideConst,
67 kInlineOpIGet,
68 kInlineOpIPut,
69};
70std::ostream& operator<<(std::ostream& os, const InlineMethodOpcode& rhs);
71
72enum InlineMethodFlags : uint16_t {
73 kNoInlineMethodFlags = 0x0000,
74 kInlineIntrinsic = 0x0001,
75 kInlineSpecial = 0x0002,
76};
77
78// IntrinsicFlags are stored in InlineMethod::d::raw_data
79enum IntrinsicFlags {
80 kIntrinsicFlagNone = 0,
81
82 // kIntrinsicMinMaxInt
83 kIntrinsicFlagMax = kIntrinsicFlagNone,
84 kIntrinsicFlagMin = 1,
85
86 // kIntrinsicIsEmptyOrLength
87 kIntrinsicFlagLength = kIntrinsicFlagNone,
88 kIntrinsicFlagIsEmpty = kIntrinsicFlagMin,
89
90 // kIntrinsicIndexOf
91 kIntrinsicFlagBase0 = kIntrinsicFlagMin,
92
93 // kIntrinsicUnsafeGet, kIntrinsicUnsafePut, kIntrinsicUnsafeCas
94 kIntrinsicFlagIsLong = kIntrinsicFlagMin,
95 // kIntrinsicUnsafeGet, kIntrinsicUnsafePut
96 kIntrinsicFlagIsVolatile = 2,
97 // kIntrinsicUnsafePut, kIntrinsicUnsafeCas
98 kIntrinsicFlagIsObject = 4,
99 // kIntrinsicUnsafePut
100 kIntrinsicFlagIsOrdered = 8,
101};
102
103struct InlineIGetIPutData {
104 // The op_variant below is opcode-Instruction::IGET for IGETs and
105 // opcode-Instruction::IPUT for IPUTs. This is because the runtime
106 // doesn't know the OpSize enumeration.
107 uint16_t op_variant : 3;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100108 uint16_t method_is_static : 1;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000109 uint16_t object_arg : 4;
110 uint16_t src_arg : 4; // iput only
Vladimir Markoe1fced12014-04-04 14:52:53 +0100111 uint16_t return_arg_plus1 : 4; // iput only, method argument to return + 1, 0 = return void.
Vladimir Markoe3e02602014-03-12 15:42:41 +0000112 uint16_t field_idx;
113 uint32_t is_volatile : 1;
114 uint32_t field_offset : 31;
115};
116COMPILE_ASSERT(sizeof(InlineIGetIPutData) == sizeof(uint64_t), InvalidSizeOfInlineIGetIPutData);
117
118struct InlineReturnArgData {
119 uint16_t arg;
120 uint16_t is_wide : 1;
121 uint16_t is_object : 1;
122 uint16_t reserved : 14;
123 uint32_t reserved2;
124};
125COMPILE_ASSERT(sizeof(InlineReturnArgData) == sizeof(uint64_t), InvalidSizeOfInlineReturnArgData);
126
127struct InlineMethod {
128 InlineMethodOpcode opcode;
129 InlineMethodFlags flags;
130 union {
131 uint64_t data;
132 InlineIGetIPutData ifield_data;
133 InlineReturnArgData return_data;
134 } d;
135};
136
137class InlineMethodAnalyser {
138 public:
139 /**
140 * Analyse method code to determine if the method is a candidate for inlining.
141 * If it is, record the inlining data.
142 *
143 * @param verifier the method verifier holding data about the method to analyse.
144 * @param method placeholder for the inline method data.
145 * @return true if the method is a candidate for inlining, false otherwise.
146 */
147 static bool AnalyseMethodCode(verifier::MethodVerifier* verifier, InlineMethod* method)
148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
149
150 static constexpr bool IsInstructionIGet(Instruction::Code opcode) {
151 return Instruction::IGET <= opcode && opcode <= Instruction::IGET_SHORT;
152 }
153
154 static constexpr bool IsInstructionIPut(Instruction::Code opcode) {
155 return Instruction::IPUT <= opcode && opcode <= Instruction::IPUT_SHORT;
156 }
157
158 static constexpr uint16_t IGetVariant(Instruction::Code opcode) {
159 return opcode - Instruction::IGET;
160 }
161
162 static constexpr uint16_t IPutVariant(Instruction::Code opcode) {
163 return opcode - Instruction::IPUT;
164 }
165
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100166 // Determines whether the method is a synthetic accessor (method name starts with "access$").
167 static bool IsSyntheticAccessor(MethodReference ref);
168
Vladimir Markoe3e02602014-03-12 15:42:41 +0000169 private:
170 static bool AnalyseReturnMethod(const DexFile::CodeItem* code_item, InlineMethod* result);
171 static bool AnalyseConstMethod(const DexFile::CodeItem* code_item, InlineMethod* result);
172 static bool AnalyseIGetMethod(verifier::MethodVerifier* verifier, InlineMethod* result)
173 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
174 static bool AnalyseIPutMethod(verifier::MethodVerifier* verifier, InlineMethod* result)
175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
176
177 // Can we fast path instance field access in a verified accessor?
178 // If yes, computes field's offset and volatility and whether the method is static or not.
179 static bool ComputeSpecialAccessorInfo(uint32_t field_idx, bool is_put,
180 verifier::MethodVerifier* verifier,
181 InlineIGetIPutData* result)
182 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
183};
184
185} // namespace art
186
187#endif // ART_RUNTIME_QUICK_INLINE_METHOD_ANALYSER_H_