blob: 12e79faac3f95b99a3db644f128aabfbd2bb5b4f [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#ifndef ART_COMPILER_OPTIMIZING_INTRINSICS_H_
18#define ART_COMPILER_OPTIMIZING_INTRINSICS_H_
19
Roland Levillainec525fc2015-04-28 15:50:20 +010020#include "code_generator.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080021#include "nodes.h"
22#include "optimization.h"
Roland Levillainec525fc2015-04-28 15:50:20 +010023#include "parallel_move_resolver.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024
25namespace art {
26
27class CompilerDriver;
28class DexFile;
29
30// Recognize intrinsics from HInvoke nodes.
31class IntrinsicsRecognizer : public HOptimization {
32 public:
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +010033 IntrinsicsRecognizer(HGraph* graph, CompilerDriver* driver)
David Brazdil69ba7b72015-06-23 18:27:30 +010034 : HOptimization(graph, kIntrinsicsRecognizerPassName),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +010035 driver_(driver) {}
Andreas Gampe71fb52f2014-12-29 17:43:08 -080036
37 void Run() OVERRIDE;
38
Andreas Gampe7c3952f2015-02-19 18:21:24 -080039 static constexpr const char* kIntrinsicsRecognizerPassName = "intrinsics_recognition";
40
Andreas Gampe71fb52f2014-12-29 17:43:08 -080041 private:
Andreas Gampe71fb52f2014-12-29 17:43:08 -080042 CompilerDriver* driver_;
43
44 DISALLOW_COPY_AND_ASSIGN(IntrinsicsRecognizer);
45};
46
47class IntrinsicVisitor : public ValueObject {
48 public:
49 virtual ~IntrinsicVisitor() {}
50
51 // Dispatch logic.
52
53 void Dispatch(HInvoke* invoke) {
54 switch (invoke->GetIntrinsic()) {
55 case Intrinsics::kNone:
56 return;
agicsaki57b81ec2015-08-11 17:39:37 -070057#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironment) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080058 case Intrinsics::k ## Name: \
59 Visit ## Name(invoke); \
60 return;
61#include "intrinsics_list.h"
62INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
63#undef INTRINSICS_LIST
64#undef OPTIMIZING_INTRINSICS
65
66 // Do not put a default case. That way the compiler will complain if we missed a case.
67 }
68 }
69
70 // Define visitor methods.
71
agicsaki57b81ec2015-08-11 17:39:37 -070072#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironment) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080073 virtual void Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
74 }
75#include "intrinsics_list.h"
76INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
77#undef INTRINSICS_LIST
78#undef OPTIMIZING_INTRINSICS
79
Roland Levillainec525fc2015-04-28 15:50:20 +010080 static void MoveArguments(HInvoke* invoke,
81 CodeGenerator* codegen,
82 InvokeDexCallingConventionVisitor* calling_convention_visitor) {
83 if (kIsDebugBuild && invoke->IsInvokeStaticOrDirect()) {
84 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
85 // When we do not run baseline, explicit clinit checks triggered by static
86 // invokes must have been pruned by art::PrepareForRegisterAllocation.
87 DCHECK(codegen->IsBaseline() || !invoke_static_or_direct->IsStaticWithExplicitClinitCheck());
88 }
89
90 if (invoke->GetNumberOfArguments() == 0) {
91 // No argument to move.
92 return;
93 }
94
95 LocationSummary* locations = invoke->GetLocations();
96
97 // We're moving potentially two or more locations to locations that could overlap, so we need
98 // a parallel move resolver.
99 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
100
101 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
102 HInstruction* input = invoke->InputAt(i);
103 Location cc_loc = calling_convention_visitor->GetNextLocation(input->GetType());
104 Location actual_loc = locations->InAt(i);
105
106 parallel_move.AddMove(actual_loc, cc_loc, input->GetType(), nullptr);
107 }
108
109 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
110 }
111
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800112 protected:
113 IntrinsicVisitor() {}
114
115 private:
116 DISALLOW_COPY_AND_ASSIGN(IntrinsicVisitor);
117};
118
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100119#define GENERIC_OPTIMIZATION(name, bit) \
120 public: \
121 void Set##name() { SetBit(k##name); } \
122 bool Get##name() const { return IsBitSet(k##name); } \
123 private: \
124 static constexpr int k##name = bit
125
126class IntrinsicOptimizations : public ValueObject {
127 public:
128 IntrinsicOptimizations(HInvoke* invoke) : value_(invoke->GetIntrinsicOptimizations()) {}
129 IntrinsicOptimizations(const HInvoke& invoke) : value_(invoke.GetIntrinsicOptimizations()) {}
130
131 static constexpr int kNumberOfGenericOptimizations = 2;
132 GENERIC_OPTIMIZATION(DoesNotNeedDexCache, 0);
133 GENERIC_OPTIMIZATION(DoesNotNeedEnvironment, 1);
134
135 protected:
136 bool IsBitSet(uint32_t bit) const {
137 return (*value_ & (1 << bit)) != 0u;
138 }
139
140 void SetBit(uint32_t bit) {
141 *(const_cast<uint32_t*>(value_)) |= (1 << bit);
142 }
143
144 private:
145 const uint32_t *value_;
146
147 DISALLOW_COPY_AND_ASSIGN(IntrinsicOptimizations);
148};
149
150#undef GENERIC_OPTIMIZATION
151
152#define INTRINSIC_OPTIMIZATION(name, bit) \
153 public: \
154 void Set##name() { SetBit(k##name); } \
155 bool Get##name() const { return IsBitSet(k##name); } \
156 private: \
157 static constexpr int k##name = bit + kNumberOfGenericOptimizations
158
159class StringEqualsOptimizations : public IntrinsicOptimizations {
160 public:
161 StringEqualsOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {}
162
163 INTRINSIC_OPTIMIZATION(ArgumentNotNull, 0);
164 INTRINSIC_OPTIMIZATION(ArgumentIsString, 1);
165
166 private:
167 DISALLOW_COPY_AND_ASSIGN(StringEqualsOptimizations);
168};
169
170#undef INTRISIC_OPTIMIZATION
171
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800172} // namespace art
173
174#endif // ART_COMPILER_OPTIMIZING_INTRINSICS_H_