ARMMachineFunctionInfo.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //===-- ARMMachineFunctionInfo.cpp - ARM 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. #include "ARMMachineFunctionInfo.h"
  9. #include "ARMSubtarget.h"
  10. using namespace llvm;
  11. void ARMFunctionInfo::anchor() {}
  12. static bool GetBranchTargetEnforcement(MachineFunction &MF) {
  13. const auto &Subtarget = MF.getSubtarget<ARMSubtarget>();
  14. if (!Subtarget.isMClass() || !Subtarget.hasV7Ops())
  15. return false;
  16. const Function &F = MF.getFunction();
  17. if (!F.hasFnAttribute("branch-target-enforcement")) {
  18. if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
  19. F.getParent()->getModuleFlag("branch-target-enforcement")))
  20. return BTE->getZExtValue();
  21. return false;
  22. }
  23. const StringRef BTIEnable =
  24. F.getFnAttribute("branch-target-enforcement").getValueAsString();
  25. assert(BTIEnable.equals_insensitive("true") ||
  26. BTIEnable.equals_insensitive("false"));
  27. return BTIEnable.equals_insensitive("true");
  28. }
  29. // The pair returns values for the ARMFunctionInfo members
  30. // SignReturnAddress and SignReturnAddressAll respectively.
  31. static std::pair<bool, bool> GetSignReturnAddress(const Function &F) {
  32. if (!F.hasFnAttribute("sign-return-address")) {
  33. const Module &M = *F.getParent();
  34. if (const auto *Sign = mdconst::extract_or_null<ConstantInt>(
  35. M.getModuleFlag("sign-return-address"))) {
  36. if (Sign->getZExtValue()) {
  37. if (const auto *All = mdconst::extract_or_null<ConstantInt>(
  38. M.getModuleFlag("sign-return-address-all")))
  39. return {true, All->getZExtValue()};
  40. return {true, false};
  41. }
  42. }
  43. return {false, false};
  44. }
  45. StringRef Scope = F.getFnAttribute("sign-return-address").getValueAsString();
  46. if (Scope.equals("none"))
  47. return {false, false};
  48. if (Scope.equals("all"))
  49. return {true, true};
  50. assert(Scope.equals("non-leaf"));
  51. return {true, false};
  52. }
  53. ARMFunctionInfo::ARMFunctionInfo(MachineFunction &MF)
  54. : isThumb(MF.getSubtarget<ARMSubtarget>().isThumb()),
  55. hasThumb2(MF.getSubtarget<ARMSubtarget>().hasThumb2()),
  56. IsCmseNSEntry(MF.getFunction().hasFnAttribute("cmse_nonsecure_entry")),
  57. IsCmseNSCall(MF.getFunction().hasFnAttribute("cmse_nonsecure_call")),
  58. BranchTargetEnforcement(GetBranchTargetEnforcement(MF)) {
  59. const auto &Subtarget = MF.getSubtarget<ARMSubtarget>();
  60. if (Subtarget.isMClass() && Subtarget.hasV7Ops())
  61. std::tie(SignReturnAddress, SignReturnAddressAll) =
  62. GetSignReturnAddress(MF.getFunction());
  63. }