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 
16 #include "AArch64MachineFunctionInfo.h"
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 
24 using namespace llvm;
25 
26 yaml::AArch64FunctionInfo::AArch64FunctionInfo(
27     const llvm::AArch64FunctionInfo &MFI)
28     : HasRedZone(MFI.hasRedZone()) {}
29 
30 void yaml::AArch64FunctionInfo::mappingImpl(yaml::IO &YamlIO) {
31   MappingTraits<AArch64FunctionInfo>::mapping(YamlIO, *this);
32 }
33 
34 void AArch64FunctionInfo::initializeBaseYamlFields(
35     const yaml::AArch64FunctionInfo &YamlMFI) {
36   if (YamlMFI.HasRedZone)
37     HasRedZone = YamlMFI.HasRedZone;
38 }
39 
40 static 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 
69 static bool ShouldSignWithBKey(const Function &F) {
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     return false;
75   }
76 
77   const StringRef Key =
78       F.getFnAttribute("sign-return-address-key").getValueAsString();
79   assert(Key.equals_insensitive("a_key") || Key.equals_insensitive("b_key"));
80   return Key.equals_insensitive("b_key");
81 }
82 
83 AArch64FunctionInfo::AArch64FunctionInfo(MachineFunction &MF_) : MF(&MF_) {
84   // If we already know that the function doesn't have a redzone, set
85   // HasRedZone here.
86   if (MF->getFunction().hasFnAttribute(Attribute::NoRedZone))
87     HasRedZone = false;
88 
89   const Function &F = MF->getFunction();
90   std::tie(SignReturnAddress, SignReturnAddressAll) = GetSignReturnAddress(F);
91   SignWithBKey = ShouldSignWithBKey(F);
92   // TODO: skip functions that have no instrumented allocas for optimization
93   IsMTETagged = F.hasFnAttribute(Attribute::SanitizeMemTag);
94 
95   if (!F.hasFnAttribute("branch-target-enforcement")) {
96     if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
97             F.getParent()->getModuleFlag("branch-target-enforcement")))
98       BranchTargetEnforcement = BTE->getZExtValue();
99     return;
100   }
101 
102   const StringRef BTIEnable =
103       F.getFnAttribute("branch-target-enforcement").getValueAsString();
104   assert(BTIEnable.equals_insensitive("true") ||
105          BTIEnable.equals_insensitive("false"));
106   BranchTargetEnforcement = BTIEnable.equals_insensitive("true");
107 }
108 
109 MachineFunctionInfo *AArch64FunctionInfo::clone(
110     BumpPtrAllocator &Allocator, MachineFunction &DestMF,
111     const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
112     const {
113   AArch64FunctionInfo *InfoClone = DestMF.cloneInfo<AArch64FunctionInfo>(*this);
114   InfoClone->MF = &DestMF;
115   return InfoClone;
116 }
117 
118 bool AArch64FunctionInfo::shouldSignReturnAddress(bool SpillsLR) const {
119   if (!SignReturnAddress)
120     return false;
121   if (SignReturnAddressAll)
122     return true;
123   return SpillsLR;
124 }
125 
126 bool AArch64FunctionInfo::shouldSignReturnAddress() const {
127   return shouldSignReturnAddress(llvm::any_of(
128       MF->getFrameInfo().getCalleeSavedInfo(),
129       [](const auto &Info) { return Info.getReg() == AArch64::LR; }));
130 }
131 
132 bool AArch64FunctionInfo::needsDwarfUnwindInfo() const {
133   if (!NeedsDwarfUnwindInfo)
134     NeedsDwarfUnwindInfo = MF->needsFrameMoves() &&
135                            !MF->getTarget().getMCAsmInfo()->usesWindowsCFI();
136 
137   return *NeedsDwarfUnwindInfo;
138 }
139 
140 bool AArch64FunctionInfo::needsAsyncDwarfUnwindInfo() const {
141   if (!NeedsAsyncDwarfUnwindInfo) {
142     const Function &F = MF->getFunction();
143     //  The check got "minsize" is because epilogue unwind info is not emitted
144     //  (yet) for homogeneous epilogues, outlined functions, and functions
145     //  outlined from.
146     NeedsAsyncDwarfUnwindInfo = needsDwarfUnwindInfo() &&
147                                 F.getUWTableKind() == UWTableKind::Async &&
148                                 !F.hasMinSize();
149   }
150   return *NeedsAsyncDwarfUnwindInfo;
151 }
152