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/IR/Function.h"
11 #include "llvm/IR/Module.h"
12 #include "llvm/Support/CommandLine.h"
13 #include "llvm/Support/Debug.h"
14 #include "llvm/Support/raw_ostream.h"
15 using namespace llvm;
16 
17 #define DEBUG_TYPE "forceattrs"
18 
19 static cl::list<std::string>
20     ForceAttributes("force-attribute", cl::Hidden,
21                     cl::desc("Add an attribute to a function. This should be a "
22                              "pair of 'function-name:attribute-name', for "
23                              "example -force-attribute=foo:noinline. This "
24                              "option can be specified multiple times."));
25 
26 static cl::list<std::string> ForceRemoveAttributes(
27     "force-remove-attribute", cl::Hidden,
28     cl::desc("Remove an attribute from a function. This should be a "
29              "pair of 'function-name:attribute-name', for "
30              "example -force-remove-attribute=foo:noinline. This "
31              "option can be specified multiple times."));
32 
33 /// If F has any forced attributes given on the command line, add them.
34 /// If F has any forced remove attributes given on the command line, remove
35 /// them. When both force and force-remove are given to a function, the latter
36 /// takes precedence.
37 static void forceAttributes(Function &F) {
38   auto ParseFunctionAndAttr = [&](StringRef S) {
39     auto Kind = Attribute::None;
40     auto KV = StringRef(S).split(':');
41     if (KV.first != F.getName())
42       return Kind;
43     Kind = Attribute::getAttrKindFromName(KV.second);
44     if (Kind == Attribute::None || !Attribute::canUseAsFnAttr(Kind)) {
45       LLVM_DEBUG(dbgs() << "ForcedAttribute: " << KV.second
46                         << " unknown or not a function attribute!\n");
47     }
48     return Kind;
49   };
50 
51   for (const auto &S : ForceAttributes) {
52     auto Kind = ParseFunctionAndAttr(S);
53     if (Kind == Attribute::None || F.hasFnAttribute(Kind))
54       continue;
55     F.addFnAttr(Kind);
56   }
57 
58   for (const auto &S : ForceRemoveAttributes) {
59     auto Kind = ParseFunctionAndAttr(S);
60     if (Kind == Attribute::None || !F.hasFnAttribute(Kind))
61       continue;
62     F.removeFnAttr(Kind);
63   }
64 }
65 
66 static bool hasForceAttributes() {
67   return !ForceAttributes.empty() || !ForceRemoveAttributes.empty();
68 }
69 
70 PreservedAnalyses ForceFunctionAttrsPass::run(Module &M,
71                                               ModuleAnalysisManager &) {
72   if (!hasForceAttributes())
73     return PreservedAnalyses::all();
74 
75   for (Function &F : M.functions())
76     forceAttributes(F);
77 
78   // Just conservatively invalidate analyses, this isn't likely to be important.
79   return PreservedAnalyses::none();
80 }
81