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 PreservedAnalyses ForceFunctionAttrsPass::run(Module &M, 92 ModuleAnalysisManager &) { 93 if (ForceAttributes.empty()) 94 return PreservedAnalyses::all(); 95 96 for (Function &F : M.functions()) 97 addForcedAttributes(F); 98 99 // Just conservatively invalidate analyses, this isn't likely to be important. 100 return PreservedAnalyses::none(); 101 } 102 103 namespace { 104 struct ForceFunctionAttrsLegacyPass : public ModulePass { 105 static char ID; // Pass identification, replacement for typeid 106 ForceFunctionAttrsLegacyPass() : ModulePass(ID) { 107 initializeForceFunctionAttrsLegacyPassPass( 108 *PassRegistry::getPassRegistry()); 109 } 110 111 bool runOnModule(Module &M) override { 112 if (ForceAttributes.empty()) 113 return false; 114 115 for (Function &F : M.functions()) 116 addForcedAttributes(F); 117 118 // Conservatively assume we changed something. 119 return true; 120 } 121 }; 122 } 123 124 char ForceFunctionAttrsLegacyPass::ID = 0; 125 INITIALIZE_PASS(ForceFunctionAttrsLegacyPass, "forceattrs", 126 "Force set function attributes", false, false) 127 128 Pass *llvm::createForceFunctionAttrsLegacyPass() { 129 return new ForceFunctionAttrsLegacyPass(); 130 } 131