blob: daec634f281cf71741052fc7622968686f42228b [file] [log] [blame]
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001/*
2 * Copyright (C) 2015 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#include "intrinsics.h"
18
Andreas Gampea1d2f952017-04-20 22:53:58 -070019#include "art_field-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080020#include "art_method-inl.h"
Andreas Gampebfb5ba92015-09-01 15:45:02 +000021#include "class_linker.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080022#include "driver/compiler_driver.h"
Nicolas Geoffray331605a2017-03-01 11:01:41 +000023#include "driver/compiler_options.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "invoke_type.h"
Andreas Gampebfb5ba92015-09-01 15:45:02 +000025#include "mirror/dex_cache-inl.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080026#include "nodes.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070028#include "thread-current-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010029#include "utils.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080030
31namespace art {
32
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +010033// Check that intrinsic enum values fit within space set aside in ArtMethod modifier flags.
34#define CHECK_INTRINSICS_ENUM_VALUES(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \
35 static_assert( \
36 static_cast<uint32_t>(Intrinsics::k ## Name) <= (kAccIntrinsicBits >> CTZ(kAccIntrinsicBits)), \
37 "Instrinsics enumeration space overflow: ");
38#include "intrinsics_list.h"
39 INTRINSICS_LIST(CHECK_INTRINSICS_ENUM_VALUES)
40#undef INTRINSICS_LIST
41#undef CHECK_INTRINSICS_ENUM_VALUES
42
Andreas Gampe71fb52f2014-12-29 17:43:08 -080043// Function that returns whether an intrinsic is static/direct or virtual.
44static inline InvokeType GetIntrinsicInvokeType(Intrinsics i) {
45 switch (i) {
46 case Intrinsics::kNone:
47 return kInterface; // Non-sensical for intrinsic.
Nicolas Geoffray762869d2016-07-15 15:28:35 +010048#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \
Aart Bik5d75afe2015-12-14 11:57:01 -080049 case Intrinsics::k ## Name: \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080050 return IsStatic;
51#include "intrinsics_list.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070052 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
Andreas Gampe71fb52f2014-12-29 17:43:08 -080053#undef INTRINSICS_LIST
54#undef OPTIMIZING_INTRINSICS
55 }
56 return kInterface;
57}
58
agicsaki57b81ec2015-08-11 17:39:37 -070059// Function that returns whether an intrinsic needs an environment or not.
Agi Csaki05f20562015-08-19 14:58:14 -070060static inline IntrinsicNeedsEnvironmentOrCache NeedsEnvironmentOrCache(Intrinsics i) {
agicsaki57b81ec2015-08-11 17:39:37 -070061 switch (i) {
62 case Intrinsics::kNone:
Agi Csaki05f20562015-08-19 14:58:14 -070063 return kNeedsEnvironmentOrCache; // Non-sensical for intrinsic.
Nicolas Geoffray762869d2016-07-15 15:28:35 +010064#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \
Aart Bik5d75afe2015-12-14 11:57:01 -080065 case Intrinsics::k ## Name: \
Agi Csaki05f20562015-08-19 14:58:14 -070066 return NeedsEnvironmentOrCache;
agicsaki57b81ec2015-08-11 17:39:37 -070067#include "intrinsics_list.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070068 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
agicsaki57b81ec2015-08-11 17:39:37 -070069#undef INTRINSICS_LIST
70#undef OPTIMIZING_INTRINSICS
71 }
Agi Csaki05f20562015-08-19 14:58:14 -070072 return kNeedsEnvironmentOrCache;
agicsaki57b81ec2015-08-11 17:39:37 -070073}
Andreas Gampe71fb52f2014-12-29 17:43:08 -080074
Aart Bik5d75afe2015-12-14 11:57:01 -080075// Function that returns whether an intrinsic has side effects.
76static inline IntrinsicSideEffects GetSideEffects(Intrinsics i) {
77 switch (i) {
78 case Intrinsics::kNone:
79 return kAllSideEffects;
Nicolas Geoffray762869d2016-07-15 15:28:35 +010080#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \
Aart Bik5d75afe2015-12-14 11:57:01 -080081 case Intrinsics::k ## Name: \
82 return SideEffects;
83#include "intrinsics_list.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070084 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
Aart Bik5d75afe2015-12-14 11:57:01 -080085#undef INTRINSICS_LIST
86#undef OPTIMIZING_INTRINSICS
87 }
88 return kAllSideEffects;
89}
90
91// Function that returns whether an intrinsic can throw exceptions.
92static inline IntrinsicExceptions GetExceptions(Intrinsics i) {
93 switch (i) {
94 case Intrinsics::kNone:
95 return kCanThrow;
Nicolas Geoffray762869d2016-07-15 15:28:35 +010096#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \
Aart Bik5d75afe2015-12-14 11:57:01 -080097 case Intrinsics::k ## Name: \
98 return Exceptions;
99#include "intrinsics_list.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -0700100 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
Aart Bik5d75afe2015-12-14 11:57:01 -0800101#undef INTRINSICS_LIST
102#undef OPTIMIZING_INTRINSICS
103 }
104 return kCanThrow;
105}
106
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100107static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000108 // Whenever the intrinsic is marked as static, report an error if we find an InvokeVirtual.
109 //
110 // Whenever the intrinsic is marked as direct and we find an InvokeVirtual, a devirtualization
111 // failure occured. We might be in a situation where we have inlined a method that calls an
112 // intrinsic, but that method is in a different dex file on which we do not have a
113 // verified_method that would have helped the compiler driver sharpen the call. In that case,
114 // make sure that the intrinsic is actually for some final method (or in a final class), as
115 // otherwise the intrinsics setup is broken.
116 //
117 // For the last direction, we have intrinsics for virtual functions that will perform a check
118 // inline. If the precise type is known, however, the instruction will be sharpened to an
119 // InvokeStaticOrDirect.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800120 InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100121 InvokeType invoke_type = invoke->GetInvokeType();
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +0100122
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800123 switch (intrinsic_type) {
124 case kStatic:
125 return (invoke_type == kStatic);
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000126
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800127 case kDirect:
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000128 if (invoke_type == kDirect) {
129 return true;
130 }
131 if (invoke_type == kVirtual) {
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100132 ArtMethod* art_method = invoke->GetResolvedMethod();
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000133 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100134 return (art_method->IsFinal() || art_method->GetDeclaringClass()->IsFinal());
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000135 }
136 return false;
137
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800138 case kVirtual:
139 // Call might be devirtualized.
140 return (invoke_type == kVirtual || invoke_type == kDirect);
141
142 default:
143 return false;
144 }
145}
146
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800147void IntrinsicsRecognizer::Run() {
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100148 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100149 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800150 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
151 inst_it.Advance()) {
152 HInstruction* inst = inst_it.Current();
153 if (inst->IsInvoke()) {
154 HInvoke* invoke = inst->AsInvoke();
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100155 ArtMethod* art_method = invoke->GetResolvedMethod();
156 if (art_method != nullptr && art_method->IsIntrinsic()) {
157 Intrinsics intrinsic = static_cast<Intrinsics>(art_method->GetIntrinsic());
158 if (!CheckInvokeType(intrinsic, invoke)) {
159 LOG(WARNING) << "Found an intrinsic with unexpected invoke type: "
Mathieu Chartierf044c222017-05-31 15:27:54 -0700160 << static_cast<uint32_t>(intrinsic) << " for "
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000161 << art_method->PrettyMethod()
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100162 << invoke->DebugName();
163 } else {
164 invoke->SetIntrinsic(intrinsic,
165 NeedsEnvironmentOrCache(intrinsic),
166 GetSideEffects(intrinsic),
167 GetExceptions(intrinsic));
Igor Murashkin1e065a52017-08-09 13:20:34 -0700168 MaybeRecordStat(stats_,
169 MethodCompilationStat::kIntrinsicRecognized);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800170 }
171 }
172 }
173 }
174 }
175}
176
177std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) {
178 switch (intrinsic) {
179 case Intrinsics::kNone:
David Brazdil109c89a2015-07-31 17:10:43 +0100180 os << "None";
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800181 break;
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100182#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800183 case Intrinsics::k ## Name: \
184 os << # Name; \
185 break;
186#include "intrinsics_list.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -0700187 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800188#undef STATIC_INTRINSICS_LIST
189#undef VIRTUAL_INTRINSICS_LIST
190#undef OPTIMIZING_INTRINSICS
191 }
192 return os;
193}
194
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000195void IntrinsicVisitor::ComputeIntegerValueOfLocations(HInvoke* invoke,
196 CodeGenerator* codegen,
197 Location return_location,
198 Location first_argument_location) {
199 if (Runtime::Current()->IsAotCompiler()) {
200 if (codegen->GetCompilerOptions().IsBootImage() ||
201 codegen->GetCompilerOptions().GetCompilePic()) {
202 // TODO(ngeoffray): Support boot image compilation.
203 return;
204 }
205 }
206
207 IntegerValueOfInfo info = ComputeIntegerValueOfInfo();
208
209 // Most common case is that we have found all we needed (classes are initialized
210 // and in the boot image). Bail if not.
211 if (info.integer_cache == nullptr ||
212 info.integer == nullptr ||
213 info.cache == nullptr ||
214 info.value_offset == 0 ||
215 // low and high cannot be 0, per the spec.
216 info.low == 0 ||
217 info.high == 0) {
218 LOG(INFO) << "Integer.valueOf will not be optimized";
219 return;
220 }
221
222 // The intrinsic will call if it needs to allocate a j.l.Integer.
223 LocationSummary* locations = new (invoke->GetBlock()->GetGraph()->GetArena()) LocationSummary(
224 invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
225 if (!invoke->InputAt(0)->IsConstant()) {
226 locations->SetInAt(0, Location::RequiresRegister());
227 }
228 locations->AddTemp(first_argument_location);
229 locations->SetOut(return_location);
230}
231
232IntrinsicVisitor::IntegerValueOfInfo IntrinsicVisitor::ComputeIntegerValueOfInfo() {
233 // Note that we could cache all of the data looked up here. but there's no good
234 // location for it. We don't want to add it to WellKnownClasses, to avoid creating global
235 // jni values. Adding it as state to the compiler singleton seems like wrong
236 // separation of concerns.
237 // The need for this data should be pretty rare though.
238
239 // The most common case is that the classes are in the boot image and initialized,
240 // which is easy to generate code for. We bail if not.
241 Thread* self = Thread::Current();
242 ScopedObjectAccess soa(self);
243 Runtime* runtime = Runtime::Current();
244 ClassLinker* class_linker = runtime->GetClassLinker();
245 gc::Heap* heap = runtime->GetHeap();
246 IntegerValueOfInfo info;
247 info.integer_cache = class_linker->FindSystemClass(self, "Ljava/lang/Integer$IntegerCache;");
248 if (info.integer_cache == nullptr) {
249 self->ClearException();
250 return info;
251 }
252 if (!heap->ObjectIsInBootImageSpace(info.integer_cache) || !info.integer_cache->IsInitialized()) {
253 // Optimization only works if the class is initialized and in the boot image.
254 return info;
255 }
256 info.integer = class_linker->FindSystemClass(self, "Ljava/lang/Integer;");
257 if (info.integer == nullptr) {
258 self->ClearException();
259 return info;
260 }
261 if (!heap->ObjectIsInBootImageSpace(info.integer) || !info.integer->IsInitialized()) {
262 // Optimization only works if the class is initialized and in the boot image.
263 return info;
264 }
265
266 ArtField* field = info.integer_cache->FindDeclaredStaticField("cache", "[Ljava/lang/Integer;");
267 if (field == nullptr) {
268 return info;
269 }
270 info.cache = static_cast<mirror::ObjectArray<mirror::Object>*>(
271 field->GetObject(info.integer_cache).Ptr());
272 if (info.cache == nullptr) {
273 return info;
274 }
275
276 if (!heap->ObjectIsInBootImageSpace(info.cache)) {
277 // Optimization only works if the object is in the boot image.
278 return info;
279 }
280
281 field = info.integer->FindDeclaredInstanceField("value", "I");
282 if (field == nullptr) {
283 return info;
284 }
285 info.value_offset = field->GetOffset().Int32Value();
286
287 field = info.integer_cache->FindDeclaredStaticField("low", "I");
288 if (field == nullptr) {
289 return info;
290 }
291 info.low = field->GetInt(info.integer_cache);
292
293 field = info.integer_cache->FindDeclaredStaticField("high", "I");
294 if (field == nullptr) {
295 return info;
296 }
297 info.high = field->GetInt(info.integer_cache);
298
299 DCHECK_EQ(info.cache->GetLength(), info.high - info.low + 1);
300 return info;
301}
302
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800303} // namespace art