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