blob: fc6ff7b1977b009efba826d7e5e86d302f27cbe4 [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 Gampebfb5ba92015-09-01 15:45:02 +000019#include "art_method.h"
20#include "class_linker.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080021#include "driver/compiler_driver.h"
22#include "invoke_type.h"
Andreas Gampebfb5ba92015-09-01 15:45:02 +000023#include "mirror/dex_cache-inl.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "nodes.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
Andreas Gampebfb5ba92015-09-01 15:45:02 +000026#include "thread-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010027#include "utils.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080028
29namespace art {
30
31// Function that returns whether an intrinsic is static/direct or virtual.
32static inline InvokeType GetIntrinsicInvokeType(Intrinsics i) {
33 switch (i) {
34 case Intrinsics::kNone:
35 return kInterface; // Non-sensical for intrinsic.
Nicolas Geoffray762869d2016-07-15 15:28:35 +010036#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \
Aart Bik5d75afe2015-12-14 11:57:01 -080037 case Intrinsics::k ## Name: \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080038 return IsStatic;
39#include "intrinsics_list.h"
40INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
41#undef INTRINSICS_LIST
42#undef OPTIMIZING_INTRINSICS
43 }
44 return kInterface;
45}
46
agicsaki57b81ec2015-08-11 17:39:37 -070047// Function that returns whether an intrinsic needs an environment or not.
Agi Csaki05f20562015-08-19 14:58:14 -070048static inline IntrinsicNeedsEnvironmentOrCache NeedsEnvironmentOrCache(Intrinsics i) {
agicsaki57b81ec2015-08-11 17:39:37 -070049 switch (i) {
50 case Intrinsics::kNone:
Agi Csaki05f20562015-08-19 14:58:14 -070051 return kNeedsEnvironmentOrCache; // Non-sensical for intrinsic.
Nicolas Geoffray762869d2016-07-15 15:28:35 +010052#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \
Aart Bik5d75afe2015-12-14 11:57:01 -080053 case Intrinsics::k ## Name: \
Agi Csaki05f20562015-08-19 14:58:14 -070054 return NeedsEnvironmentOrCache;
agicsaki57b81ec2015-08-11 17:39:37 -070055#include "intrinsics_list.h"
56INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
57#undef INTRINSICS_LIST
58#undef OPTIMIZING_INTRINSICS
59 }
Agi Csaki05f20562015-08-19 14:58:14 -070060 return kNeedsEnvironmentOrCache;
agicsaki57b81ec2015-08-11 17:39:37 -070061}
Andreas Gampe71fb52f2014-12-29 17:43:08 -080062
Aart Bik5d75afe2015-12-14 11:57:01 -080063// Function that returns whether an intrinsic has side effects.
64static inline IntrinsicSideEffects GetSideEffects(Intrinsics i) {
65 switch (i) {
66 case Intrinsics::kNone:
67 return kAllSideEffects;
Nicolas Geoffray762869d2016-07-15 15:28:35 +010068#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \
Aart Bik5d75afe2015-12-14 11:57:01 -080069 case Intrinsics::k ## Name: \
70 return SideEffects;
71#include "intrinsics_list.h"
72INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
73#undef INTRINSICS_LIST
74#undef OPTIMIZING_INTRINSICS
75 }
76 return kAllSideEffects;
77}
78
79// Function that returns whether an intrinsic can throw exceptions.
80static inline IntrinsicExceptions GetExceptions(Intrinsics i) {
81 switch (i) {
82 case Intrinsics::kNone:
83 return kCanThrow;
Nicolas Geoffray762869d2016-07-15 15:28:35 +010084#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \
Aart Bik5d75afe2015-12-14 11:57:01 -080085 case Intrinsics::k ## Name: \
86 return Exceptions;
87#include "intrinsics_list.h"
88INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
89#undef INTRINSICS_LIST
90#undef OPTIMIZING_INTRINSICS
91 }
92 return kCanThrow;
93}
94
Nicolas Geoffray762869d2016-07-15 15:28:35 +010095static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +000096 // Whenever the intrinsic is marked as static, report an error if we find an InvokeVirtual.
97 //
98 // Whenever the intrinsic is marked as direct and we find an InvokeVirtual, a devirtualization
99 // failure occured. We might be in a situation where we have inlined a method that calls an
100 // intrinsic, but that method is in a different dex file on which we do not have a
101 // verified_method that would have helped the compiler driver sharpen the call. In that case,
102 // make sure that the intrinsic is actually for some final method (or in a final class), as
103 // otherwise the intrinsics setup is broken.
104 //
105 // For the last direction, we have intrinsics for virtual functions that will perform a check
106 // inline. If the precise type is known, however, the instruction will be sharpened to an
107 // InvokeStaticOrDirect.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800108 InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100109 InvokeType invoke_type = invoke->GetInvokeType();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800110 switch (intrinsic_type) {
111 case kStatic:
112 return (invoke_type == kStatic);
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000113
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800114 case kDirect:
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000115 if (invoke_type == kDirect) {
116 return true;
117 }
118 if (invoke_type == kVirtual) {
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100119 ArtMethod* art_method = invoke->GetResolvedMethod();
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000120 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100121 return (art_method->IsFinal() || art_method->GetDeclaringClass()->IsFinal());
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000122 }
123 return false;
124
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800125 case kVirtual:
126 // Call might be devirtualized.
127 return (invoke_type == kVirtual || invoke_type == kDirect);
128
129 default:
130 return false;
131 }
132}
133
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800134void IntrinsicsRecognizer::Run() {
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100135 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100136 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800137 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
138 inst_it.Advance()) {
139 HInstruction* inst = inst_it.Current();
140 if (inst->IsInvoke()) {
141 HInvoke* invoke = inst->AsInvoke();
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100142 ArtMethod* art_method = invoke->GetResolvedMethod();
143 if (art_method != nullptr && art_method->IsIntrinsic()) {
144 Intrinsics intrinsic = static_cast<Intrinsics>(art_method->GetIntrinsic());
145 if (!CheckInvokeType(intrinsic, invoke)) {
146 LOG(WARNING) << "Found an intrinsic with unexpected invoke type: "
147 << intrinsic << " for "
David Sehr709b0702016-10-13 09:12:37 -0700148 << invoke->GetDexFile().PrettyMethod(invoke->GetDexMethodIndex())
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100149 << invoke->DebugName();
150 } else {
151 invoke->SetIntrinsic(intrinsic,
152 NeedsEnvironmentOrCache(intrinsic),
153 GetSideEffects(intrinsic),
154 GetExceptions(intrinsic));
155 MaybeRecordStat(MethodCompilationStat::kIntrinsicRecognized);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800156 }
157 }
158 }
159 }
160 }
161}
162
163std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) {
164 switch (intrinsic) {
165 case Intrinsics::kNone:
David Brazdil109c89a2015-07-31 17:10:43 +0100166 os << "None";
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800167 break;
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100168#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800169 case Intrinsics::k ## Name: \
170 os << # Name; \
171 break;
172#include "intrinsics_list.h"
173INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
174#undef STATIC_INTRINSICS_LIST
175#undef VIRTUAL_INTRINSICS_LIST
176#undef OPTIMIZING_INTRINSICS
177 }
178 return os;
179}
180
181} // namespace art