LLVM 18.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 // The function should be signed in the following situations:
42 // - sign-return-address=all
43 // - sign-return-address=non-leaf and the functions spills the LR
44 if (!F.hasFnAttribute("sign-return-address")) {
45 const Module &M = *F.getParent();
46 if (const auto *Sign = mdconst::extract_or_null<ConstantInt>(
47 M.getModuleFlag("sign-return-address"))) {
48 if (Sign->getZExtValue()) {
49 if (const auto *All = mdconst::extract_or_null<ConstantInt>(
50 M.getModuleFlag("sign-return-address-all")))
51 return {true, All->getZExtValue()};
52 return {true, false};
53 }
54 }
55 return {false, false};
56 }
57
58 StringRef Scope = F.getFnAttribute("sign-return-address").getValueAsString();
59 if (Scope.equals("none"))
60 return {false, false};
61
62 if (Scope.equals("all"))
63 return {true, true};
64
65 assert(Scope.equals("non-leaf"));
66 return {true, false};
67}
68
69static bool ShouldSignWithBKey(const Function &F, const AArch64Subtarget &STI) {
70 if (!F.hasFnAttribute("sign-return-address-key")) {
71 if (const auto *BKey = mdconst::extract_or_null<ConstantInt>(
72 F.getParent()->getModuleFlag("sign-return-address-with-bkey")))
73 return BKey->getZExtValue();
74 if (STI.getTargetTriple().isOSWindows())
75 return true;
76 return false;
77 }
78
79 const StringRef Key =
80 F.getFnAttribute("sign-return-address-key").getValueAsString();
81 assert(Key.equals_insensitive("a_key") || Key.equals_insensitive("b_key"));
82 return Key.equals_insensitive("b_key");
83}
84
86 const AArch64Subtarget *STI) {
87 // If we already know that the function doesn't have a redzone, set
88 // HasRedZone here.
89 if (F.hasFnAttribute(Attribute::NoRedZone))
90 HasRedZone = false;
91 std::tie(SignReturnAddress, SignReturnAddressAll) = GetSignReturnAddress(F);
92 SignWithBKey = ShouldSignWithBKey(F, *STI);
93 // TODO: skip functions that have no instrumented allocas for optimization
94 IsMTETagged = F.hasFnAttribute(Attribute::SanitizeMemTag);
95
96 if (!F.hasFnAttribute("branch-target-enforcement")) {
97 if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
98 F.getParent()->getModuleFlag("branch-target-enforcement")))
99 BranchTargetEnforcement = BTE->getZExtValue();
100 return;
101 }
102
103 const StringRef BTIEnable =
104 F.getFnAttribute("branch-target-enforcement").getValueAsString();
105 assert(BTIEnable.equals_insensitive("true") ||
106 BTIEnable.equals_insensitive("false"));
107 BranchTargetEnforcement = BTIEnable.equals_insensitive("true");
108}
109
111 BumpPtrAllocator &Allocator, MachineFunction &DestMF,
113 const {
114 return DestMF.cloneInfo<AArch64FunctionInfo>(*this);
115}
116
118 if (!SignReturnAddress)
119 return false;
120 if (SignReturnAddressAll)
121 return true;
122 return SpillsLR;
123}
124
125static bool isLRSpilled(const MachineFunction &MF) {
126 return llvm::any_of(
127 MF.getFrameInfo().getCalleeSavedInfo(),
128 [](const auto &Info) { return Info.getReg() == AArch64::LR; });
129}
130
132 const MachineFunction &MF) const {
133 return shouldSignReturnAddress(isLRSpilled(MF));
134}
135
137 MachineFunction &MF) const {
138 if (!(isLRSpilled(MF) &&
139 MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack)))
140 return false;
141
143 report_fatal_error("Must reserve x18 to use shadow call stack");
144
145 return true;
146}
147
149 const MachineFunction &MF) const {
150 if (!NeedsDwarfUnwindInfo)
151 NeedsDwarfUnwindInfo = MF.needsFrameMoves() &&
153
154 return *NeedsDwarfUnwindInfo;
155}
156
158 const MachineFunction &MF) const {
159 if (!NeedsAsyncDwarfUnwindInfo) {
160 const Function &F = MF.getFunction();
161 // The check got "minsize" is because epilogue unwind info is not emitted
162 // (yet) for homogeneous epilogues, outlined functions, and functions
163 // outlined from.
164 NeedsAsyncDwarfUnwindInfo = needsDwarfUnwindInfo(MF) &&
165 F.getUWTableKind() == UWTableKind::Async &&
166 !F.hasMinSize();
167 }
168 return *NeedsAsyncDwarfUnwindInfo;
169}
static std::pair< bool, bool > GetSignReturnAddress(const Function &F)
static bool ShouldSignWithBKey(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:1274
#define F(x, y, z)
Definition: MD5.cpp:55
This file contains the declarations for metadata subclasses.
Module.h This file contains the declarations for the Module class.
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
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:666
bool usesWindowsCFI() const
Definition: MCAsmInfo.h:805
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.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Ty * cloneInfo(const Ty &Old)
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:50
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
Definition: StringRef.h:170
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
bool isOSWindows() const
Tests whether the OS is Windows.
Definition: Triple.h:583
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:1733
@ 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:156
MachineFunctionInfo - This class can be derived from and used by targets to hold private target-speci...
void mappingImpl(yaml::IO &YamlIO) override