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 cl::list<std::string> ForceRemoveAttributes( 30 "force-remove-attribute", cl::Hidden, 31 cl::desc("Remove an attribute from a function. This should be a " 32 "pair of 'function-name:attribute-name', for " 33 "example -force-remove-attribute=foo:noinline. This " 34 "option can be specified multiple times.")); 35 36 static Attribute::AttrKind parseAttrKind(StringRef Kind) { 37 return StringSwitch<Attribute::AttrKind>(Kind) 38 .Case("alwaysinline", Attribute::AlwaysInline) 39 .Case("builtin", Attribute::Builtin) 40 .Case("cold", Attribute::Cold) 41 .Case("convergent", Attribute::Convergent) 42 .Case("inlinehint", Attribute::InlineHint) 43 .Case("jumptable", Attribute::JumpTable) 44 .Case("minsize", Attribute::MinSize) 45 .Case("naked", Attribute::Naked) 46 .Case("nobuiltin", Attribute::NoBuiltin) 47 .Case("noduplicate", Attribute::NoDuplicate) 48 .Case("noimplicitfloat", Attribute::NoImplicitFloat) 49 .Case("noinline", Attribute::NoInline) 50 .Case("nonlazybind", Attribute::NonLazyBind) 51 .Case("noredzone", Attribute::NoRedZone) 52 .Case("noreturn", Attribute::NoReturn) 53 .Case("nocf_check", Attribute::NoCfCheck) 54 .Case("norecurse", Attribute::NoRecurse) 55 .Case("nounwind", Attribute::NoUnwind) 56 .Case("nosanitize_coverage", Attribute::NoSanitizeCoverage) 57 .Case("optforfuzzing", Attribute::OptForFuzzing) 58 .Case("optnone", Attribute::OptimizeNone) 59 .Case("optsize", Attribute::OptimizeForSize) 60 .Case("readnone", Attribute::ReadNone) 61 .Case("readonly", Attribute::ReadOnly) 62 .Case("argmemonly", Attribute::ArgMemOnly) 63 .Case("returns_twice", Attribute::ReturnsTwice) 64 .Case("safestack", Attribute::SafeStack) 65 .Case("shadowcallstack", Attribute::ShadowCallStack) 66 .Case("sanitize_address", Attribute::SanitizeAddress) 67 .Case("sanitize_hwaddress", Attribute::SanitizeHWAddress) 68 .Case("sanitize_memory", Attribute::SanitizeMemory) 69 .Case("sanitize_thread", Attribute::SanitizeThread) 70 .Case("sanitize_memtag", Attribute::SanitizeMemTag) 71 .Case("speculative_load_hardening", Attribute::SpeculativeLoadHardening) 72 .Case("ssp", Attribute::StackProtect) 73 .Case("sspreq", Attribute::StackProtectReq) 74 .Case("sspstrong", Attribute::StackProtectStrong) 75 .Case("strictfp", Attribute::StrictFP) 76 .Case("uwtable", Attribute::UWTable) 77 .Case("vscale_range", Attribute::VScaleRange) 78 .Default(Attribute::None); 79 } 80 81 /// If F has any forced attributes given on the command line, add them. 82 /// If F has any forced remove attributes given on the command line, remove 83 /// them. When both force and force-remove are given to a function, the latter 84 /// takes precedence. 85 static void forceAttributes(Function &F) { 86 auto ParseFunctionAndAttr = [&](StringRef S) { 87 auto Kind = Attribute::None; 88 auto KV = StringRef(S).split(':'); 89 if (KV.first != F.getName()) 90 return Kind; 91 Kind = parseAttrKind(KV.second); 92 if (Kind == Attribute::None) { 93 LLVM_DEBUG(dbgs() << "ForcedAttribute: " << KV.second 94 << " unknown or not handled!\n"); 95 } 96 return Kind; 97 }; 98 99 for (auto &S : ForceAttributes) { 100 auto Kind = ParseFunctionAndAttr(S); 101 if (Kind == Attribute::None || F.hasFnAttribute(Kind)) 102 continue; 103 F.addFnAttr(Kind); 104 } 105 106 for (auto &S : ForceRemoveAttributes) { 107 auto Kind = ParseFunctionAndAttr(S); 108 if (Kind == Attribute::None || !F.hasFnAttribute(Kind)) 109 continue; 110 F.removeFnAttr(Kind); 111 } 112 } 113 114 static bool hasForceAttributes() { 115 return !ForceAttributes.empty() || !ForceRemoveAttributes.empty(); 116 } 117 118 PreservedAnalyses ForceFunctionAttrsPass::run(Module &M, 119 ModuleAnalysisManager &) { 120 if (!hasForceAttributes()) 121 return PreservedAnalyses::all(); 122 123 for (Function &F : M.functions()) 124 forceAttributes(F); 125 126 // Just conservatively invalidate analyses, this isn't likely to be important. 127 return PreservedAnalyses::none(); 128 } 129 130 namespace { 131 struct ForceFunctionAttrsLegacyPass : public ModulePass { 132 static char ID; // Pass identification, replacement for typeid 133 ForceFunctionAttrsLegacyPass() : ModulePass(ID) { 134 initializeForceFunctionAttrsLegacyPassPass( 135 *PassRegistry::getPassRegistry()); 136 } 137 138 bool runOnModule(Module &M) override { 139 if (!hasForceAttributes()) 140 return false; 141 142 for (Function &F : M.functions()) 143 forceAttributes(F); 144 145 // Conservatively assume we changed something. 146 return true; 147 } 148 }; 149 } 150 151 char ForceFunctionAttrsLegacyPass::ID = 0; 152 INITIALIZE_PASS(ForceFunctionAttrsLegacyPass, "forceattrs", 153 "Force set function attributes", false, false) 154 155 Pass *llvm::createForceFunctionAttrsLegacyPass() { 156 return new ForceFunctionAttrsLegacyPass(); 157 } 158