LLVM 20.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"
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
28 : HasRedZone(MFI.hasRedZone()) {}
29
32}
33
35 const yaml::AArch64FunctionInfo &YamlMFI) {
36 if (YamlMFI.HasRedZone)
37 HasRedZone = YamlMFI.HasRedZone;
38}
39
40static std::pair<bool, bool> GetSignReturnAddress(const Function &F) {
41 if (F.hasFnAttribute("ptrauth-returns"))
42 return {true, false}; // non-leaf
43 // The function should be signed in the following situations:
44 // - sign-return-address=all
45 // - sign-return-address=non-leaf and the functions spills the LR
46 if (!F.hasFnAttribute("sign-return-address"))
47 return {false, false};
48
49 StringRef Scope = F.getFnAttribute("sign-return-address").getValueAsString();
50 if (Scope == "none")
51 return {false, false};
52
53 if (Scope == "all")
54 return {true, true};
55
56 assert(Scope == "non-leaf");
57 return {true, false};
58}
59
60static bool ShouldSignWithBKey(const Function &F, const AArch64Subtarget &STI) {
61 if (F.hasFnAttribute("ptrauth-returns"))
62 return true;
63 if (!F.hasFnAttribute("sign-return-address-key")) {
64 if (STI.getTargetTriple().isOSWindows())
65 return true;
66 return false;
67 }
68
69 const StringRef Key =
70 F.getFnAttribute("sign-return-address-key").getValueAsString();
71 assert(Key == "a_key" || Key == "b_key");
72 return Key == "b_key";
73}
74
75static bool hasELFSignedGOTHelper(const Function &F,
76 const AArch64Subtarget *STI) {
78 return false;
79 const Module *M = F.getParent();
80 const auto *Flag = mdconst::extract_or_null<ConstantInt>(
81 M->getModuleFlag("ptrauth-elf-got"));
82 if (Flag && Flag->getZExtValue() == 1)
83 return true;
84 return false;
85}
86
88 const AArch64Subtarget *STI) {
89 // If we already know that the function doesn't have a redzone, set
90 // HasRedZone here.
91 if (F.hasFnAttribute(Attribute::NoRedZone))
92 HasRedZone = false;
93 std::tie(SignReturnAddress, SignReturnAddressAll) = GetSignReturnAddress(F);
94 SignWithBKey = ShouldSignWithBKey(F, *STI);
95 HasELFSignedGOT = hasELFSignedGOTHelper(F, STI);
96 // TODO: skip functions that have no instrumented allocas for optimization
97 IsMTETagged = F.hasFnAttribute(Attribute::SanitizeMemTag);
98
99 // BTI/PAuthLR are set on the function attribute.
100 BranchTargetEnforcement = F.hasFnAttribute("branch-target-enforcement");
101 BranchProtectionPAuthLR = F.hasFnAttribute("branch-protection-pauth-lr");
102
103 // The default stack probe size is 4096 if the function has no
104 // stack-probe-size attribute. This is a safe default because it is the
105 // smallest possible guard page size.
106 uint64_t ProbeSize = 4096;
107 if (F.hasFnAttribute("stack-probe-size"))
108 ProbeSize = F.getFnAttributeAsParsedInteger("stack-probe-size");
109 else if (const auto *PS = mdconst::extract_or_null<ConstantInt>(
110 F.getParent()->getModuleFlag("stack-probe-size")))
111 ProbeSize = PS->getZExtValue();
112 assert(int64_t(ProbeSize) > 0 && "Invalid stack probe size");
113
114 if (STI->isTargetWindows()) {
115 if (!F.hasFnAttribute("no-stack-arg-probe"))
116 StackProbeSize = ProbeSize;
117 } else {
118 // Round down to the stack alignment.
119 uint64_t StackAlign =
121 ProbeSize = std::max(StackAlign, ProbeSize & ~(StackAlign - 1U));
122 StringRef ProbeKind;
123 if (F.hasFnAttribute("probe-stack"))
124 ProbeKind = F.getFnAttribute("probe-stack").getValueAsString();
125 else if (const auto *PS = dyn_cast_or_null<MDString>(
126 F.getParent()->getModuleFlag("probe-stack")))
127 ProbeKind = PS->getString();
128 if (ProbeKind.size()) {
129 if (ProbeKind != "inline-asm")
130 report_fatal_error("Unsupported stack probing method");
131 StackProbeSize = ProbeSize;
132 }
133 }
134}
135
137 BumpPtrAllocator &Allocator, MachineFunction &DestMF,
139 const {
140 return DestMF.cloneInfo<AArch64FunctionInfo>(*this);
141}
142
144 if (!SignReturnAddress)
145 return false;
146 if (SignReturnAddressAll)
147 return true;
148 return SpillsLR;
149}
150
151static bool isLRSpilled(const MachineFunction &MF) {
152 return llvm::any_of(
153 MF.getFrameInfo().getCalleeSavedInfo(),
154 [](const auto &Info) { return Info.getReg() == AArch64::LR; });
155}
156
158 const MachineFunction &MF) const {
159 return shouldSignReturnAddress(isLRSpilled(MF));
160}
161
163 MachineFunction &MF) const {
164 if (!(isLRSpilled(MF) &&
165 MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack)))
166 return false;
167
169 report_fatal_error("Must reserve x18 to use shadow call stack");
170
171 return true;
172}
173
175 const MachineFunction &MF) const {
176 if (!NeedsDwarfUnwindInfo)
177 NeedsDwarfUnwindInfo = MF.needsFrameMoves() &&
179
180 return *NeedsDwarfUnwindInfo;
181}
182
184 const MachineFunction &MF) const {
185 if (!NeedsAsyncDwarfUnwindInfo) {
186 const Function &F = MF.getFunction();
188 // The check got "minsize" is because epilogue unwind info is not emitted
189 // (yet) for homogeneous epilogues, outlined functions, and functions
190 // outlined from.
191 NeedsAsyncDwarfUnwindInfo =
192 needsDwarfUnwindInfo(MF) &&
193 ((F.getUWTableKind() == UWTableKind::Async && !F.hasMinSize()) ||
195 }
196 return *NeedsAsyncDwarfUnwindInfo;
197}
static std::pair< bool, bool > GetSignReturnAddress(const Function &F)
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...
IO & YamlIO
Definition: ELFYAML.cpp:1314
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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...
bool needsShadowCallStackPrologueEpilogue(MachineFunction &MF) const
bool shouldSignReturnAddress(const MachineFunction &MF) const
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
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:731
bool usesWindowsCFI() const
Definition: MCAsmInfo.h:759
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:65
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
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.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isOSWindows() const
Tests whether the OS is Windows.
Definition: Triple.h:635
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:730
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1746
@ Async
"Asynchronous" unwind tables (instr precise)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
MachineFunctionInfo - This class can be derived from and used by targets to hold private target-speci...
void mappingImpl(yaml::IO &YamlIO) override