blob: 837662d879e887929dac0a458a2acf4d78684635 [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,
Scott Wakeling611d3392015-07-10 11:42:06 +010042 kIntrinsicNumberOfLeadingZeros,
Scott Wakeling9ee23f42015-07-23 10:44:35 +010043 kIntrinsicNumberOfTrailingZeros,
44 kIntrinsicRotateRight,
45 kIntrinsicRotateLeft,
Vladimir Markoe3e02602014-03-12 15:42:41 +000046 kIntrinsicAbsInt,
47 kIntrinsicAbsLong,
48 kIntrinsicAbsFloat,
49 kIntrinsicAbsDouble,
50 kIntrinsicMinMaxInt,
Serban Constantinescu23abec92014-07-02 16:13:38 +010051 kIntrinsicMinMaxLong,
52 kIntrinsicMinMaxFloat,
53 kIntrinsicMinMaxDouble,
Vladimir Markoe3e02602014-03-12 15:42:41 +000054 kIntrinsicSqrt,
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +010055 kIntrinsicCeil,
56 kIntrinsicFloor,
57 kIntrinsicRint,
58 kIntrinsicRoundFloat,
59 kIntrinsicRoundDouble,
Mathieu Chartiercd48f2d2014-09-09 13:51:09 -070060 kIntrinsicReferenceGetReferent,
Vladimir Markoe3e02602014-03-12 15:42:41 +000061 kIntrinsicCharAt,
62 kIntrinsicCompareTo,
agicsaki7da072f2015-08-12 20:30:17 -070063 kIntrinsicEquals,
Jeff Hao848f70a2014-01-15 13:49:50 -080064 kIntrinsicGetCharsNoCheck,
Vladimir Markoe3e02602014-03-12 15:42:41 +000065 kIntrinsicIsEmptyOrLength,
66 kIntrinsicIndexOf,
Jeff Hao848f70a2014-01-15 13:49:50 -080067 kIntrinsicNewStringFromBytes,
68 kIntrinsicNewStringFromChars,
69 kIntrinsicNewStringFromString,
Vladimir Markoe3e02602014-03-12 15:42:41 +000070 kIntrinsicCurrentThread,
71 kIntrinsicPeek,
72 kIntrinsicPoke,
73 kIntrinsicCas,
74 kIntrinsicUnsafeGet,
75 kIntrinsicUnsafePut,
DaniilSokolov70c4f062014-06-24 17:34:00 -070076 kIntrinsicSystemArrayCopyCharArray,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +010077 kIntrinsicSystemArrayCopy,
Vladimir Markoe3e02602014-03-12 15:42:41 +000078
79 kInlineOpNop,
80 kInlineOpReturnArg,
81 kInlineOpNonWideConst,
82 kInlineOpIGet,
83 kInlineOpIPut,
Jeff Hao848f70a2014-01-15 13:49:50 -080084 kInlineStringInit,
Vladimir Markoe3e02602014-03-12 15:42:41 +000085};
86std::ostream& operator<<(std::ostream& os, const InlineMethodOpcode& rhs);
87
88enum InlineMethodFlags : uint16_t {
89 kNoInlineMethodFlags = 0x0000,
90 kInlineIntrinsic = 0x0001,
91 kInlineSpecial = 0x0002,
92};
93
94// IntrinsicFlags are stored in InlineMethod::d::raw_data
95enum IntrinsicFlags {
96 kIntrinsicFlagNone = 0,
97
98 // kIntrinsicMinMaxInt
99 kIntrinsicFlagMax = kIntrinsicFlagNone,
100 kIntrinsicFlagMin = 1,
101
102 // kIntrinsicIsEmptyOrLength
103 kIntrinsicFlagLength = kIntrinsicFlagNone,
104 kIntrinsicFlagIsEmpty = kIntrinsicFlagMin,
105
106 // kIntrinsicIndexOf
107 kIntrinsicFlagBase0 = kIntrinsicFlagMin,
108
109 // kIntrinsicUnsafeGet, kIntrinsicUnsafePut, kIntrinsicUnsafeCas
110 kIntrinsicFlagIsLong = kIntrinsicFlagMin,
111 // kIntrinsicUnsafeGet, kIntrinsicUnsafePut
112 kIntrinsicFlagIsVolatile = 2,
113 // kIntrinsicUnsafePut, kIntrinsicUnsafeCas
114 kIntrinsicFlagIsObject = 4,
115 // kIntrinsicUnsafePut
116 kIntrinsicFlagIsOrdered = 8,
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800117
118 // kIntrinsicDoubleCvt, kIntrinsicFloatCvt.
119 kIntrinsicFlagToFloatingPoint = kIntrinsicFlagMin,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000120};
121
122struct InlineIGetIPutData {
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000123 // The op_variant below is DexMemAccessType but the runtime doesn't know that enumeration.
Vladimir Markoe3e02602014-03-12 15:42:41 +0000124 uint16_t op_variant : 3;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100125 uint16_t method_is_static : 1;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000126 uint16_t object_arg : 4;
127 uint16_t src_arg : 4; // iput only
Vladimir Markoe1fced12014-04-04 14:52:53 +0100128 uint16_t return_arg_plus1 : 4; // iput only, method argument to return + 1, 0 = return void.
Vladimir Markoe3e02602014-03-12 15:42:41 +0000129 uint16_t field_idx;
130 uint32_t is_volatile : 1;
131 uint32_t field_offset : 31;
132};
Andreas Gampe575e78c2014-11-03 23:41:03 -0800133static_assert(sizeof(InlineIGetIPutData) == sizeof(uint64_t), "Invalid size of InlineIGetIPutData");
Vladimir Markoe3e02602014-03-12 15:42:41 +0000134
135struct InlineReturnArgData {
136 uint16_t arg;
137 uint16_t is_wide : 1;
138 uint16_t is_object : 1;
139 uint16_t reserved : 14;
140 uint32_t reserved2;
141};
Andreas Gampe575e78c2014-11-03 23:41:03 -0800142static_assert(sizeof(InlineReturnArgData) == sizeof(uint64_t),
143 "Invalid size of InlineReturnArgData");
Vladimir Markoe3e02602014-03-12 15:42:41 +0000144
145struct InlineMethod {
146 InlineMethodOpcode opcode;
147 InlineMethodFlags flags;
148 union {
149 uint64_t data;
150 InlineIGetIPutData ifield_data;
151 InlineReturnArgData return_data;
152 } d;
153};
154
155class InlineMethodAnalyser {
156 public:
157 /**
158 * Analyse method code to determine if the method is a candidate for inlining.
159 * If it is, record the inlining data.
160 *
161 * @param verifier the method verifier holding data about the method to analyse.
162 * @param method placeholder for the inline method data.
163 * @return true if the method is a candidate for inlining, false otherwise.
164 */
165 static bool AnalyseMethodCode(verifier::MethodVerifier* verifier, InlineMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700166 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000167
168 static constexpr bool IsInstructionIGet(Instruction::Code opcode) {
169 return Instruction::IGET <= opcode && opcode <= Instruction::IGET_SHORT;
170 }
171
172 static constexpr bool IsInstructionIPut(Instruction::Code opcode) {
173 return Instruction::IPUT <= opcode && opcode <= Instruction::IPUT_SHORT;
174 }
175
176 static constexpr uint16_t IGetVariant(Instruction::Code opcode) {
177 return opcode - Instruction::IGET;
178 }
179
180 static constexpr uint16_t IPutVariant(Instruction::Code opcode) {
181 return opcode - Instruction::IPUT;
182 }
183
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100184 // Determines whether the method is a synthetic accessor (method name starts with "access$").
185 static bool IsSyntheticAccessor(MethodReference ref);
186
Vladimir Markoe3e02602014-03-12 15:42:41 +0000187 private:
188 static bool AnalyseReturnMethod(const DexFile::CodeItem* code_item, InlineMethod* result);
189 static bool AnalyseConstMethod(const DexFile::CodeItem* code_item, InlineMethod* result);
190 static bool AnalyseIGetMethod(verifier::MethodVerifier* verifier, InlineMethod* result)
Mathieu Chartier90443472015-07-16 20:32:27 -0700191 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000192 static bool AnalyseIPutMethod(verifier::MethodVerifier* verifier, InlineMethod* result)
Mathieu Chartier90443472015-07-16 20:32:27 -0700193 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000194
195 // Can we fast path instance field access in a verified accessor?
196 // If yes, computes field's offset and volatility and whether the method is static or not.
197 static bool ComputeSpecialAccessorInfo(uint32_t field_idx, bool is_put,
198 verifier::MethodVerifier* verifier,
199 InlineIGetIPutData* result)
Mathieu Chartier90443472015-07-16 20:32:27 -0700200 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000201};
202
203} // namespace art
204
205#endif // ART_RUNTIME_QUICK_INLINE_METHOD_ANALYSER_H_