1 //=- AArch64MachineFunctionInfo.cpp - AArch64 Machine Function Info ---------=//
2
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file implements AArch64-specific per-machine-function
12 /// information.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #include "AArch64MachineFunctionInfo.h"
17 #include "AArch64InstrInfo.h"
18 #include <llvm/IR/Metadata.h>
19 #include <llvm/IR/Module.h>
20
21 using namespace llvm;
22
AArch64FunctionInfo(const llvm::AArch64FunctionInfo & MFI)23 yaml::AArch64FunctionInfo::AArch64FunctionInfo(
24 const llvm::AArch64FunctionInfo &MFI)
25 : HasRedZone(MFI.hasRedZone()) {}
26
mappingImpl(yaml::IO & YamlIO)27 void yaml::AArch64FunctionInfo::mappingImpl(yaml::IO &YamlIO) {
28 MappingTraits<AArch64FunctionInfo>::mapping(YamlIO, *this);
29 }
30
initializeBaseYamlFields(const yaml::AArch64FunctionInfo & YamlMFI)31 void AArch64FunctionInfo::initializeBaseYamlFields(
32 const yaml::AArch64FunctionInfo &YamlMFI) {
33 if (YamlMFI.HasRedZone.hasValue())
34 HasRedZone = YamlMFI.HasRedZone;
35 }
36
GetSignReturnAddress(const Function & F)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()};
49 return {true, false};
50 }
51 }
52 return {false, false};
53 }
54
55 StringRef Scope = F.getFnAttribute("sign-return-address").getValueAsString();
56 if (Scope.equals("none"))
57 return {false, false};
58
59 if (Scope.equals("all"))
60 return {true, true};
61
62 assert(Scope.equals("non-leaf"));
63 return {true, false};
64 }
65
ShouldSignWithBKey(const Function & F)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();
71 return false;
72 }
73
74 const StringRef Key =
75 F.getFnAttribute("sign-return-address-key").getValueAsString();
76 assert(Key.equals_lower("a_key") || Key.equals_lower("b_key"));
77 return Key.equals_lower("b_key");
78 }
79
AArch64FunctionInfo(MachineFunction & MF)80 AArch64FunctionInfo::AArch64FunctionInfo(MachineFunction &MF) : MF(MF) {
81 // If we already know that the function doesn't have a redzone, set
82 // HasRedZone here.
83 if (MF.getFunction().hasFnAttribute(Attribute::NoRedZone))
84 HasRedZone = false;
85
86 const Function &F = MF.getFunction();
87 std::tie(SignReturnAddress, SignReturnAddressAll) = GetSignReturnAddress(F);
88 SignWithBKey = ShouldSignWithBKey(F);
89
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();
94 return;
95 }
96
97 const StringRef BTIEnable = F.getFnAttribute("branch-target-enforcement").getValueAsString();
98 assert(BTIEnable.equals_lower("true") || BTIEnable.equals_lower("false"));
99 BranchTargetEnforcement = BTIEnable.equals_lower("true");
100 }
101
shouldSignReturnAddress(bool SpillsLR) const102 bool AArch64FunctionInfo::shouldSignReturnAddress(bool SpillsLR) const {
103 if (!SignReturnAddress)
104 return false;
105 if (SignReturnAddressAll)
106 return true;
107 return SpillsLR;
108 }
109
shouldSignReturnAddress() const110 bool AArch64FunctionInfo::shouldSignReturnAddress() const {
111 return shouldSignReturnAddress(llvm::any_of(
112 MF.getFrameInfo().getCalleeSavedInfo(),
113 [](const auto &Info) { return Info.getReg() == AArch64::LR; }));
114 }
115