blob: 34630250cfa7c75ca0b50d4cdab2de29c6047ce8 [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,
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +010051 kIntrinsicCeil,
52 kIntrinsicFloor,
53 kIntrinsicRint,
54 kIntrinsicRoundFloat,
55 kIntrinsicRoundDouble,
Mathieu Chartiercd48f2d2014-09-09 13:51:09 -070056 kIntrinsicReferenceGetReferent,
Vladimir Markoe3e02602014-03-12 15:42:41 +000057 kIntrinsicCharAt,
58 kIntrinsicCompareTo,
59 kIntrinsicIsEmptyOrLength,
60 kIntrinsicIndexOf,
61 kIntrinsicCurrentThread,
62 kIntrinsicPeek,
63 kIntrinsicPoke,
64 kIntrinsicCas,
65 kIntrinsicUnsafeGet,
66 kIntrinsicUnsafePut,
DaniilSokolov70c4f062014-06-24 17:34:00 -070067 kIntrinsicSystemArrayCopyCharArray,
Vladimir Markoe3e02602014-03-12 15:42:41 +000068
69 kInlineOpNop,
70 kInlineOpReturnArg,
71 kInlineOpNonWideConst,
72 kInlineOpIGet,
73 kInlineOpIPut,
74};
75std::ostream& operator<<(std::ostream& os, const InlineMethodOpcode& rhs);
76
77enum InlineMethodFlags : uint16_t {
78 kNoInlineMethodFlags = 0x0000,
79 kInlineIntrinsic = 0x0001,
80 kInlineSpecial = 0x0002,
81};
82
83// IntrinsicFlags are stored in InlineMethod::d::raw_data
84enum IntrinsicFlags {
85 kIntrinsicFlagNone = 0,
86
87 // kIntrinsicMinMaxInt
88 kIntrinsicFlagMax = kIntrinsicFlagNone,
89 kIntrinsicFlagMin = 1,
90
91 // kIntrinsicIsEmptyOrLength
92 kIntrinsicFlagLength = kIntrinsicFlagNone,
93 kIntrinsicFlagIsEmpty = kIntrinsicFlagMin,
94
95 // kIntrinsicIndexOf
96 kIntrinsicFlagBase0 = kIntrinsicFlagMin,
97
98 // kIntrinsicUnsafeGet, kIntrinsicUnsafePut, kIntrinsicUnsafeCas
99 kIntrinsicFlagIsLong = kIntrinsicFlagMin,
100 // kIntrinsicUnsafeGet, kIntrinsicUnsafePut
101 kIntrinsicFlagIsVolatile = 2,
102 // kIntrinsicUnsafePut, kIntrinsicUnsafeCas
103 kIntrinsicFlagIsObject = 4,
104 // kIntrinsicUnsafePut
105 kIntrinsicFlagIsOrdered = 8,
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800106
107 // kIntrinsicDoubleCvt, kIntrinsicFloatCvt.
108 kIntrinsicFlagToFloatingPoint = kIntrinsicFlagMin,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000109};
110
111struct InlineIGetIPutData {
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000112 // The op_variant below is DexMemAccessType but the runtime doesn't know that enumeration.
Vladimir Markoe3e02602014-03-12 15:42:41 +0000113 uint16_t op_variant : 3;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100114 uint16_t method_is_static : 1;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000115 uint16_t object_arg : 4;
116 uint16_t src_arg : 4; // iput only
Vladimir Markoe1fced12014-04-04 14:52:53 +0100117 uint16_t return_arg_plus1 : 4; // iput only, method argument to return + 1, 0 = return void.
Vladimir Markoe3e02602014-03-12 15:42:41 +0000118 uint16_t field_idx;
119 uint32_t is_volatile : 1;
120 uint32_t field_offset : 31;
121};
Andreas Gampe575e78c2014-11-03 23:41:03 -0800122static_assert(sizeof(InlineIGetIPutData) == sizeof(uint64_t), "Invalid size of InlineIGetIPutData");
Vladimir Markoe3e02602014-03-12 15:42:41 +0000123
124struct InlineReturnArgData {
125 uint16_t arg;
126 uint16_t is_wide : 1;
127 uint16_t is_object : 1;
128 uint16_t reserved : 14;
129 uint32_t reserved2;
130};
Andreas Gampe575e78c2014-11-03 23:41:03 -0800131static_assert(sizeof(InlineReturnArgData) == sizeof(uint64_t),
132 "Invalid size of InlineReturnArgData");
Vladimir Markoe3e02602014-03-12 15:42:41 +0000133
134struct InlineMethod {
135 InlineMethodOpcode opcode;
136 InlineMethodFlags flags;
137 union {
138 uint64_t data;
139 InlineIGetIPutData ifield_data;
140 InlineReturnArgData return_data;
141 } d;
142};
143
144class InlineMethodAnalyser {
145 public:
146 /**
147 * Analyse method code to determine if the method is a candidate for inlining.
148 * If it is, record the inlining data.
149 *
150 * @param verifier the method verifier holding data about the method to analyse.
151 * @param method placeholder for the inline method data.
152 * @return true if the method is a candidate for inlining, false otherwise.
153 */
154 static bool AnalyseMethodCode(verifier::MethodVerifier* verifier, InlineMethod* method)
155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
156
157 static constexpr bool IsInstructionIGet(Instruction::Code opcode) {
158 return Instruction::IGET <= opcode && opcode <= Instruction::IGET_SHORT;
159 }
160
161 static constexpr bool IsInstructionIPut(Instruction::Code opcode) {
162 return Instruction::IPUT <= opcode && opcode <= Instruction::IPUT_SHORT;
163 }
164
165 static constexpr uint16_t IGetVariant(Instruction::Code opcode) {
166 return opcode - Instruction::IGET;
167 }
168
169 static constexpr uint16_t IPutVariant(Instruction::Code opcode) {
170 return opcode - Instruction::IPUT;
171 }
172
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100173 // Determines whether the method is a synthetic accessor (method name starts with "access$").
174 static bool IsSyntheticAccessor(MethodReference ref);
175
Vladimir Markoe3e02602014-03-12 15:42:41 +0000176 private:
177 static bool AnalyseReturnMethod(const DexFile::CodeItem* code_item, InlineMethod* result);
178 static bool AnalyseConstMethod(const DexFile::CodeItem* code_item, InlineMethod* result);
179 static bool AnalyseIGetMethod(verifier::MethodVerifier* verifier, InlineMethod* result)
180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
181 static bool AnalyseIPutMethod(verifier::MethodVerifier* verifier, InlineMethod* result)
182 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
183
184 // Can we fast path instance field access in a verified accessor?
185 // If yes, computes field's offset and volatility and whether the method is static or not.
186 static bool ComputeSpecialAccessorInfo(uint32_t field_idx, bool is_put,
187 verifier::MethodVerifier* verifier,
188 InlineIGetIPutData* result)
189 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
190};
191
192} // namespace art
193
194#endif // ART_RUNTIME_QUICK_INLINE_METHOD_ANALYSER_H_