LLVM  12.0.0git
AArch64MachineFunctionInfo.cpp
Go to the documentation of this file.
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 
17 #include "AArch64InstrInfo.h"
18 #include <llvm/IR/Metadata.h>
19 #include <llvm/IR/Module.h>
20 
21 using namespace llvm;
22 
24  const llvm::AArch64FunctionInfo &MFI)
25  : HasRedZone(MFI.hasRedZone()) {}
26 
29 }
30 
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_lower("a_key") || Key.equals_lower("b_key"));
77  return Key.equals_lower("b_key");
78 }
79 
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 = F.getFnAttribute("branch-target-enforcement").getValueAsString();
98  assert(BTIEnable.equals_lower("true") || BTIEnable.equals_lower("false"));
99  BranchTargetEnforcement = BTIEnable.equals_lower("true");
100 }
101 
103  if (!SignReturnAddress)
104  return false;
105  if (SignReturnAddressAll)
106  return true;
107  return SpillsLR;
108 }
109 
113  [](const auto &Info) { return Info.getReg() == AArch64::LR; }));
114 }
AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
LLVM_NODISCARD bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
Definition: StringRef.h:197
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
This file contains the declarations for metadata subclasses.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:345
F(f)
Function & getFunction()
Return the LLVM function that this machine code represents.
Key
PAL metadata keys.
Analysis containing CSE Info
Definition: CSEInfo.cpp:25
void initializeBaseYamlFields(const yaml::AArch64FunctionInfo &YamlMFI)
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1505
Module.h This file contains the declarations for the Module class.
static std::pair< bool, bool > GetSignReturnAddress(const Function &F)
constexpr bool hasValue() const
Definition: Optional.h:263
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
static bool ShouldSignWithBKey(const Function &F)
IO & YamlIO
Definition: ELFYAML.cpp:1049
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void mappingImpl(yaml::IO &YamlIO) override
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57