1 //===- ForceFunctionAttrs.cpp - Force function attrs for debugging --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/IR/Function.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/raw_ostream.h"
19 #define DEBUG_TYPE "forceattrs"
21 static cl::list<std::string>
22 ForceAttributes("force-attribute", cl::Hidden,
23 cl::desc("Add an attribute to a function. This should be a "
24 "pair of 'function-name:attribute-name', for "
25 "example -force-attribute=foo:noinline. This "
26 "option can be specified multiple times."));
28 static Attribute::AttrKind parseAttrKind(StringRef Kind) {
29 return StringSwitch<Attribute::AttrKind>(Kind)
30 .Case("alwaysinline", Attribute::AlwaysInline)
31 .Case("builtin", Attribute::Builtin)
32 .Case("cold", Attribute::Cold)
33 .Case("convergent", Attribute::Convergent)
34 .Case("inlinehint", Attribute::InlineHint)
35 .Case("jumptable", Attribute::JumpTable)
36 .Case("minsize", Attribute::MinSize)
37 .Case("naked", Attribute::Naked)
38 .Case("nobuiltin", Attribute::NoBuiltin)
39 .Case("noduplicate", Attribute::NoDuplicate)
40 .Case("noimplicitfloat", Attribute::NoImplicitFloat)
41 .Case("noinline", Attribute::NoInline)
42 .Case("nonlazybind", Attribute::NonLazyBind)
43 .Case("noredzone", Attribute::NoRedZone)
44 .Case("noreturn", Attribute::NoReturn)
45 .Case("norecurse", Attribute::NoRecurse)
46 .Case("nounwind", Attribute::NoUnwind)
47 .Case("optnone", Attribute::OptimizeNone)
48 .Case("optsize", Attribute::OptimizeForSize)
49 .Case("readnone", Attribute::ReadNone)
50 .Case("readonly", Attribute::ReadOnly)
51 .Case("argmemonly", Attribute::ArgMemOnly)
52 .Case("returns_twice", Attribute::ReturnsTwice)
53 .Case("safestack", Attribute::SafeStack)
54 .Case("sanitize_address", Attribute::SanitizeAddress)
55 .Case("sanitize_hwaddress", Attribute::SanitizeHWAddress)
56 .Case("sanitize_memory", Attribute::SanitizeMemory)
57 .Case("sanitize_thread", Attribute::SanitizeThread)
58 .Case("ssp", Attribute::StackProtect)
59 .Case("sspreq", Attribute::StackProtectReq)
60 .Case("sspstrong", Attribute::StackProtectStrong)
61 .Case("strictfp", Attribute::StrictFP)
62 .Case("uwtable", Attribute::UWTable)
63 .Default(Attribute::None);
66 /// If F has any forced attributes given on the command line, add them.
67 static void addForcedAttributes(Function &F) {
68 for (auto &S : ForceAttributes) {
69 auto KV = StringRef(S).split(':');
70 if (KV.first != F.getName())
73 auto Kind = parseAttrKind(KV.second);
74 if (Kind == Attribute::None) {
75 DEBUG(dbgs() << "ForcedAttribute: " << KV.second
76 << " unknown or not handled!\n");
79 if (F.hasFnAttribute(Kind))
85 PreservedAnalyses ForceFunctionAttrsPass::run(Module &M,
86 ModuleAnalysisManager &) {
87 if (ForceAttributes.empty())
88 return PreservedAnalyses::all();
90 for (Function &F : M.functions())
91 addForcedAttributes(F);
93 // Just conservatively invalidate analyses, this isn't likely to be important.
94 return PreservedAnalyses::none();
98 struct ForceFunctionAttrsLegacyPass : public ModulePass {
99 static char ID; // Pass identification, replacement for typeid
100 ForceFunctionAttrsLegacyPass() : ModulePass(ID) {
101 initializeForceFunctionAttrsLegacyPassPass(
102 *PassRegistry::getPassRegistry());
105 bool runOnModule(Module &M) override {
106 if (ForceAttributes.empty())
109 for (Function &F : M.functions())
110 addForcedAttributes(F);
112 // Conservatively assume we changed something.
118 char ForceFunctionAttrsLegacyPass::ID = 0;
119 INITIALIZE_PASS(ForceFunctionAttrsLegacyPass, "forceattrs",
120 "Force set function attributes", false, false)
122 Pass *llvm::createForceFunctionAttrsLegacyPass() {
123 return new ForceFunctionAttrsLegacyPass();