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.hasValue()) 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 93 if (!F.hasFnAttribute("branch-target-enforcement")) { 94 if (const auto *BTE = mdconst::extract_or_null<ConstantInt>( 95 F.getParent()->getModuleFlag("branch-target-enforcement"))) 96 BranchTargetEnforcement = BTE->getZExtValue(); 97 return; 98 } 99 100 const StringRef BTIEnable = 101 F.getFnAttribute("branch-target-enforcement").getValueAsString(); 102 assert(BTIEnable.equals_insensitive("true") || 103 BTIEnable.equals_insensitive("false")); 104 BranchTargetEnforcement = BTIEnable.equals_insensitive("true"); 105 } 106 107 bool AArch64FunctionInfo::shouldSignReturnAddress(bool SpillsLR) const { 108 if (!SignReturnAddress) 109 return false; 110 if (SignReturnAddressAll) 111 return true; 112 return SpillsLR; 113 } 114 115 bool AArch64FunctionInfo::shouldSignReturnAddress() const { 116 return shouldSignReturnAddress(llvm::any_of( 117 MF.getFrameInfo().getCalleeSavedInfo(), 118 [](const auto &Info) { return Info.getReg() == AArch64::LR; })); 119 } 120 121 bool AArch64FunctionInfo::needsDwarfUnwindInfo() const { 122 if (!NeedsDwarfUnwindInfo) 123 NeedsDwarfUnwindInfo = MF.needsFrameMoves() && 124 !MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); 125 126 return *NeedsDwarfUnwindInfo; 127 } 128 129 bool AArch64FunctionInfo::needsAsyncDwarfUnwindInfo() const { 130 if (!NeedsAsyncDwarfUnwindInfo) { 131 const Function &F = MF.getFunction(); 132 // The check got "minsize" is because epilogue unwind info is not emitted 133 // (yet) for homogeneous epilogues, outlined functions, and functions 134 // outlined from. 135 NeedsAsyncDwarfUnwindInfo = needsDwarfUnwindInfo() && 136 F.getUWTableKind() == UWTableKind::Async && 137 !F.hasMinSize(); 138 } 139 return *NeedsAsyncDwarfUnwindInfo; 140 } 141