blob: dd3703cad5029a1610931dd51b2fea89902d0542 [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,
Vladimir Markoe3e02602014-03-12 15:42:41 +000043 kIntrinsicAbsInt,
44 kIntrinsicAbsLong,
45 kIntrinsicAbsFloat,
46 kIntrinsicAbsDouble,
47 kIntrinsicMinMaxInt,
Serban Constantinescu23abec92014-07-02 16:13:38 +010048 kIntrinsicMinMaxLong,
49 kIntrinsicMinMaxFloat,
50 kIntrinsicMinMaxDouble,
Vladimir Markoe3e02602014-03-12 15:42:41 +000051 kIntrinsicSqrt,
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +010052 kIntrinsicCeil,
53 kIntrinsicFloor,
54 kIntrinsicRint,
55 kIntrinsicRoundFloat,
56 kIntrinsicRoundDouble,
Mathieu Chartiercd48f2d2014-09-09 13:51:09 -070057 kIntrinsicReferenceGetReferent,
Vladimir Markoe3e02602014-03-12 15:42:41 +000058 kIntrinsicCharAt,
59 kIntrinsicCompareTo,
agicsaki7da072f2015-08-12 20:30:17 -070060 kIntrinsicEquals,
Jeff Hao848f70a2014-01-15 13:49:50 -080061 kIntrinsicGetCharsNoCheck,
Vladimir Markoe3e02602014-03-12 15:42:41 +000062 kIntrinsicIsEmptyOrLength,
63 kIntrinsicIndexOf,
Jeff Hao848f70a2014-01-15 13:49:50 -080064 kIntrinsicNewStringFromBytes,
65 kIntrinsicNewStringFromChars,
66 kIntrinsicNewStringFromString,
Vladimir Markoe3e02602014-03-12 15:42:41 +000067 kIntrinsicCurrentThread,
68 kIntrinsicPeek,
69 kIntrinsicPoke,
70 kIntrinsicCas,
71 kIntrinsicUnsafeGet,
72 kIntrinsicUnsafePut,
DaniilSokolov70c4f062014-06-24 17:34:00 -070073 kIntrinsicSystemArrayCopyCharArray,
Vladimir Markoe3e02602014-03-12 15:42:41 +000074
75 kInlineOpNop,
76 kInlineOpReturnArg,
77 kInlineOpNonWideConst,
78 kInlineOpIGet,
79 kInlineOpIPut,
Jeff Hao848f70a2014-01-15 13:49:50 -080080 kInlineStringInit,
Vladimir Markoe3e02602014-03-12 15:42:41 +000081};
82std::ostream& operator<<(std::ostream& os, const InlineMethodOpcode& rhs);
83
84enum InlineMethodFlags : uint16_t {
85 kNoInlineMethodFlags = 0x0000,
86 kInlineIntrinsic = 0x0001,
87 kInlineSpecial = 0x0002,
88};
89
90// IntrinsicFlags are stored in InlineMethod::d::raw_data
91enum IntrinsicFlags {
92 kIntrinsicFlagNone = 0,
93
94 // kIntrinsicMinMaxInt
95 kIntrinsicFlagMax = kIntrinsicFlagNone,
96 kIntrinsicFlagMin = 1,
97
98 // kIntrinsicIsEmptyOrLength
99 kIntrinsicFlagLength = kIntrinsicFlagNone,
100 kIntrinsicFlagIsEmpty = kIntrinsicFlagMin,
101
102 // kIntrinsicIndexOf
103 kIntrinsicFlagBase0 = kIntrinsicFlagMin,
104
105 // kIntrinsicUnsafeGet, kIntrinsicUnsafePut, kIntrinsicUnsafeCas
106 kIntrinsicFlagIsLong = kIntrinsicFlagMin,
107 // kIntrinsicUnsafeGet, kIntrinsicUnsafePut
108 kIntrinsicFlagIsVolatile = 2,
109 // kIntrinsicUnsafePut, kIntrinsicUnsafeCas
110 kIntrinsicFlagIsObject = 4,
111 // kIntrinsicUnsafePut
112 kIntrinsicFlagIsOrdered = 8,
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800113
114 // kIntrinsicDoubleCvt, kIntrinsicFloatCvt.
115 kIntrinsicFlagToFloatingPoint = kIntrinsicFlagMin,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000116};
117
118struct InlineIGetIPutData {
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000119 // The op_variant below is DexMemAccessType but the runtime doesn't know that enumeration.
Vladimir Markoe3e02602014-03-12 15:42:41 +0000120 uint16_t op_variant : 3;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100121 uint16_t method_is_static : 1;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000122 uint16_t object_arg : 4;
123 uint16_t src_arg : 4; // iput only
Vladimir Markoe1fced12014-04-04 14:52:53 +0100124 uint16_t return_arg_plus1 : 4; // iput only, method argument to return + 1, 0 = return void.
Vladimir Markoe3e02602014-03-12 15:42:41 +0000125 uint16_t field_idx;
126 uint32_t is_volatile : 1;
127 uint32_t field_offset : 31;
128};
Andreas Gampe575e78c2014-11-03 23:41:03 -0800129static_assert(sizeof(InlineIGetIPutData) == sizeof(uint64_t), "Invalid size of InlineIGetIPutData");
Vladimir Markoe3e02602014-03-12 15:42:41 +0000130
131struct InlineReturnArgData {
132 uint16_t arg;
133 uint16_t is_wide : 1;
134 uint16_t is_object : 1;
135 uint16_t reserved : 14;
136 uint32_t reserved2;
137};
Andreas Gampe575e78c2014-11-03 23:41:03 -0800138static_assert(sizeof(InlineReturnArgData) == sizeof(uint64_t),
139 "Invalid size of InlineReturnArgData");
Vladimir Markoe3e02602014-03-12 15:42:41 +0000140
141struct InlineMethod {
142 InlineMethodOpcode opcode;
143 InlineMethodFlags flags;
144 union {
145 uint64_t data;
146 InlineIGetIPutData ifield_data;
147 InlineReturnArgData return_data;
148 } d;
149};
150
151class InlineMethodAnalyser {
152 public:
153 /**
154 * Analyse method code to determine if the method is a candidate for inlining.
155 * If it is, record the inlining data.
156 *
157 * @param verifier the method verifier holding data about the method to analyse.
158 * @param method placeholder for the inline method data.
159 * @return true if the method is a candidate for inlining, false otherwise.
160 */
161 static bool AnalyseMethodCode(verifier::MethodVerifier* verifier, InlineMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700162 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000163
164 static constexpr bool IsInstructionIGet(Instruction::Code opcode) {
165 return Instruction::IGET <= opcode && opcode <= Instruction::IGET_SHORT;
166 }
167
168 static constexpr bool IsInstructionIPut(Instruction::Code opcode) {
169 return Instruction::IPUT <= opcode && opcode <= Instruction::IPUT_SHORT;
170 }
171
172 static constexpr uint16_t IGetVariant(Instruction::Code opcode) {
173 return opcode - Instruction::IGET;
174 }
175
176 static constexpr uint16_t IPutVariant(Instruction::Code opcode) {
177 return opcode - Instruction::IPUT;
178 }
179
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100180 // Determines whether the method is a synthetic accessor (method name starts with "access$").
181 static bool IsSyntheticAccessor(MethodReference ref);
182
Vladimir Markoe3e02602014-03-12 15:42:41 +0000183 private:
184 static bool AnalyseReturnMethod(const DexFile::CodeItem* code_item, InlineMethod* result);
185 static bool AnalyseConstMethod(const DexFile::CodeItem* code_item, InlineMethod* result);
186 static bool AnalyseIGetMethod(verifier::MethodVerifier* verifier, InlineMethod* result)
Mathieu Chartier90443472015-07-16 20:32:27 -0700187 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000188 static bool AnalyseIPutMethod(verifier::MethodVerifier* verifier, InlineMethod* result)
Mathieu Chartier90443472015-07-16 20:32:27 -0700189 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000190
191 // Can we fast path instance field access in a verified accessor?
192 // If yes, computes field's offset and volatility and whether the method is static or not.
193 static bool ComputeSpecialAccessorInfo(uint32_t field_idx, bool is_put,
194 verifier::MethodVerifier* verifier,
195 InlineIGetIPutData* result)
Mathieu Chartier90443472015-07-16 20:32:27 -0700196 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000197};
198
199} // namespace art
200
201#endif // ART_RUNTIME_QUICK_INLINE_METHOD_ANALYSER_H_