AArch64MachineFunctionInfo.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //=- AArch64MachineFunctionInfo.cpp - AArch64 Machine Function Info ---------=//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// This file implements AArch64-specific per-machine-function
  11. /// information.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "AArch64MachineFunctionInfo.h"
  15. #include "AArch64InstrInfo.h"
  16. #include "AArch64Subtarget.h"
  17. #include "llvm/IR/Constants.h"
  18. #include "llvm/IR/Metadata.h"
  19. #include "llvm/IR/Module.h"
  20. #include "llvm/MC/MCAsmInfo.h"
  21. using namespace llvm;
  22. yaml::AArch64FunctionInfo::AArch64FunctionInfo(
  23. const llvm::AArch64FunctionInfo &MFI)
  24. : HasRedZone(MFI.hasRedZone()) {}
  25. void yaml::AArch64FunctionInfo::mappingImpl(yaml::IO &YamlIO) {
  26. MappingTraits<AArch64FunctionInfo>::mapping(YamlIO, *this);
  27. }
  28. void AArch64FunctionInfo::initializeBaseYamlFields(
  29. const yaml::AArch64FunctionInfo &YamlMFI) {
  30. if (YamlMFI.HasRedZone)
  31. HasRedZone = YamlMFI.HasRedZone;
  32. }
  33. static std::pair<bool, bool> GetSignReturnAddress(const Function &F) {
  34. // The function should be signed in the following situations:
  35. // - sign-return-address=all
  36. // - sign-return-address=non-leaf and the functions spills the LR
  37. if (!F.hasFnAttribute("sign-return-address")) {
  38. const Module &M = *F.getParent();
  39. if (const auto *Sign = mdconst::extract_or_null<ConstantInt>(
  40. M.getModuleFlag("sign-return-address"))) {
  41. if (Sign->getZExtValue()) {
  42. if (const auto *All = mdconst::extract_or_null<ConstantInt>(
  43. M.getModuleFlag("sign-return-address-all")))
  44. return {true, All->getZExtValue()};
  45. return {true, false};
  46. }
  47. }
  48. return {false, false};
  49. }
  50. StringRef Scope = F.getFnAttribute("sign-return-address").getValueAsString();
  51. if (Scope.equals("none"))
  52. return {false, false};
  53. if (Scope.equals("all"))
  54. return {true, true};
  55. assert(Scope.equals("non-leaf"));
  56. return {true, false};
  57. }
  58. static bool ShouldSignWithBKey(const Function &F, const AArch64Subtarget &STI) {
  59. if (!F.hasFnAttribute("sign-return-address-key")) {
  60. if (const auto *BKey = mdconst::extract_or_null<ConstantInt>(
  61. F.getParent()->getModuleFlag("sign-return-address-with-bkey")))
  62. return BKey->getZExtValue();
  63. if (STI.getTargetTriple().isOSWindows())
  64. return true;
  65. return false;
  66. }
  67. const StringRef Key =
  68. F.getFnAttribute("sign-return-address-key").getValueAsString();
  69. assert(Key.equals_insensitive("a_key") || Key.equals_insensitive("b_key"));
  70. return Key.equals_insensitive("b_key");
  71. }
  72. AArch64FunctionInfo::AArch64FunctionInfo(const Function &F,
  73. const AArch64Subtarget *STI) {
  74. // If we already know that the function doesn't have a redzone, set
  75. // HasRedZone here.
  76. if (F.hasFnAttribute(Attribute::NoRedZone))
  77. HasRedZone = false;
  78. std::tie(SignReturnAddress, SignReturnAddressAll) = GetSignReturnAddress(F);
  79. SignWithBKey = ShouldSignWithBKey(F, *STI);
  80. // TODO: skip functions that have no instrumented allocas for optimization
  81. IsMTETagged = F.hasFnAttribute(Attribute::SanitizeMemTag);
  82. if (!F.hasFnAttribute("branch-target-enforcement")) {
  83. if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
  84. F.getParent()->getModuleFlag("branch-target-enforcement")))
  85. BranchTargetEnforcement = BTE->getZExtValue();
  86. return;
  87. }
  88. const StringRef BTIEnable =
  89. F.getFnAttribute("branch-target-enforcement").getValueAsString();
  90. assert(BTIEnable.equals_insensitive("true") ||
  91. BTIEnable.equals_insensitive("false"));
  92. BranchTargetEnforcement = BTIEnable.equals_insensitive("true");
  93. }
  94. MachineFunctionInfo *AArch64FunctionInfo::clone(
  95. BumpPtrAllocator &Allocator, MachineFunction &DestMF,
  96. const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
  97. const {
  98. return DestMF.cloneInfo<AArch64FunctionInfo>(*this);
  99. }
  100. bool AArch64FunctionInfo::shouldSignReturnAddress(bool SpillsLR) const {
  101. if (!SignReturnAddress)
  102. return false;
  103. if (SignReturnAddressAll)
  104. return true;
  105. return SpillsLR;
  106. }
  107. bool AArch64FunctionInfo::shouldSignReturnAddress(
  108. const MachineFunction &MF) const {
  109. return shouldSignReturnAddress(llvm::any_of(
  110. MF.getFrameInfo().getCalleeSavedInfo(),
  111. [](const auto &Info) { return Info.getReg() == AArch64::LR; }));
  112. }
  113. bool AArch64FunctionInfo::needsDwarfUnwindInfo(
  114. const MachineFunction &MF) const {
  115. if (!NeedsDwarfUnwindInfo)
  116. NeedsDwarfUnwindInfo = MF.needsFrameMoves() &&
  117. !MF.getTarget().getMCAsmInfo()->usesWindowsCFI();
  118. return *NeedsDwarfUnwindInfo;
  119. }
  120. bool AArch64FunctionInfo::needsAsyncDwarfUnwindInfo(
  121. const MachineFunction &MF) const {
  122. if (!NeedsAsyncDwarfUnwindInfo) {
  123. const Function &F = MF.getFunction();
  124. // The check got "minsize" is because epilogue unwind info is not emitted
  125. // (yet) for homogeneous epilogues, outlined functions, and functions
  126. // outlined from.
  127. NeedsAsyncDwarfUnwindInfo = needsDwarfUnwindInfo(MF) &&
  128. F.getUWTableKind() == UWTableKind::Async &&
  129. !F.hasMinSize();
  130. }
  131. return *NeedsAsyncDwarfUnwindInfo;
  132. }