blob: 29f815c1bef91321fb2863b7adcc41837e51801e [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
Vladimir Marko9922f002020-06-08 15:05:15 +010020#include "base/casts.h"
Andreas Gampe85b62f22015-09-09 13:15:38 -070021#include "base/macros.h"
22#include "code_generator.h"
23#include "locations.h"
24#include "nodes.h"
25#include "utils/assembler.h"
26#include "utils/label.h"
27
Vladimir Marko0a516052019-10-14 13:00:44 +000028namespace art {
Andreas Gampe85b62f22015-09-09 13:15:38 -070029
30// Default slow-path for fallback (calling the managed code to handle the intrinsic) in an
31// intrinsified call. This will copy the arguments into the positions for a regular call.
32//
33// Note: The actual parameters are required to be in the locations given by the invoke's location
34// summary. If an intrinsic modifies those locations before a slowpath call, they must be
35// restored!
36//
37// Note: If an invoke wasn't sharpened, we will put down an invoke-virtual here. That's potentially
38// sub-optimal (compared to a direct pointer call), but this is a slow-path.
39
Vladimir Marko9922f002020-06-08 15:05:15 +010040template <typename TDexCallingConvention,
41 typename TSlowPathCode = SlowPathCode,
42 typename TAssembler = Assembler>
43class IntrinsicSlowPath : public TSlowPathCode {
Andreas Gampe85b62f22015-09-09 13:15:38 -070044 public:
Vladimir Marko9922f002020-06-08 15:05:15 +010045 explicit IntrinsicSlowPath(HInvoke* invoke) : TSlowPathCode(invoke), invoke_(invoke) { }
Andreas Gampe85b62f22015-09-09 13:15:38 -070046
47 Location MoveArguments(CodeGenerator* codegen) {
48 TDexCallingConvention calling_convention_visitor;
49 IntrinsicVisitor::MoveArguments(invoke_, codegen, &calling_convention_visitor);
50 return calling_convention_visitor.GetMethodLocation();
51 }
52
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010053 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko9922f002020-06-08 15:05:15 +010054 TAssembler* assembler = down_cast<TAssembler*>(codegen->GetAssembler());
55 assembler->Bind(this->GetEntryLabel());
Andreas Gampe85b62f22015-09-09 13:15:38 -070056
Vladimir Marko9922f002020-06-08 15:05:15 +010057 this->SaveLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe85b62f22015-09-09 13:15:38 -070058
59 Location method_loc = MoveArguments(codegen);
60
61 if (invoke_->IsInvokeStaticOrDirect()) {
Vladimir Marko86c87522020-05-11 16:55:55 +010062 HInvokeStaticOrDirect* invoke_static_or_direct = invoke_->AsInvokeStaticOrDirect();
63 DCHECK_NE(invoke_static_or_direct->GetMethodLoadKind(),
64 HInvokeStaticOrDirect::MethodLoadKind::kRecursive);
65 DCHECK_NE(invoke_static_or_direct->GetCodePtrLocation(),
66 HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative);
67 codegen->GenerateStaticOrDirectCall(invoke_static_or_direct, method_loc, this);
Andreas Gampe85b62f22015-09-09 13:15:38 -070068 } else {
Vladimir Markoe7197bf2017-06-02 17:00:23 +010069 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), method_loc, this);
Andreas Gampe85b62f22015-09-09 13:15:38 -070070 }
Andreas Gampe85b62f22015-09-09 13:15:38 -070071
72 // Copy the result back to the expected output.
73 Location out = invoke_->GetLocations()->Out();
74 if (out.IsValid()) {
75 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
76 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
77 codegen->MoveFromReturnRegister(out, invoke_->GetType());
78 }
79
Vladimir Marko9922f002020-06-08 15:05:15 +010080 this->RestoreLiveRegisters(codegen, invoke_->GetLocations());
81 assembler->Jump(this->GetExitLabel());
Andreas Gampe85b62f22015-09-09 13:15:38 -070082 }
83
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010084 const char* GetDescription() const override { return "IntrinsicSlowPath"; }
Andreas Gampe85b62f22015-09-09 13:15:38 -070085
86 private:
87 // The instruction where this slow path is happening.
88 HInvoke* const invoke_;
89
90 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPath);
91};
92
93} // namespace art
94
95#endif // ART_COMPILER_OPTIMIZING_INTRINSICS_UTILS_H_