blob: 741fb64fd8823bd57aee74c2149412b75164851b [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)
Andreas Gampe7c3952f2015-02-19 18:21:24 -080034 : HOptimization(graph, true, 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;
57#define OPTIMIZING_INTRINSICS(Name, IsStatic) \
58 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
72#define OPTIMIZING_INTRINSICS(Name, IsStatic) \
73 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
119} // namespace art
120
121#endif // ART_COMPILER_OPTIMIZING_INTRINSICS_H_