blob: 8c69d9b643df153480b135acc51c41d1a2328e9a [file] [log] [blame]
Andreas Gampe85b62f22015-09-09 13:15:38 -07001/*
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_UTILS_H_
18#define ART_COMPILER_OPTIMIZING_INTRINSICS_UTILS_H_
19
20#include "base/macros.h"
21#include "code_generator.h"
22#include "locations.h"
23#include "nodes.h"
24#include "utils/assembler.h"
25#include "utils/label.h"
26
27namespace art {
28
29// Default slow-path for fallback (calling the managed code to handle the intrinsic) in an
30// intrinsified call. This will copy the arguments into the positions for a regular call.
31//
32// Note: The actual parameters are required to be in the locations given by the invoke's location
33// summary. If an intrinsic modifies those locations before a slowpath call, they must be
34// restored!
35//
36// Note: If an invoke wasn't sharpened, we will put down an invoke-virtual here. That's potentially
37// sub-optimal (compared to a direct pointer call), but this is a slow-path.
38
39template <typename TDexCallingConvention>
40class IntrinsicSlowPath : public SlowPathCode {
41 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000042 explicit IntrinsicSlowPath(HInvoke* invoke) : SlowPathCode(invoke), invoke_(invoke) { }
Andreas Gampe85b62f22015-09-09 13:15:38 -070043
44 Location MoveArguments(CodeGenerator* codegen) {
45 TDexCallingConvention calling_convention_visitor;
46 IntrinsicVisitor::MoveArguments(invoke_, codegen, &calling_convention_visitor);
47 return calling_convention_visitor.GetMethodLocation();
48 }
49
50 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
51 Assembler* assembler = codegen->GetAssembler();
52 assembler->Bind(GetEntryLabel());
53
54 SaveLiveRegisters(codegen, invoke_->GetLocations());
55
56 Location method_loc = MoveArguments(codegen);
57
58 if (invoke_->IsInvokeStaticOrDirect()) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +010059 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(), method_loc, this);
Andreas Gampe85b62f22015-09-09 13:15:38 -070060 } else {
Vladimir Markoe7197bf2017-06-02 17:00:23 +010061 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), method_loc, this);
Andreas Gampe85b62f22015-09-09 13:15:38 -070062 }
Andreas Gampe85b62f22015-09-09 13:15:38 -070063
64 // Copy the result back to the expected output.
65 Location out = invoke_->GetLocations()->Out();
66 if (out.IsValid()) {
67 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
68 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
69 codegen->MoveFromReturnRegister(out, invoke_->GetType());
70 }
71
72 RestoreLiveRegisters(codegen, invoke_->GetLocations());
73 assembler->Jump(GetExitLabel());
74 }
75
76 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPath"; }
77
78 private:
79 // The instruction where this slow path is happening.
80 HInvoke* const invoke_;
81
82 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPath);
83};
84
85} // namespace art
86
87#endif // ART_COMPILER_OPTIMIZING_INTRINSICS_UTILS_H_