166037b84SJessica Paquette //=- AArch64MachineFunctionInfo.cpp - AArch64 Machine Function Info ---------=//
266037b84SJessica Paquette
366037b84SJessica Paquette //
466037b84SJessica Paquette // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
566037b84SJessica Paquette // See https://llvm.org/LICENSE.txt for license information.
666037b84SJessica Paquette // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
766037b84SJessica Paquette //
866037b84SJessica Paquette //===----------------------------------------------------------------------===//
966037b84SJessica Paquette ///
1066037b84SJessica Paquette /// \file
1166037b84SJessica Paquette /// This file implements AArch64-specific per-machine-function
1266037b84SJessica Paquette /// information.
1366037b84SJessica Paquette ///
1466037b84SJessica Paquette //===----------------------------------------------------------------------===//
1566037b84SJessica Paquette
1666037b84SJessica Paquette #include "AArch64MachineFunctionInfo.h"
17a88c722eSMomchil Velikov #include "AArch64InstrInfo.h"
18d0ea42a7SMomchil Velikov #include "AArch64Subtarget.h"
19a5bbc6efSBill Wendling #include "llvm/IR/Constants.h"
20a5bbc6efSBill Wendling #include "llvm/IR/Metadata.h"
21a5bbc6efSBill Wendling #include "llvm/IR/Module.h"
22d0ea42a7SMomchil Velikov #include "llvm/MC/MCAsmInfo.h"
2366037b84SJessica Paquette
2466037b84SJessica Paquette using namespace llvm;
2566037b84SJessica Paquette
AArch64FunctionInfo(const llvm::AArch64FunctionInfo & MFI)2666037b84SJessica Paquette yaml::AArch64FunctionInfo::AArch64FunctionInfo(
2766037b84SJessica Paquette const llvm::AArch64FunctionInfo &MFI)
2866037b84SJessica Paquette : HasRedZone(MFI.hasRedZone()) {}
2966037b84SJessica Paquette
mappingImpl(yaml::IO & YamlIO)3066037b84SJessica Paquette void yaml::AArch64FunctionInfo::mappingImpl(yaml::IO &YamlIO) {
3166037b84SJessica Paquette MappingTraits<AArch64FunctionInfo>::mapping(YamlIO, *this);
3266037b84SJessica Paquette }
3366037b84SJessica Paquette
initializeBaseYamlFields(const yaml::AArch64FunctionInfo & YamlMFI)3466037b84SJessica Paquette void AArch64FunctionInfo::initializeBaseYamlFields(
3566037b84SJessica Paquette const yaml::AArch64FunctionInfo &YamlMFI) {
36*e0e687a6SKazu Hirata if (YamlMFI.HasRedZone)
3766037b84SJessica Paquette HasRedZone = YamlMFI.HasRedZone;
3866037b84SJessica Paquette }
39a88c722eSMomchil Velikov
GetSignReturnAddress(const Function & F)40a88c722eSMomchil Velikov static std::pair<bool, bool> GetSignReturnAddress(const Function &F) {
41a88c722eSMomchil Velikov // The function should be signed in the following situations:
42a88c722eSMomchil Velikov // - sign-return-address=all
43a88c722eSMomchil Velikov // - sign-return-address=non-leaf and the functions spills the LR
44a88c722eSMomchil Velikov if (!F.hasFnAttribute("sign-return-address")) {
45a88c722eSMomchil Velikov const Module &M = *F.getParent();
46a88c722eSMomchil Velikov if (const auto *Sign = mdconst::extract_or_null<ConstantInt>(
47a88c722eSMomchil Velikov M.getModuleFlag("sign-return-address"))) {
48a88c722eSMomchil Velikov if (Sign->getZExtValue()) {
49a88c722eSMomchil Velikov if (const auto *All = mdconst::extract_or_null<ConstantInt>(
50a88c722eSMomchil Velikov M.getModuleFlag("sign-return-address-all")))
51a88c722eSMomchil Velikov return {true, All->getZExtValue()};
52a88c722eSMomchil Velikov return {true, false};
53a88c722eSMomchil Velikov }
54a88c722eSMomchil Velikov }
55a88c722eSMomchil Velikov return {false, false};
56a88c722eSMomchil Velikov }
57a88c722eSMomchil Velikov
58a88c722eSMomchil Velikov StringRef Scope = F.getFnAttribute("sign-return-address").getValueAsString();
59a88c722eSMomchil Velikov if (Scope.equals("none"))
60a88c722eSMomchil Velikov return {false, false};
61a88c722eSMomchil Velikov
62a88c722eSMomchil Velikov if (Scope.equals("all"))
63a88c722eSMomchil Velikov return {true, true};
64a88c722eSMomchil Velikov
65a88c722eSMomchil Velikov assert(Scope.equals("non-leaf"));
66a88c722eSMomchil Velikov return {true, false};
67a88c722eSMomchil Velikov }
68a88c722eSMomchil Velikov
ShouldSignWithBKey(const Function & F)69a88c722eSMomchil Velikov static bool ShouldSignWithBKey(const Function &F) {
70a88c722eSMomchil Velikov if (!F.hasFnAttribute("sign-return-address-key")) {
71a88c722eSMomchil Velikov if (const auto *BKey = mdconst::extract_or_null<ConstantInt>(
72a88c722eSMomchil Velikov F.getParent()->getModuleFlag("sign-return-address-with-bkey")))
73a88c722eSMomchil Velikov return BKey->getZExtValue();
74a88c722eSMomchil Velikov return false;
75a88c722eSMomchil Velikov }
76a88c722eSMomchil Velikov
77a88c722eSMomchil Velikov const StringRef Key =
78a88c722eSMomchil Velikov F.getFnAttribute("sign-return-address-key").getValueAsString();
7942f74e82SMartin Storsjö assert(Key.equals_insensitive("a_key") || Key.equals_insensitive("b_key"));
8042f74e82SMartin Storsjö return Key.equals_insensitive("b_key");
81a88c722eSMomchil Velikov }
82a88c722eSMomchil Velikov
AArch64FunctionInfo(MachineFunction & MF_)83cc5a1b3dSMatt Arsenault AArch64FunctionInfo::AArch64FunctionInfo(MachineFunction &MF_) : MF(&MF_) {
84a88c722eSMomchil Velikov // If we already know that the function doesn't have a redzone, set
85a88c722eSMomchil Velikov // HasRedZone here.
86cc5a1b3dSMatt Arsenault if (MF->getFunction().hasFnAttribute(Attribute::NoRedZone))
87a88c722eSMomchil Velikov HasRedZone = false;
88a88c722eSMomchil Velikov
89cc5a1b3dSMatt Arsenault const Function &F = MF->getFunction();
90a88c722eSMomchil Velikov std::tie(SignReturnAddress, SignReturnAddressAll) = GetSignReturnAddress(F);
91a88c722eSMomchil Velikov SignWithBKey = ShouldSignWithBKey(F);
920593ce5fSFlorian Mayer // TODO: skip functions that have no instrumented allocas for optimization
930593ce5fSFlorian Mayer IsMTETagged = F.hasFnAttribute(Attribute::SanitizeMemTag);
94a88c722eSMomchil Velikov
95a88c722eSMomchil Velikov if (!F.hasFnAttribute("branch-target-enforcement")) {
96a88c722eSMomchil Velikov if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
97a88c722eSMomchil Velikov F.getParent()->getModuleFlag("branch-target-enforcement")))
98a88c722eSMomchil Velikov BranchTargetEnforcement = BTE->getZExtValue();
99a88c722eSMomchil Velikov return;
100a88c722eSMomchil Velikov }
101a88c722eSMomchil Velikov
10242f74e82SMartin Storsjö const StringRef BTIEnable =
10342f74e82SMartin Storsjö F.getFnAttribute("branch-target-enforcement").getValueAsString();
10442f74e82SMartin Storsjö assert(BTIEnable.equals_insensitive("true") ||
10542f74e82SMartin Storsjö BTIEnable.equals_insensitive("false"));
10642f74e82SMartin Storsjö BranchTargetEnforcement = BTIEnable.equals_insensitive("true");
107a88c722eSMomchil Velikov }
108a88c722eSMomchil Velikov
clone(BumpPtrAllocator & Allocator,MachineFunction & DestMF,const DenseMap<MachineBasicBlock *,MachineBasicBlock * > & Src2DstMBB) const109cc5a1b3dSMatt Arsenault MachineFunctionInfo *AArch64FunctionInfo::clone(
110cc5a1b3dSMatt Arsenault BumpPtrAllocator &Allocator, MachineFunction &DestMF,
111cc5a1b3dSMatt Arsenault const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
112cc5a1b3dSMatt Arsenault const {
113cc5a1b3dSMatt Arsenault AArch64FunctionInfo *InfoClone = DestMF.cloneInfo<AArch64FunctionInfo>(*this);
114cc5a1b3dSMatt Arsenault InfoClone->MF = &DestMF;
115cc5a1b3dSMatt Arsenault return InfoClone;
116cc5a1b3dSMatt Arsenault }
117cc5a1b3dSMatt Arsenault
shouldSignReturnAddress(bool SpillsLR) const118a88c722eSMomchil Velikov bool AArch64FunctionInfo::shouldSignReturnAddress(bool SpillsLR) const {
119a88c722eSMomchil Velikov if (!SignReturnAddress)
120a88c722eSMomchil Velikov return false;
121a88c722eSMomchil Velikov if (SignReturnAddressAll)
122a88c722eSMomchil Velikov return true;
123a88c722eSMomchil Velikov return SpillsLR;
124a88c722eSMomchil Velikov }
125a88c722eSMomchil Velikov
shouldSignReturnAddress() const126a88c722eSMomchil Velikov bool AArch64FunctionInfo::shouldSignReturnAddress() const {
127a88c722eSMomchil Velikov return shouldSignReturnAddress(llvm::any_of(
128cc5a1b3dSMatt Arsenault MF->getFrameInfo().getCalleeSavedInfo(),
129a88c722eSMomchil Velikov [](const auto &Info) { return Info.getReg() == AArch64::LR; }));
130a88c722eSMomchil Velikov }
13125e92920SMomchil Velikov
needsDwarfUnwindInfo() const13225e92920SMomchil Velikov bool AArch64FunctionInfo::needsDwarfUnwindInfo() const {
133d0ea42a7SMomchil Velikov if (!NeedsDwarfUnwindInfo)
134cc5a1b3dSMatt Arsenault NeedsDwarfUnwindInfo = MF->needsFrameMoves() &&
135cc5a1b3dSMatt Arsenault !MF->getTarget().getMCAsmInfo()->usesWindowsCFI();
13625e92920SMomchil Velikov
137d0ea42a7SMomchil Velikov return *NeedsDwarfUnwindInfo;
13825e92920SMomchil Velikov }
13925e92920SMomchil Velikov
needsAsyncDwarfUnwindInfo() const14025e92920SMomchil Velikov bool AArch64FunctionInfo::needsAsyncDwarfUnwindInfo() const {
141d0ea42a7SMomchil Velikov if (!NeedsAsyncDwarfUnwindInfo) {
142cc5a1b3dSMatt Arsenault const Function &F = MF->getFunction();
143d0ea42a7SMomchil Velikov // The check got "minsize" is because epilogue unwind info is not emitted
144d0ea42a7SMomchil Velikov // (yet) for homogeneous epilogues, outlined functions, and functions
145d0ea42a7SMomchil Velikov // outlined from.
146d0ea42a7SMomchil Velikov NeedsAsyncDwarfUnwindInfo = needsDwarfUnwindInfo() &&
147d0ea42a7SMomchil Velikov F.getUWTableKind() == UWTableKind::Async &&
148d0ea42a7SMomchil Velikov !F.hasMinSize();
149d0ea42a7SMomchil Velikov }
150d0ea42a7SMomchil Velikov return *NeedsAsyncDwarfUnwindInfo;
15125e92920SMomchil Velikov }
152