xref: /llvm-project/llvm/lib/Transforms/IPO/ForceFunctionAttrs.cpp (revision d34e60ca8532511acb8c93ef26297e349fbec86a)
1 //===- ForceFunctionAttrs.cpp - Force function attrs for debugging --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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"
17 using namespace llvm;
18 
19 #define DEBUG_TYPE "forceattrs"
20 
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."));
27 
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("nocf_check", Attribute::NoCfCheck)
46       .Case("norecurse", Attribute::NoRecurse)
47       .Case("nounwind", Attribute::NoUnwind)
48       .Case("optforfuzzing", Attribute::OptForFuzzing)
49       .Case("optnone", Attribute::OptimizeNone)
50       .Case("optsize", Attribute::OptimizeForSize)
51       .Case("readnone", Attribute::ReadNone)
52       .Case("readonly", Attribute::ReadOnly)
53       .Case("argmemonly", Attribute::ArgMemOnly)
54       .Case("returns_twice", Attribute::ReturnsTwice)
55       .Case("safestack", Attribute::SafeStack)
56       .Case("shadowcallstack", Attribute::ShadowCallStack)
57       .Case("sanitize_address", Attribute::SanitizeAddress)
58       .Case("sanitize_hwaddress", Attribute::SanitizeHWAddress)
59       .Case("sanitize_memory", Attribute::SanitizeMemory)
60       .Case("sanitize_thread", Attribute::SanitizeThread)
61       .Case("ssp", Attribute::StackProtect)
62       .Case("sspreq", Attribute::StackProtectReq)
63       .Case("sspstrong", Attribute::StackProtectStrong)
64       .Case("strictfp", Attribute::StrictFP)
65       .Case("uwtable", Attribute::UWTable)
66       .Default(Attribute::None);
67 }
68 
69 /// If F has any forced attributes given on the command line, add them.
70 static void addForcedAttributes(Function &F) {
71   for (auto &S : ForceAttributes) {
72     auto KV = StringRef(S).split(':');
73     if (KV.first != F.getName())
74       continue;
75 
76     auto Kind = parseAttrKind(KV.second);
77     if (Kind == Attribute::None) {
78       LLVM_DEBUG(dbgs() << "ForcedAttribute: " << KV.second
79                         << " unknown or not handled!\n");
80       continue;
81     }
82     if (F.hasFnAttribute(Kind))
83       continue;
84     F.addFnAttr(Kind);
85   }
86 }
87 
88 PreservedAnalyses ForceFunctionAttrsPass::run(Module &M,
89                                               ModuleAnalysisManager &) {
90   if (ForceAttributes.empty())
91     return PreservedAnalyses::all();
92 
93   for (Function &F : M.functions())
94     addForcedAttributes(F);
95 
96   // Just conservatively invalidate analyses, this isn't likely to be important.
97   return PreservedAnalyses::none();
98 }
99 
100 namespace {
101 struct ForceFunctionAttrsLegacyPass : public ModulePass {
102   static char ID; // Pass identification, replacement for typeid
103   ForceFunctionAttrsLegacyPass() : ModulePass(ID) {
104     initializeForceFunctionAttrsLegacyPassPass(
105         *PassRegistry::getPassRegistry());
106   }
107 
108   bool runOnModule(Module &M) override {
109     if (ForceAttributes.empty())
110       return false;
111 
112     for (Function &F : M.functions())
113       addForcedAttributes(F);
114 
115     // Conservatively assume we changed something.
116     return true;
117   }
118 };
119 }
120 
121 char ForceFunctionAttrsLegacyPass::ID = 0;
122 INITIALIZE_PASS(ForceFunctionAttrsLegacyPass, "forceattrs",
123                 "Force set function attributes", false, false)
124 
125 Pass *llvm::createForceFunctionAttrsLegacyPass() {
126   return new ForceFunctionAttrsLegacyPass();
127 }
128