LLVM 24.0.0git
AArch64MachineFunctionInfo.cpp
Go to the documentation of this file.
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
17#include "AArch64InstrInfo.h"
18#include "AArch64Subtarget.h"
20#include "llvm/IR/Constants.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>
30 uint64_t (AArch64FunctionInfo::*GetStackSize)() const) {
32 return std::nullopt;
33 return (MFI.*GetStackSize)();
34}
35
38 : HasRedZone(MFI.hasRedZone()),
40 getSVEStackSize(MFI, &llvm::AArch64FunctionInfo::getStackSizeZPR)),
42 getSVEStackSize(MFI, &llvm::AArch64FunctionInfo::getStackSizePPR)),
43 HasStackFrame(MFI.hasStackFrame()
44 ? std::optional<bool>(MFI.hasStackFrame())
45 : std::nullopt),
47 MFI.hasStreamingModeChanges()
48 ? std::optional<bool>(MFI.hasStreamingModeChanges())
49 : std::nullopt) {}
50
54
56 const yaml::AArch64FunctionInfo &YamlMFI) {
57 if (YamlMFI.HasRedZone)
58 HasRedZone = YamlMFI.HasRedZone;
59 if (YamlMFI.StackSizeZPR || YamlMFI.StackSizePPR)
60 setStackSizeSVE(YamlMFI.StackSizeZPR.value_or(0),
61 YamlMFI.StackSizePPR.value_or(0));
62 if (YamlMFI.HasStackFrame)
64 if (YamlMFI.HasStreamingModeChanges)
66}
67
69 if (F.hasFnAttribute("ptrauth-returns"))
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("sign-return-address"))
77
78 StringRef Scope = F.getFnAttribute("sign-return-address").getValueAsString();
83}
84
85static bool ShouldSignWithBKey(const Function &F, const AArch64Subtarget &STI) {
86 if (F.hasFnAttribute("ptrauth-returns"))
87 return true;
88 if (!F.hasFnAttribute("sign-return-address-key")) {
89 if (STI.getTargetTriple().isOSWindows())
90 return true;
91 return false;
92 }
93
94 const StringRef Key =
95 F.getFnAttribute("sign-return-address-key").getValueAsString();
96 assert(Key == "a_key" || Key == "b_key");
97 if (STI.getTargetTriple().isOSWindows() && Key == "a_key" &&
99 F.getContext().diagnose(DiagnosticInfoUnsupported{
100 F, "A-key return address signing is unsupported on AArch64 Windows"});
101 return true;
102 }
103 return Key == "b_key";
104}
105
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 M->getModuleFlag("ptrauth-elf-got"));
113 if (Flag && Flag->getZExtValue() == 1)
114 return true;
115 return false;
116}
117
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(Attribute::NoRedZone))
123 HasRedZone = false;
124 SignCondition = GetSignReturnAddress(F);
125 SignWithBKey = ShouldSignWithBKey(F, *STI);
126 HasELFSignedGOT = hasELFSignedGOTHelper(F, STI);
127 // TODO: skip functions that have no instrumented allocas for optimization
128 IsMTETagged = F.hasFnAttribute(Attribute::SanitizeMemTag);
129
130 Attribute SignReturnAddrHardenFnAttr =
131 F.getFnAttribute("sign-return-address-harden");
132 if (SignReturnAddrHardenFnAttr.isValid()) {
133 SignReturnAddressHardening =
135 SignReturnAddrHardenFnAttr.getValueAsString())
136 .Case("none", SignReturnAddressHardeningKind::None)
137 .Case("load-return-address",
138 SignReturnAddressHardeningKind::LoadReturnAddress)
140 }
141
142 // BTI/PAuthLR are set on the function attribute.
143 BranchTargetEnforcement = F.hasFnAttribute("branch-target-enforcement");
144 BranchProtectionPAuthLR = F.hasFnAttribute("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("stack-probe-size"))
154 ProbeSize = F.getFnAttributeAsParsedInteger("stack-probe-size");
155 else if (const auto *PS = mdconst::extract_or_null<ConstantInt>(
156 F.getParent()->getModuleFlag("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("no-stack-arg-probe"))
162 StackProbeSize = ProbeSize;
163 } else {
164 // Round down to the stack alignment.
165 uint64_t StackAlign =
167 ProbeSize = std::max(StackAlign, ProbeSize & ~(StackAlign - 1U));
168 StringRef ProbeKind;
169 if (F.hasFnAttribute("probe-stack"))
170 ProbeKind = F.getFnAttribute("probe-stack").getValueAsString();
171 else if (const auto *PS = dyn_cast_or_null<MDString>(
172 F.getParent()->getModuleFlag("probe-stack")))
173 ProbeKind = PS->getString();
174 if (ProbeKind.size()) {
175 if (ProbeKind != "inline-asm")
176 report_fatal_error("Unsupported stack probing method");
177 StackProbeSize = ProbeSize;
178 }
179 }
180}
181
188
189static bool isLRSpilled(const MachineFunction &MF) {
190 return llvm::any_of(
191 MF.getFrameInfo().getCalleeSavedInfo(),
192 [](const auto &Info) { return Info.getReg() == AArch64::LR; });
193}
194
196 bool IsLRSpilled) {
197 switch (Condition) {
199 return false;
201 return IsLRSpilled;
203 return true;
204 }
205 llvm_unreachable("Unknown SignReturnAddress enum");
206}
207
209 const MachineFunction &MF) const {
210 return shouldSignReturnAddress(SignCondition, isLRSpilled(MF));
211}
212
214 MachineFunction &MF) const {
215 if (!(isLRSpilled(MF) &&
216 MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack)))
217 return false;
218
220 report_fatal_error("Must reserve x18 to use shadow call stack");
221
222 return true;
223}
224
226 const MachineFunction &MF) const {
227 if (!NeedsDwarfUnwindInfo)
228 NeedsDwarfUnwindInfo = !MF.usesWindowsUnwindInfo() && MF.needsFrameMoves();
229
230 return *NeedsDwarfUnwindInfo;
231}
232
234 const MachineFunction &MF) const {
235 if (!NeedsAsyncDwarfUnwindInfo) {
236 const Function &F = MF.getFunction();
238 // The check got "minsize" is because epilogue unwind info is not emitted
239 // (yet) for homogeneous epilogues, outlined functions, and functions
240 // outlined from.
241 NeedsAsyncDwarfUnwindInfo =
243 ((F.getUWTableKind() == UWTableKind::Async && !F.hasMinSize()) ||
245 }
246 return *NeedsAsyncDwarfUnwindInfo;
247}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static SignReturnAddress GetSignReturnAddress(const Function &F)
static std::optional< uint64_t > getSVEStackSize(const AArch64FunctionInfo &MFI, uint64_t(AArch64FunctionInfo::*GetStackSize)() const)
static bool ShouldSignWithBKey(const Function &F, const AArch64Subtarget &STI)
static bool hasELFSignedGOTHelper(const Function &F, const AArch64Subtarget *STI)
static bool isLRSpilled(const MachineFunction &MF)
unsigned uint64_t
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
This file contains the declarations for metadata subclasses.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...
bool needsShadowCallStackPrologueEpilogue(MachineFunction &MF) const
void setStackSizeSVE(uint64_t ZPR, uint64_t PPR)
AArch64FunctionInfo(const Function &F, const AArch64Subtarget *STI)
bool needsDwarfUnwindInfo(const MachineFunction &MF) const
static bool shouldSignReturnAddress(SignReturnAddress Condition, bool IsLRSpilled)
void initializeBaseYamlFields(const yaml::AArch64FunctionInfo &YamlMFI)
bool needsAsyncDwarfUnwindInfo(const MachineFunction &MF) const
MachineFunctionInfo * clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF, const DenseMap< MachineBasicBlock *, MachineBasicBlock * > &Src2DstMBB) const override
Make a functionally equivalent copy of this MachineFunctionInfo in MF.
void setHasStreamingModeChanges(bool HasChanges)
const Triple & getTargetTriple() const
bool isXRegisterReserved(size_t i) const
const AArch64FrameLowering * getFrameLowering() const override
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:106
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:266
Diagnostic information for unsupported feature in backend.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:734
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool needsFrameMoves() const
True if this function needs frame moves for debug or exceptions.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
bool usesWindowsUnwindInfo() const
True if this function describes its unwind information with Windows SEH unwind opcodes (....
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Ty * cloneInfo(const Ty &Old)
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
SMEAttrs is a utility class to parse the SME ACLE attributes on functions.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
R DefaultUnreachable(const char *Message="Fell off the end of a string-switch")
Declare default as unreachable, making sure that all cases were handled.
Align getTransientStackAlign() const
getTransientStackAlignment - This method returns the number of bytes to which the stack pointer must ...
bool isOSWindows() const
Tests whether the OS is Windows.
Definition Triple.h:778
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition Triple.h:867
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
Definition Metadata.h:694
This is an optimization pass for GlobalISel generic memory operations.
SignReturnAddress
Condition of signing the return address in a function.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
@ Async
"Asynchronous" unwind tables (instr precise)
Definition CodeGen.h:312
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:82
MachineFunctionInfo - This class can be derived from and used by targets to hold private target-speci...
void mappingImpl(yaml::IO &YamlIO) override
This class should be specialized by any type that needs to be converted to/from a YAML mapping.
Definition YAMLTraits.h:63