3131#include " llvm/ADT/StringRef.h"
3232#include " llvm/ADT/StringSet.h"
3333#include " llvm/Frontend/OpenMP/OMPGridValues.h"
34+ #include " llvm/IR/Attributes.h"
3435#include " llvm/IR/DerivedTypes.h"
36+ #include " llvm/IR/Function.h"
3537#include " llvm/Support/DataTypes.h"
3638#include " llvm/Support/Error.h"
3739#include " llvm/Support/VersionTuple.h"
@@ -1368,15 +1370,14 @@ class TargetInfo : public TransferrableTargetInfo,
13681370 return StringRef ();
13691371 }
13701372
1371- struct BranchProtectionInfo {
1373+ class BranchProtectionInfo {
1374+ public:
13721375 LangOptions::SignReturnAddressScopeKind SignReturnAddr;
13731376 LangOptions::SignReturnAddressKeyKind SignKey;
13741377 bool BranchTargetEnforcement;
13751378 bool BranchProtectionPAuthLR;
13761379 bool GuardedControlStack;
13771380
1378- BranchProtectionInfo () = default ;
1379-
13801381 const char *getSignReturnAddrStr () const {
13811382 switch (SignReturnAddr) {
13821383 case LangOptions::SignReturnAddressScopeKind::None:
@@ -1388,7 +1389,6 @@ class TargetInfo : public TransferrableTargetInfo,
13881389 }
13891390 llvm_unreachable (" Unexpected SignReturnAddressScopeKind" );
13901391 }
1391-
13921392 const char *getSignKeyStr () const {
13931393 switch (SignKey) {
13941394 case LangOptions::SignReturnAddressKeyKind::AKey:
@@ -1398,6 +1398,41 @@ class TargetInfo : public TransferrableTargetInfo,
13981398 }
13991399 llvm_unreachable (" Unexpected SignReturnAddressKeyKind" );
14001400 }
1401+
1402+ BranchProtectionInfo () = default ;
1403+ BranchProtectionInfo (const LangOptions &LangOpts) {
1404+ SignReturnAddr =
1405+ LangOpts.hasSignReturnAddress ()
1406+ ? (LangOpts.isSignReturnAddressScopeAll ()
1407+ ? LangOptions::SignReturnAddressScopeKind::All
1408+ : LangOptions::SignReturnAddressScopeKind::NonLeaf)
1409+ : LangOptions::SignReturnAddressScopeKind::None;
1410+ SignKey = LangOpts.isSignReturnAddressWithAKey ()
1411+ ? LangOptions::SignReturnAddressKeyKind::AKey
1412+ : LangOptions::SignReturnAddressKeyKind::BKey;
1413+ BranchTargetEnforcement = LangOpts.BranchTargetEnforcement ;
1414+ BranchProtectionPAuthLR = LangOpts.BranchProtectionPAuthLR ;
1415+ GuardedControlStack = LangOpts.GuardedControlStack ;
1416+ }
1417+
1418+ void setFnAttributes (llvm::Function &F) {
1419+ llvm::AttrBuilder FuncAttrs (F.getContext ());
1420+ setFnAttributes (FuncAttrs);
1421+ F.addFnAttrs (FuncAttrs);
1422+ }
1423+
1424+ void setFnAttributes (llvm::AttrBuilder &FuncAttrs) {
1425+ if (SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
1426+ FuncAttrs.addAttribute (" sign-return-address" , getSignReturnAddrStr ());
1427+ FuncAttrs.addAttribute (" sign-return-address-key" , getSignKeyStr ());
1428+ }
1429+ if (BranchTargetEnforcement)
1430+ FuncAttrs.addAttribute (" branch-target-enforcement" , " true" );
1431+ if (BranchProtectionPAuthLR)
1432+ FuncAttrs.addAttribute (" branch-protection-pauth-lr" , " true" );
1433+ if (GuardedControlStack)
1434+ FuncAttrs.addAttribute (" guarded-control-stack" , " true" );
1435+ }
14011436 };
14021437
14031438 // / Determine if the Architecture in this TargetInfo supports branch
0 commit comments