AArch64MachineFunctionInfo.cpp 3.9 KB

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