15ffd83dbSDimitry Andric //=- AArch64MachineFunctionInfo.cpp - AArch64 Machine Function Info ---------=//
25ffd83dbSDimitry Andric 
35ffd83dbSDimitry Andric //
45ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
55ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
65ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
75ffd83dbSDimitry Andric //
85ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
95ffd83dbSDimitry Andric ///
105ffd83dbSDimitry Andric /// \file
115ffd83dbSDimitry Andric /// This file implements AArch64-specific per-machine-function
125ffd83dbSDimitry Andric /// information.
135ffd83dbSDimitry Andric ///
145ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
155ffd83dbSDimitry Andric 
165ffd83dbSDimitry Andric #include "AArch64MachineFunctionInfo.h"
17e8d8bef9SDimitry Andric #include "AArch64InstrInfo.h"
1881ad6265SDimitry Andric #include "AArch64Subtarget.h"
1981ad6265SDimitry Andric #include "llvm/IR/Constants.h"
2081ad6265SDimitry Andric #include "llvm/IR/Metadata.h"
2181ad6265SDimitry Andric #include "llvm/IR/Module.h"
2281ad6265SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
235ffd83dbSDimitry Andric 
245ffd83dbSDimitry Andric using namespace llvm;
255ffd83dbSDimitry Andric 
AArch64FunctionInfo(const llvm::AArch64FunctionInfo & MFI)265ffd83dbSDimitry Andric yaml::AArch64FunctionInfo::AArch64FunctionInfo(
275ffd83dbSDimitry Andric     const llvm::AArch64FunctionInfo &MFI)
285ffd83dbSDimitry Andric     : HasRedZone(MFI.hasRedZone()) {}
295ffd83dbSDimitry Andric 
mappingImpl(yaml::IO & YamlIO)305ffd83dbSDimitry Andric void yaml::AArch64FunctionInfo::mappingImpl(yaml::IO &YamlIO) {
315ffd83dbSDimitry Andric   MappingTraits<AArch64FunctionInfo>::mapping(YamlIO, *this);
325ffd83dbSDimitry Andric }
335ffd83dbSDimitry Andric 
initializeBaseYamlFields(const yaml::AArch64FunctionInfo & YamlMFI)345ffd83dbSDimitry Andric void AArch64FunctionInfo::initializeBaseYamlFields(
355ffd83dbSDimitry Andric     const yaml::AArch64FunctionInfo &YamlMFI) {
3681ad6265SDimitry Andric   if (YamlMFI.HasRedZone)
375ffd83dbSDimitry Andric     HasRedZone = YamlMFI.HasRedZone;
385ffd83dbSDimitry Andric }
39e8d8bef9SDimitry Andric 
GetSignReturnAddress(const Function & F)40e8d8bef9SDimitry Andric static std::pair<bool, bool> GetSignReturnAddress(const Function &F) {
41e8d8bef9SDimitry Andric   // The function should be signed in the following situations:
42e8d8bef9SDimitry Andric   // - sign-return-address=all
43e8d8bef9SDimitry Andric   // - sign-return-address=non-leaf and the functions spills the LR
44e8d8bef9SDimitry Andric   if (!F.hasFnAttribute("sign-return-address")) {
45e8d8bef9SDimitry Andric     const Module &M = *F.getParent();
46e8d8bef9SDimitry Andric     if (const auto *Sign = mdconst::extract_or_null<ConstantInt>(
47e8d8bef9SDimitry Andric             M.getModuleFlag("sign-return-address"))) {
48e8d8bef9SDimitry Andric       if (Sign->getZExtValue()) {
49e8d8bef9SDimitry Andric         if (const auto *All = mdconst::extract_or_null<ConstantInt>(
50e8d8bef9SDimitry Andric                 M.getModuleFlag("sign-return-address-all")))
51e8d8bef9SDimitry Andric           return {true, All->getZExtValue()};
52e8d8bef9SDimitry Andric         return {true, false};
53e8d8bef9SDimitry Andric       }
54e8d8bef9SDimitry Andric     }
55e8d8bef9SDimitry Andric     return {false, false};
56e8d8bef9SDimitry Andric   }
57e8d8bef9SDimitry Andric 
58e8d8bef9SDimitry Andric   StringRef Scope = F.getFnAttribute("sign-return-address").getValueAsString();
59e8d8bef9SDimitry Andric   if (Scope.equals("none"))
60e8d8bef9SDimitry Andric     return {false, false};
61e8d8bef9SDimitry Andric 
62e8d8bef9SDimitry Andric   if (Scope.equals("all"))
63e8d8bef9SDimitry Andric     return {true, true};
64e8d8bef9SDimitry Andric 
65e8d8bef9SDimitry Andric   assert(Scope.equals("non-leaf"));
66e8d8bef9SDimitry Andric   return {true, false};
67e8d8bef9SDimitry Andric }
68e8d8bef9SDimitry Andric 
ShouldSignWithBKey(const Function & F,const AArch64Subtarget & STI)69bdd1243dSDimitry Andric static bool ShouldSignWithBKey(const Function &F, const AArch64Subtarget &STI) {
70e8d8bef9SDimitry Andric   if (!F.hasFnAttribute("sign-return-address-key")) {
71e8d8bef9SDimitry Andric     if (const auto *BKey = mdconst::extract_or_null<ConstantInt>(
72e8d8bef9SDimitry Andric             F.getParent()->getModuleFlag("sign-return-address-with-bkey")))
73e8d8bef9SDimitry Andric       return BKey->getZExtValue();
74bdd1243dSDimitry Andric     if (STI.getTargetTriple().isOSWindows())
75bdd1243dSDimitry Andric       return true;
76e8d8bef9SDimitry Andric     return false;
77e8d8bef9SDimitry Andric   }
78e8d8bef9SDimitry Andric 
79e8d8bef9SDimitry Andric   const StringRef Key =
80e8d8bef9SDimitry Andric       F.getFnAttribute("sign-return-address-key").getValueAsString();
815f757f3fSDimitry Andric   assert(Key == "a_key" || Key == "b_key");
825f757f3fSDimitry Andric   return Key == "b_key";
83e8d8bef9SDimitry Andric }
84e8d8bef9SDimitry Andric 
AArch64FunctionInfo(const Function & F,const AArch64Subtarget * STI)85bdd1243dSDimitry Andric AArch64FunctionInfo::AArch64FunctionInfo(const Function &F,
86bdd1243dSDimitry Andric                                          const AArch64Subtarget *STI) {
87e8d8bef9SDimitry Andric   // If we already know that the function doesn't have a redzone, set
88e8d8bef9SDimitry Andric   // HasRedZone here.
89bdd1243dSDimitry Andric   if (F.hasFnAttribute(Attribute::NoRedZone))
90e8d8bef9SDimitry Andric     HasRedZone = false;
91e8d8bef9SDimitry Andric   std::tie(SignReturnAddress, SignReturnAddressAll) = GetSignReturnAddress(F);
92bdd1243dSDimitry Andric   SignWithBKey = ShouldSignWithBKey(F, *STI);
9381ad6265SDimitry Andric   // TODO: skip functions that have no instrumented allocas for optimization
9481ad6265SDimitry Andric   IsMTETagged = F.hasFnAttribute(Attribute::SanitizeMemTag);
95e8d8bef9SDimitry Andric 
96*cb14a3feSDimitry Andric   // BTI/PAuthLR may be set either on the function or the module. Set Bool from
97*cb14a3feSDimitry Andric   // either the function attribute or module attribute, depending on what is
98*cb14a3feSDimitry Andric   // set.
99*cb14a3feSDimitry Andric   // Note: the module attributed is numeric (0 or 1) but the function attribute
100*cb14a3feSDimitry Andric   // is stringy ("true" or "false").
101*cb14a3feSDimitry Andric   auto TryFnThenModule = [&](StringRef AttrName, bool &Bool) {
102*cb14a3feSDimitry Andric     if (F.hasFnAttribute(AttrName)) {
103*cb14a3feSDimitry Andric       const StringRef V = F.getFnAttribute(AttrName).getValueAsString();
104*cb14a3feSDimitry Andric       assert(V.equals_insensitive("true") || V.equals_insensitive("false"));
105*cb14a3feSDimitry Andric       Bool = V.equals_insensitive("true");
106*cb14a3feSDimitry Andric     } else if (const auto *ModVal = mdconst::extract_or_null<ConstantInt>(
107*cb14a3feSDimitry Andric                    F.getParent()->getModuleFlag(AttrName))) {
108*cb14a3feSDimitry Andric       Bool = ModVal->getZExtValue();
1095f757f3fSDimitry Andric     }
110*cb14a3feSDimitry Andric   };
111*cb14a3feSDimitry Andric 
112*cb14a3feSDimitry Andric   TryFnThenModule("branch-target-enforcement", BranchTargetEnforcement);
113*cb14a3feSDimitry Andric   TryFnThenModule("branch-protection-pauth-lr", BranchProtectionPAuthLR);
1145f757f3fSDimitry Andric 
1155f757f3fSDimitry Andric   // The default stack probe size is 4096 if the function has no
1165f757f3fSDimitry Andric   // stack-probe-size attribute. This is a safe default because it is the
1175f757f3fSDimitry Andric   // smallest possible guard page size.
1185f757f3fSDimitry Andric   uint64_t ProbeSize = 4096;
1195f757f3fSDimitry Andric   if (F.hasFnAttribute("stack-probe-size"))
1205f757f3fSDimitry Andric     ProbeSize = F.getFnAttributeAsParsedInteger("stack-probe-size");
1215f757f3fSDimitry Andric   else if (const auto *PS = mdconst::extract_or_null<ConstantInt>(
1225f757f3fSDimitry Andric                F.getParent()->getModuleFlag("stack-probe-size")))
1235f757f3fSDimitry Andric     ProbeSize = PS->getZExtValue();
1245f757f3fSDimitry Andric   assert(int64_t(ProbeSize) > 0 && "Invalid stack probe size");
1255f757f3fSDimitry Andric 
1265f757f3fSDimitry Andric   if (STI->isTargetWindows()) {
1275f757f3fSDimitry Andric     if (!F.hasFnAttribute("no-stack-arg-probe"))
1285f757f3fSDimitry Andric       StackProbeSize = ProbeSize;
1295f757f3fSDimitry Andric   } else {
1305f757f3fSDimitry Andric     // Round down to the stack alignment.
1315f757f3fSDimitry Andric     uint64_t StackAlign =
1325f757f3fSDimitry Andric         STI->getFrameLowering()->getTransientStackAlign().value();
1335f757f3fSDimitry Andric     ProbeSize = std::max(StackAlign, ProbeSize & ~(StackAlign - 1U));
1345f757f3fSDimitry Andric     StringRef ProbeKind;
1355f757f3fSDimitry Andric     if (F.hasFnAttribute("probe-stack"))
1365f757f3fSDimitry Andric       ProbeKind = F.getFnAttribute("probe-stack").getValueAsString();
1375f757f3fSDimitry Andric     else if (const auto *PS = dyn_cast_or_null<MDString>(
1385f757f3fSDimitry Andric                  F.getParent()->getModuleFlag("probe-stack")))
1395f757f3fSDimitry Andric       ProbeKind = PS->getString();
1405f757f3fSDimitry Andric     if (ProbeKind.size()) {
1415f757f3fSDimitry Andric       if (ProbeKind != "inline-asm")
1425f757f3fSDimitry Andric         report_fatal_error("Unsupported stack probing method");
1435f757f3fSDimitry Andric       StackProbeSize = ProbeSize;
1445f757f3fSDimitry Andric     }
1455f757f3fSDimitry Andric   }
146e8d8bef9SDimitry Andric }
147e8d8bef9SDimitry Andric 
clone(BumpPtrAllocator & Allocator,MachineFunction & DestMF,const DenseMap<MachineBasicBlock *,MachineBasicBlock * > & Src2DstMBB) const14881ad6265SDimitry Andric MachineFunctionInfo *AArch64FunctionInfo::clone(
14981ad6265SDimitry Andric     BumpPtrAllocator &Allocator, MachineFunction &DestMF,
15081ad6265SDimitry Andric     const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
15181ad6265SDimitry Andric     const {
152bdd1243dSDimitry Andric   return DestMF.cloneInfo<AArch64FunctionInfo>(*this);
15381ad6265SDimitry Andric }
15481ad6265SDimitry Andric 
shouldSignReturnAddress(bool SpillsLR) const155e8d8bef9SDimitry Andric bool AArch64FunctionInfo::shouldSignReturnAddress(bool SpillsLR) const {
156e8d8bef9SDimitry Andric   if (!SignReturnAddress)
157e8d8bef9SDimitry Andric     return false;
158e8d8bef9SDimitry Andric   if (SignReturnAddressAll)
159e8d8bef9SDimitry Andric     return true;
160e8d8bef9SDimitry Andric   return SpillsLR;
161e8d8bef9SDimitry Andric }
162e8d8bef9SDimitry Andric 
isLRSpilled(const MachineFunction & MF)1635f757f3fSDimitry Andric static bool isLRSpilled(const MachineFunction &MF) {
1645f757f3fSDimitry Andric   return llvm::any_of(
1655f757f3fSDimitry Andric       MF.getFrameInfo().getCalleeSavedInfo(),
1665f757f3fSDimitry Andric       [](const auto &Info) { return Info.getReg() == AArch64::LR; });
1675f757f3fSDimitry Andric }
1685f757f3fSDimitry Andric 
shouldSignReturnAddress(const MachineFunction & MF) const169bdd1243dSDimitry Andric bool AArch64FunctionInfo::shouldSignReturnAddress(
170bdd1243dSDimitry Andric     const MachineFunction &MF) const {
1715f757f3fSDimitry Andric   return shouldSignReturnAddress(isLRSpilled(MF));
1725f757f3fSDimitry Andric }
1735f757f3fSDimitry Andric 
needsShadowCallStackPrologueEpilogue(MachineFunction & MF) const1745f757f3fSDimitry Andric bool AArch64FunctionInfo::needsShadowCallStackPrologueEpilogue(
1755f757f3fSDimitry Andric     MachineFunction &MF) const {
1765f757f3fSDimitry Andric   if (!(isLRSpilled(MF) &&
1775f757f3fSDimitry Andric         MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack)))
1785f757f3fSDimitry Andric     return false;
1795f757f3fSDimitry Andric 
1805f757f3fSDimitry Andric   if (!MF.getSubtarget<AArch64Subtarget>().isXRegisterReserved(18))
1815f757f3fSDimitry Andric     report_fatal_error("Must reserve x18 to use shadow call stack");
1825f757f3fSDimitry Andric 
1835f757f3fSDimitry Andric   return true;
184e8d8bef9SDimitry Andric }
18581ad6265SDimitry Andric 
needsDwarfUnwindInfo(const MachineFunction & MF) const186bdd1243dSDimitry Andric bool AArch64FunctionInfo::needsDwarfUnwindInfo(
187bdd1243dSDimitry Andric     const MachineFunction &MF) const {
18881ad6265SDimitry Andric   if (!NeedsDwarfUnwindInfo)
189bdd1243dSDimitry Andric     NeedsDwarfUnwindInfo = MF.needsFrameMoves() &&
190bdd1243dSDimitry Andric                            !MF.getTarget().getMCAsmInfo()->usesWindowsCFI();
19181ad6265SDimitry Andric 
19281ad6265SDimitry Andric   return *NeedsDwarfUnwindInfo;
19381ad6265SDimitry Andric }
19481ad6265SDimitry Andric 
needsAsyncDwarfUnwindInfo(const MachineFunction & MF) const195bdd1243dSDimitry Andric bool AArch64FunctionInfo::needsAsyncDwarfUnwindInfo(
196bdd1243dSDimitry Andric     const MachineFunction &MF) const {
19781ad6265SDimitry Andric   if (!NeedsAsyncDwarfUnwindInfo) {
198bdd1243dSDimitry Andric     const Function &F = MF.getFunction();
19981ad6265SDimitry Andric     //  The check got "minsize" is because epilogue unwind info is not emitted
20081ad6265SDimitry Andric     //  (yet) for homogeneous epilogues, outlined functions, and functions
20181ad6265SDimitry Andric     //  outlined from.
202bdd1243dSDimitry Andric     NeedsAsyncDwarfUnwindInfo = needsDwarfUnwindInfo(MF) &&
20381ad6265SDimitry Andric                                 F.getUWTableKind() == UWTableKind::Async &&
20481ad6265SDimitry Andric                                 !F.hasMinSize();
20581ad6265SDimitry Andric   }
20681ad6265SDimitry Andric   return *NeedsAsyncDwarfUnwindInfo;
20781ad6265SDimitry Andric }
208