1 //=- AArch64MachineFunctionInfo.cpp - AArch64 Machine Function Info ---------=//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
11 /// This file implements AArch64-specific per-machine-function
14 //===----------------------------------------------------------------------===//
16 #include "AArch64MachineFunctionInfo.h"
17 #include "AArch64InstrInfo.h"
18 #include <llvm/IR/Metadata.h>
19 #include <llvm/IR/Module.h>
23 yaml::AArch64FunctionInfo::AArch64FunctionInfo(
24 const llvm::AArch64FunctionInfo
&MFI
)
25 : HasRedZone(MFI
.hasRedZone()) {}
27 void yaml::AArch64FunctionInfo::mappingImpl(yaml::IO
&YamlIO
) {
28 MappingTraits
<AArch64FunctionInfo
>::mapping(YamlIO
, *this);
31 void AArch64FunctionInfo::initializeBaseYamlFields(
32 const yaml::AArch64FunctionInfo
&YamlMFI
) {
33 if (YamlMFI
.HasRedZone
.hasValue())
34 HasRedZone
= YamlMFI
.HasRedZone
;
37 static std::pair
<bool, bool> GetSignReturnAddress(const Function
&F
) {
38 // The function should be signed in the following situations:
39 // - sign-return-address=all
40 // - sign-return-address=non-leaf and the functions spills the LR
41 if (!F
.hasFnAttribute("sign-return-address")) {
42 const Module
&M
= *F
.getParent();
43 if (const auto *Sign
= mdconst::extract_or_null
<ConstantInt
>(
44 M
.getModuleFlag("sign-return-address"))) {
45 if (Sign
->getZExtValue()) {
46 if (const auto *All
= mdconst::extract_or_null
<ConstantInt
>(
47 M
.getModuleFlag("sign-return-address-all")))
48 return {true, All
->getZExtValue()};
52 return {false, false};
55 StringRef Scope
= F
.getFnAttribute("sign-return-address").getValueAsString();
56 if (Scope
.equals("none"))
57 return {false, false};
59 if (Scope
.equals("all"))
62 assert(Scope
.equals("non-leaf"));
66 static bool ShouldSignWithBKey(const Function
&F
) {
67 if (!F
.hasFnAttribute("sign-return-address-key")) {
68 if (const auto *BKey
= mdconst::extract_or_null
<ConstantInt
>(
69 F
.getParent()->getModuleFlag("sign-return-address-with-bkey")))
70 return BKey
->getZExtValue();
75 F
.getFnAttribute("sign-return-address-key").getValueAsString();
76 assert(Key
.equals_insensitive("a_key") || Key
.equals_insensitive("b_key"));
77 return Key
.equals_insensitive("b_key");
80 AArch64FunctionInfo::AArch64FunctionInfo(MachineFunction
&MF
) : MF(MF
) {
81 // If we already know that the function doesn't have a redzone, set
83 if (MF
.getFunction().hasFnAttribute(Attribute::NoRedZone
))
86 const Function
&F
= MF
.getFunction();
87 std::tie(SignReturnAddress
, SignReturnAddressAll
) = GetSignReturnAddress(F
);
88 SignWithBKey
= ShouldSignWithBKey(F
);
90 if (!F
.hasFnAttribute("branch-target-enforcement")) {
91 if (const auto *BTE
= mdconst::extract_or_null
<ConstantInt
>(
92 F
.getParent()->getModuleFlag("branch-target-enforcement")))
93 BranchTargetEnforcement
= BTE
->getZExtValue();
97 const StringRef BTIEnable
=
98 F
.getFnAttribute("branch-target-enforcement").getValueAsString();
99 assert(BTIEnable
.equals_insensitive("true") ||
100 BTIEnable
.equals_insensitive("false"));
101 BranchTargetEnforcement
= BTIEnable
.equals_insensitive("true");
104 bool AArch64FunctionInfo::shouldSignReturnAddress(bool SpillsLR
) const {
105 if (!SignReturnAddress
)
107 if (SignReturnAddressAll
)
112 bool AArch64FunctionInfo::shouldSignReturnAddress() const {
113 return shouldSignReturnAddress(llvm::any_of(
114 MF
.getFrameInfo().getCalleeSavedInfo(),
115 [](const auto &Info
) { return Info
.getReg() == AArch64::LR
; }));