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