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 "AArch64Subtarget.h"
19#include "llvm/ADT/StringSwitch.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/DiagnosticInfo.h"
22#include "llvm/IR/Metadata.h"
23#include "llvm/IR/Module.h"
24#include "llvm/MC/MCAsmInfo.h"
25
26using namespace llvm;
27
28static std::optional<uint64_t>
29getSVEStackSize(const AArch64FunctionInfo &MFI,
30 uint64_t (AArch64FunctionInfo::*GetStackSize)() const) {
31 if (!MFI.hasCalculatedStackSizeSVE())
32 return std::nullopt;
33 return (MFI.*GetStackSize)();
34}
35
36yaml::AArch64FunctionInfo::AArch64FunctionInfo(
37 const llvm::AArch64FunctionInfo &MFI)
38 : HasRedZone(MFI.hasRedZone()),
39 StackSizeZPR(
40 getSVEStackSize(MFI, GetStackSize: &llvm::AArch64FunctionInfo::getStackSizeZPR)),
41 StackSizePPR(
42 getSVEStackSize(MFI, GetStackSize: &llvm::AArch64FunctionInfo::getStackSizePPR)),
43 HasStackFrame(MFI.hasStackFrame()
44 ? std::optional<bool>(MFI.hasStackFrame())
45 : std::nullopt),
46 HasStreamingModeChanges(
47 MFI.hasStreamingModeChanges()
48 ? std::optional<bool>(MFI.hasStreamingModeChanges())
49 : std::nullopt) {}
50
51void yaml::AArch64FunctionInfo::mappingImpl(yaml::IO &YamlIO) {
52 MappingTraits<AArch64FunctionInfo>::mapping(YamlIO, MFI&: *this);
53}
54
55void AArch64FunctionInfo::initializeBaseYamlFields(
56 const yaml::AArch64FunctionInfo &YamlMFI) {
57 if (YamlMFI.HasRedZone)
58 HasRedZone = YamlMFI.HasRedZone;
59 if (YamlMFI.StackSizeZPR || YamlMFI.StackSizePPR)
60 setStackSizeSVE(ZPR: YamlMFI.StackSizeZPR.value_or(u: 0),
61 PPR: YamlMFI.StackSizePPR.value_or(u: 0));
62 if (YamlMFI.HasStackFrame)
63 setHasStackFrame(*YamlMFI.HasStackFrame);
64 if (YamlMFI.HasStreamingModeChanges)
65 setHasStreamingModeChanges(*YamlMFI.HasStreamingModeChanges);
66}
67
68static SignReturnAddress GetSignReturnAddress(const Function &F) {
69 if (F.hasFnAttribute(Kind: "ptrauth-returns"))
70 return SignReturnAddress::NonLeaf;
71
72 // The function should be signed in the following situations:
73 // - sign-return-address=all
74 // - sign-return-address=non-leaf and the functions spills the LR
75 if (!F.hasFnAttribute(Kind: "sign-return-address"))
76 return SignReturnAddress::None;
77
78 StringRef Scope = F.getFnAttribute(Kind: "sign-return-address").getValueAsString();
79 return StringSwitch<SignReturnAddress>(Scope)
80 .Case(S: "none", Value: SignReturnAddress::None)
81 .Case(S: "non-leaf", Value: SignReturnAddress::NonLeaf)
82 .Case(S: "all", Value: SignReturnAddress::All);
83}
84
85static bool ShouldSignWithBKey(const Function &F, const AArch64Subtarget &STI) {
86 if (F.hasFnAttribute(Kind: "ptrauth-returns"))
87 return true;
88 if (!F.hasFnAttribute(Kind: "sign-return-address-key")) {
89 if (STI.getTargetTriple().isOSWindows())
90 return true;
91 return false;
92 }
93
94 const StringRef Key =
95 F.getFnAttribute(Kind: "sign-return-address-key").getValueAsString();
96 assert(Key == "a_key" || Key == "b_key");
97 if (STI.getTargetTriple().isOSWindows() && Key == "a_key" &&
98 GetSignReturnAddress(F) != SignReturnAddress::None) {
99 F.getContext().diagnose(DI: DiagnosticInfoUnsupported{
100 F, "A-key return address signing is unsupported on AArch64 Windows"});
101 return true;
102 }
103 return Key == "b_key";
104}
105
106static bool hasELFSignedGOTHelper(const Function &F,
107 const AArch64Subtarget *STI) {
108 if (!STI->getTargetTriple().isOSBinFormatELF())
109 return false;
110 const Module *M = F.getParent();
111 const auto *Flag = mdconst::extract_or_null<ConstantInt>(
112 MD: M->getModuleFlag(Key: "ptrauth-elf-got"));
113 if (Flag && Flag->getZExtValue() == 1)
114 return true;
115 return false;
116}
117
118AArch64FunctionInfo::AArch64FunctionInfo(const Function &F,
119 const AArch64Subtarget *STI) {
120 // If we already know that the function doesn't have a redzone, set
121 // HasRedZone here.
122 if (F.hasFnAttribute(Kind: Attribute::NoRedZone))
123 HasRedZone = false;
124 SignCondition = GetSignReturnAddress(F);
125 SignWithBKey = ShouldSignWithBKey(F, STI: *STI);
126 HasELFSignedGOT = hasELFSignedGOTHelper(F, STI);
127 // TODO: skip functions that have no instrumented allocas for optimization
128 IsMTETagged = F.hasFnAttribute(Kind: Attribute::SanitizeMemTag);
129
130 Attribute SignReturnAddrHardenFnAttr =
131 F.getFnAttribute(Kind: "sign-return-address-harden");
132 if (SignReturnAddrHardenFnAttr.isValid()) {
133 SignReturnAddressHardening =
134 StringSwitch<SignReturnAddressHardeningKind>(
135 SignReturnAddrHardenFnAttr.getValueAsString())
136 .Case(S: "none", Value: SignReturnAddressHardeningKind::None)
137 .Case(S: "load-return-address",
138 Value: SignReturnAddressHardeningKind::LoadReturnAddress)
139 .DefaultUnreachable();
140 }
141
142 // BTI/PAuthLR are set on the function attribute.
143 BranchTargetEnforcement = F.hasFnAttribute(Kind: "branch-target-enforcement");
144 BranchProtectionPAuthLR = F.hasFnAttribute(Kind: "branch-protection-pauth-lr");
145
146 // Parse the SME function attributes.
147 SMEFnAttrs = SMEAttrs(F);
148
149 // The default stack probe size is 4096 if the function has no
150 // stack-probe-size attribute. This is a safe default because it is the
151 // smallest possible guard page size.
152 uint64_t ProbeSize = 4096;
153 if (F.hasFnAttribute(Kind: "stack-probe-size"))
154 ProbeSize = F.getFnAttributeAsParsedInteger(Kind: "stack-probe-size");
155 else if (const auto *PS = mdconst::extract_or_null<ConstantInt>(
156 MD: F.getParent()->getModuleFlag(Key: "stack-probe-size")))
157 ProbeSize = PS->getZExtValue();
158 assert(int64_t(ProbeSize) > 0 && "Invalid stack probe size");
159
160 if (STI->isTargetWindows()) {
161 if (!F.hasFnAttribute(Kind: "no-stack-arg-probe"))
162 StackProbeSize = ProbeSize;
163 } else {
164 // Round down to the stack alignment.
165 uint64_t StackAlign =
166 STI->getFrameLowering()->getTransientStackAlign().value();
167 ProbeSize = std::max(a: StackAlign, b: ProbeSize & ~(StackAlign - 1U));
168 StringRef ProbeKind;
169 if (F.hasFnAttribute(Kind: "probe-stack"))
170 ProbeKind = F.getFnAttribute(Kind: "probe-stack").getValueAsString();
171 else if (const auto *PS = dyn_cast_or_null<MDString>(
172 Val: F.getParent()->getModuleFlag(Key: "probe-stack")))
173 ProbeKind = PS->getString();
174 if (ProbeKind.size()) {
175 if (ProbeKind != "inline-asm")
176 report_fatal_error(reason: "Unsupported stack probing method");
177 StackProbeSize = ProbeSize;
178 }
179 }
180}
181
182MachineFunctionInfo *AArch64FunctionInfo::clone(
183 BumpPtrAllocator &Allocator, MachineFunction &DestMF,
184 const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
185 const {
186 return DestMF.cloneInfo<AArch64FunctionInfo>(Old: *this);
187}
188
189static bool isLRSpilled(const MachineFunction &MF) {
190 return llvm::any_of(
191 Range: MF.getFrameInfo().getCalleeSavedInfo(),
192 P: [](const auto &Info) { return Info.getReg() == AArch64::LR; });
193}
194
195bool AArch64FunctionInfo::shouldSignReturnAddress(SignReturnAddress Condition,
196 bool IsLRSpilled) {
197 switch (Condition) {
198 case SignReturnAddress::None:
199 return false;
200 case SignReturnAddress::NonLeaf:
201 return IsLRSpilled;
202 case SignReturnAddress::All:
203 return true;
204 }
205 llvm_unreachable("Unknown SignReturnAddress enum");
206}
207
208bool AArch64FunctionInfo::shouldSignReturnAddress(
209 const MachineFunction &MF) const {
210 return shouldSignReturnAddress(Condition: SignCondition, IsLRSpilled: isLRSpilled(MF));
211}
212
213bool AArch64FunctionInfo::needsShadowCallStackPrologueEpilogue(
214 MachineFunction &MF) const {
215 if (!(isLRSpilled(MF) &&
216 MF.getFunction().hasFnAttribute(Kind: Attribute::ShadowCallStack)))
217 return false;
218
219 if (!MF.getSubtarget<AArch64Subtarget>().isXRegisterReserved(i: 18))
220 report_fatal_error(reason: "Must reserve x18 to use shadow call stack");
221
222 return true;
223}
224
225bool AArch64FunctionInfo::needsDwarfUnwindInfo(
226 const MachineFunction &MF) const {
227 if (!NeedsDwarfUnwindInfo)
228 NeedsDwarfUnwindInfo =
229 MF.needsFrameMoves() && !MF.getTarget().getMCAsmInfo().usesWindowsCFI();
230
231 return *NeedsDwarfUnwindInfo;
232}
233
234bool AArch64FunctionInfo::needsAsyncDwarfUnwindInfo(
235 const MachineFunction &MF) const {
236 if (!NeedsAsyncDwarfUnwindInfo) {
237 const Function &F = MF.getFunction();
238 const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
239 // The check got "minsize" is because epilogue unwind info is not emitted
240 // (yet) for homogeneous epilogues, outlined functions, and functions
241 // outlined from.
242 NeedsAsyncDwarfUnwindInfo =
243 needsDwarfUnwindInfo(MF) &&
244 ((F.getUWTableKind() == UWTableKind::Async && !F.hasMinSize()) ||
245 AFI->hasStreamingModeChanges());
246 }
247 return *NeedsAsyncDwarfUnwindInfo;
248}
249