1 //===- ForceFunctionAttrs.cpp - Force function attrs for debugging --------===// 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 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h" 10 #include "llvm/ADT/StringSwitch.h" 11 #include "llvm/IR/Function.h" 12 #include "llvm/IR/LLVMContext.h" 13 #include "llvm/IR/Module.h" 14 #include "llvm/InitializePasses.h" 15 #include "llvm/Support/CommandLine.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/raw_ostream.h" 18 using namespace llvm; 19 20 #define DEBUG_TYPE "forceattrs" 21 22 static cl::list<std::string> 23 ForceAttributes("force-attribute", cl::Hidden, 24 cl::desc("Add an attribute to a function. This should be a " 25 "pair of 'function-name:attribute-name', for " 26 "example -force-attribute=foo:noinline. This " 27 "option can be specified multiple times.")); 28 29 static Attribute::AttrKind parseAttrKind(StringRef Kind) { 30 return StringSwitch<Attribute::AttrKind>(Kind) 31 .Case("alwaysinline", Attribute::AlwaysInline) 32 .Case("builtin", Attribute::Builtin) 33 .Case("cold", Attribute::Cold) 34 .Case("convergent", Attribute::Convergent) 35 .Case("inlinehint", Attribute::InlineHint) 36 .Case("jumptable", Attribute::JumpTable) 37 .Case("minsize", Attribute::MinSize) 38 .Case("naked", Attribute::Naked) 39 .Case("nobuiltin", Attribute::NoBuiltin) 40 .Case("noduplicate", Attribute::NoDuplicate) 41 .Case("noimplicitfloat", Attribute::NoImplicitFloat) 42 .Case("noinline", Attribute::NoInline) 43 .Case("nonlazybind", Attribute::NonLazyBind) 44 .Case("noredzone", Attribute::NoRedZone) 45 .Case("noreturn", Attribute::NoReturn) 46 .Case("nocf_check", Attribute::NoCfCheck) 47 .Case("norecurse", Attribute::NoRecurse) 48 .Case("nounwind", Attribute::NoUnwind) 49 .Case("optforfuzzing", Attribute::OptForFuzzing) 50 .Case("optnone", Attribute::OptimizeNone) 51 .Case("optsize", Attribute::OptimizeForSize) 52 .Case("readnone", Attribute::ReadNone) 53 .Case("readonly", Attribute::ReadOnly) 54 .Case("argmemonly", Attribute::ArgMemOnly) 55 .Case("returns_twice", Attribute::ReturnsTwice) 56 .Case("safestack", Attribute::SafeStack) 57 .Case("shadowcallstack", Attribute::ShadowCallStack) 58 .Case("sanitize_address", Attribute::SanitizeAddress) 59 .Case("sanitize_hwaddress", Attribute::SanitizeHWAddress) 60 .Case("sanitize_memory", Attribute::SanitizeMemory) 61 .Case("sanitize_thread", Attribute::SanitizeThread) 62 .Case("sanitize_memtag", Attribute::SanitizeMemTag) 63 .Case("speculative_load_hardening", Attribute::SpeculativeLoadHardening) 64 .Case("ssp", Attribute::StackProtect) 65 .Case("sspreq", Attribute::StackProtectReq) 66 .Case("sspstrong", Attribute::StackProtectStrong) 67 .Case("strictfp", Attribute::StrictFP) 68 .Case("uwtable", Attribute::UWTable) 69 .Default(Attribute::None); 70 } 71 72 /// If F has any forced attributes given on the command line, add them. 73 static void addForcedAttributes(Function &F) { 74 for (auto &S : ForceAttributes) { 75 auto KV = StringRef(S).split(':'); 76 if (KV.first != F.getName()) 77 continue; 78 79 auto Kind = parseAttrKind(KV.second); 80 if (Kind == Attribute::None) { 81 LLVM_DEBUG(dbgs() << "ForcedAttribute: " << KV.second 82 << " unknown or not handled!\n"); 83 continue; 84 } 85 if (F.hasFnAttribute(Kind)) 86 continue; 87 F.addFnAttr(Kind); 88 } 89 } 90 91 static bool hasForceAttributes() { return !ForceAttributes.empty(); } 92 93 PreservedAnalyses ForceFunctionAttrsPass::run(Module &M, 94 ModuleAnalysisManager &) { 95 if (!hasForceAttributes()) 96 return PreservedAnalyses::all(); 97 98 for (Function &F : M.functions()) 99 addForcedAttributes(F); 100 101 // Just conservatively invalidate analyses, this isn't likely to be important. 102 return PreservedAnalyses::none(); 103 } 104 105 namespace { 106 struct ForceFunctionAttrsLegacyPass : public ModulePass { 107 static char ID; // Pass identification, replacement for typeid 108 ForceFunctionAttrsLegacyPass() : ModulePass(ID) { 109 initializeForceFunctionAttrsLegacyPassPass( 110 *PassRegistry::getPassRegistry()); 111 } 112 113 bool runOnModule(Module &M) override { 114 if (ForceAttributes.empty()) 115 return false; 116 117 for (Function &F : M.functions()) 118 addForcedAttributes(F); 119 120 // Conservatively assume we changed something. 121 return true; 122 } 123 }; 124 } 125 126 char ForceFunctionAttrsLegacyPass::ID = 0; 127 INITIALIZE_PASS(ForceFunctionAttrsLegacyPass, "forceattrs", 128 "Force set function attributes", false, false) 129 130 Pass *llvm::createForceFunctionAttrsLegacyPass() { 131 return new ForceFunctionAttrsLegacyPass(); 132 } 133