1 //===- ForceFunctionAttrs.cpp - Force function attrs for debugging --------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
10 #include "llvm/IR/Function.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/InitializePasses.h"
14 #include "llvm/Support/CommandLine.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 cl::list
<std::string
> ForceRemoveAttributes(
29 "force-remove-attribute", cl::Hidden
,
30 cl::desc("Remove an attribute from a function. This should be a "
31 "pair of 'function-name:attribute-name', for "
32 "example -force-remove-attribute=foo:noinline. This "
33 "option can be specified multiple times."));
35 /// If F has any forced attributes given on the command line, add them.
36 /// If F has any forced remove attributes given on the command line, remove
37 /// them. When both force and force-remove are given to a function, the latter
39 static void forceAttributes(Function
&F
) {
40 auto ParseFunctionAndAttr
= [&](StringRef S
) {
41 auto Kind
= Attribute::None
;
42 auto KV
= StringRef(S
).split(':');
43 if (KV
.first
!= F
.getName())
45 Kind
= Attribute::getAttrKindFromName(KV
.second
);
46 if (Kind
== Attribute::None
|| !Attribute::canUseAsFnAttr(Kind
)) {
47 LLVM_DEBUG(dbgs() << "ForcedAttribute: " << KV
.second
48 << " unknown or not a function attribute!\n");
53 for (auto &S
: ForceAttributes
) {
54 auto Kind
= ParseFunctionAndAttr(S
);
55 if (Kind
== Attribute::None
|| F
.hasFnAttribute(Kind
))
60 for (auto &S
: ForceRemoveAttributes
) {
61 auto Kind
= ParseFunctionAndAttr(S
);
62 if (Kind
== Attribute::None
|| !F
.hasFnAttribute(Kind
))
68 static bool hasForceAttributes() {
69 return !ForceAttributes
.empty() || !ForceRemoveAttributes
.empty();
72 PreservedAnalyses
ForceFunctionAttrsPass::run(Module
&M
,
73 ModuleAnalysisManager
&) {
74 if (!hasForceAttributes())
75 return PreservedAnalyses::all();
77 for (Function
&F
: M
.functions())
80 // Just conservatively invalidate analyses, this isn't likely to be important.
81 return PreservedAnalyses::none();
85 struct ForceFunctionAttrsLegacyPass
: public ModulePass
{
86 static char ID
; // Pass identification, replacement for typeid
87 ForceFunctionAttrsLegacyPass() : ModulePass(ID
) {
88 initializeForceFunctionAttrsLegacyPassPass(
89 *PassRegistry::getPassRegistry());
92 bool runOnModule(Module
&M
) override
{
93 if (!hasForceAttributes())
96 for (Function
&F
: M
.functions())
99 // Conservatively assume we changed something.
105 char ForceFunctionAttrsLegacyPass::ID
= 0;
106 INITIALIZE_PASS(ForceFunctionAttrsLegacyPass
, "forceattrs",
107 "Force set function attributes", false, false)
109 Pass
*llvm::createForceFunctionAttrsLegacyPass() {
110 return new ForceFunctionAttrsLegacyPass();