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 <llvm/IR/Metadata.h> 19 #include <llvm/IR/Module.h> 20 21 using namespace llvm; 22 23 yaml::AArch64FunctionInfo::AArch64FunctionInfo( 24 const llvm::AArch64FunctionInfo &MFI) 25 : HasRedZone(MFI.hasRedZone()) {} 26 27 void yaml::AArch64FunctionInfo::mappingImpl(yaml::IO &YamlIO) { 28 MappingTraits<AArch64FunctionInfo>::mapping(YamlIO, *this); 29 } 30 31 void AArch64FunctionInfo::initializeBaseYamlFields( 32 const yaml::AArch64FunctionInfo &YamlMFI) { 33 if (YamlMFI.HasRedZone.hasValue()) 34 HasRedZone = YamlMFI.HasRedZone; 35 } 36 37 static std::pair<bool, bool> GetSignReturnAddress(const Function &F) { 38 // The function should be signed in the following situations: 39 // - sign-return-address=all 40 // - sign-return-address=non-leaf and the functions spills the LR 41 if (!F.hasFnAttribute("sign-return-address")) { 42 const Module &M = *F.getParent(); 43 if (const auto *Sign = mdconst::extract_or_null<ConstantInt>( 44 M.getModuleFlag("sign-return-address"))) { 45 if (Sign->getZExtValue()) { 46 if (const auto *All = mdconst::extract_or_null<ConstantInt>( 47 M.getModuleFlag("sign-return-address-all"))) 48 return {true, All->getZExtValue()}; 49 return {true, false}; 50 } 51 } 52 return {false, false}; 53 } 54 55 StringRef Scope = F.getFnAttribute("sign-return-address").getValueAsString(); 56 if (Scope.equals("none")) 57 return {false, false}; 58 59 if (Scope.equals("all")) 60 return {true, true}; 61 62 assert(Scope.equals("non-leaf")); 63 return {true, false}; 64 } 65 66 static bool ShouldSignWithBKey(const Function &F) { 67 if (!F.hasFnAttribute("sign-return-address-key")) { 68 if (const auto *BKey = mdconst::extract_or_null<ConstantInt>( 69 F.getParent()->getModuleFlag("sign-return-address-with-bkey"))) 70 return BKey->getZExtValue(); 71 return false; 72 } 73 74 const StringRef Key = 75 F.getFnAttribute("sign-return-address-key").getValueAsString(); 76 assert(Key.equals_insensitive("a_key") || Key.equals_insensitive("b_key")); 77 return Key.equals_insensitive("b_key"); 78 } 79 80 AArch64FunctionInfo::AArch64FunctionInfo(MachineFunction &MF) : MF(MF) { 81 // If we already know that the function doesn't have a redzone, set 82 // HasRedZone here. 83 if (MF.getFunction().hasFnAttribute(Attribute::NoRedZone)) 84 HasRedZone = false; 85 86 const Function &F = MF.getFunction(); 87 std::tie(SignReturnAddress, SignReturnAddressAll) = GetSignReturnAddress(F); 88 SignWithBKey = ShouldSignWithBKey(F); 89 90 if (!F.hasFnAttribute("branch-target-enforcement")) { 91 if (const auto *BTE = mdconst::extract_or_null<ConstantInt>( 92 F.getParent()->getModuleFlag("branch-target-enforcement"))) 93 BranchTargetEnforcement = BTE->getZExtValue(); 94 return; 95 } 96 97 const StringRef BTIEnable = 98 F.getFnAttribute("branch-target-enforcement").getValueAsString(); 99 assert(BTIEnable.equals_insensitive("true") || 100 BTIEnable.equals_insensitive("false")); 101 BranchTargetEnforcement = BTIEnable.equals_insensitive("true"); 102 } 103 104 bool AArch64FunctionInfo::shouldSignReturnAddress(bool SpillsLR) const { 105 if (!SignReturnAddress) 106 return false; 107 if (SignReturnAddressAll) 108 return true; 109 return SpillsLR; 110 } 111 112 bool AArch64FunctionInfo::shouldSignReturnAddress() const { 113 return shouldSignReturnAddress(llvm::any_of( 114 MF.getFrameInfo().getCalleeSavedInfo(), 115 [](const auto &Info) { return Info.getReg() == AArch64::LR; })); 116 } 117