xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/Transforms/IPO/ForceFunctionAttrs.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
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 
parseAttrKind(StringRef Kind)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("optforfuzzing", Attribute::OptForFuzzing)
57       .Case("optnone", Attribute::OptimizeNone)
58       .Case("optsize", Attribute::OptimizeForSize)
59       .Case("readnone", Attribute::ReadNone)
60       .Case("readonly", Attribute::ReadOnly)
61       .Case("argmemonly", Attribute::ArgMemOnly)
62       .Case("returns_twice", Attribute::ReturnsTwice)
63       .Case("safestack", Attribute::SafeStack)
64       .Case("shadowcallstack", Attribute::ShadowCallStack)
65       .Case("sanitize_address", Attribute::SanitizeAddress)
66       .Case("sanitize_hwaddress", Attribute::SanitizeHWAddress)
67       .Case("sanitize_memory", Attribute::SanitizeMemory)
68       .Case("sanitize_thread", Attribute::SanitizeThread)
69       .Case("sanitize_memtag", Attribute::SanitizeMemTag)
70       .Case("speculative_load_hardening", Attribute::SpeculativeLoadHardening)
71       .Case("ssp", Attribute::StackProtect)
72       .Case("sspreq", Attribute::StackProtectReq)
73       .Case("sspstrong", Attribute::StackProtectStrong)
74       .Case("strictfp", Attribute::StrictFP)
75       .Case("uwtable", Attribute::UWTable)
76       .Case("vscale_range", Attribute::VScaleRange)
77       .Default(Attribute::None);
78 }
79 
80 /// If F has any forced attributes given on the command line, add them.
81 /// If F has any forced remove attributes given on the command line, remove
82 /// them. When both force and force-remove are given to a function, the latter
83 /// takes precedence.
forceAttributes(Function & F)84 static void forceAttributes(Function &F) {
85   auto ParseFunctionAndAttr = [&](StringRef S) {
86     auto Kind = Attribute::None;
87     auto KV = StringRef(S).split(':');
88     if (KV.first != F.getName())
89       return Kind;
90     Kind = parseAttrKind(KV.second);
91     if (Kind == Attribute::None) {
92       LLVM_DEBUG(dbgs() << "ForcedAttribute: " << KV.second
93                         << " unknown or not handled!\n");
94     }
95     return Kind;
96   };
97 
98   for (auto &S : ForceAttributes) {
99     auto Kind = ParseFunctionAndAttr(S);
100     if (Kind == Attribute::None || F.hasFnAttribute(Kind))
101       continue;
102     F.addFnAttr(Kind);
103   }
104 
105   for (auto &S : ForceRemoveAttributes) {
106     auto Kind = ParseFunctionAndAttr(S);
107     if (Kind == Attribute::None || !F.hasFnAttribute(Kind))
108       continue;
109     F.removeFnAttr(Kind);
110   }
111 }
112 
hasForceAttributes()113 static bool hasForceAttributes() {
114   return !ForceAttributes.empty() || !ForceRemoveAttributes.empty();
115 }
116 
run(Module & M,ModuleAnalysisManager &)117 PreservedAnalyses ForceFunctionAttrsPass::run(Module &M,
118                                               ModuleAnalysisManager &) {
119   if (!hasForceAttributes())
120     return PreservedAnalyses::all();
121 
122   for (Function &F : M.functions())
123     forceAttributes(F);
124 
125   // Just conservatively invalidate analyses, this isn't likely to be important.
126   return PreservedAnalyses::none();
127 }
128 
129 namespace {
130 struct ForceFunctionAttrsLegacyPass : public ModulePass {
131   static char ID; // Pass identification, replacement for typeid
ForceFunctionAttrsLegacyPass__anonb68c4fd30211::ForceFunctionAttrsLegacyPass132   ForceFunctionAttrsLegacyPass() : ModulePass(ID) {
133     initializeForceFunctionAttrsLegacyPassPass(
134         *PassRegistry::getPassRegistry());
135   }
136 
runOnModule__anonb68c4fd30211::ForceFunctionAttrsLegacyPass137   bool runOnModule(Module &M) override {
138     if (!hasForceAttributes())
139       return false;
140 
141     for (Function &F : M.functions())
142       forceAttributes(F);
143 
144     // Conservatively assume we changed something.
145     return true;
146   }
147 };
148 }
149 
150 char ForceFunctionAttrsLegacyPass::ID = 0;
151 INITIALIZE_PASS(ForceFunctionAttrsLegacyPass, "forceattrs",
152                 "Force set function attributes", false, false)
153 
createForceFunctionAttrsLegacyPass()154 Pass *llvm::createForceFunctionAttrsLegacyPass() {
155   return new ForceFunctionAttrsLegacyPass();
156 }
157