LLVM  14.0.0git
ForceFunctionAttrs.cpp
Go to the documentation of this file.
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 
10 #include "llvm/IR/Function.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/InitializePasses.h"
15 #include "llvm/Support/Debug.h"
17 using namespace llvm;
18 
19 #define DEBUG_TYPE "forceattrs"
20 
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 
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."));
34 
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
38 /// takes precedence.
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())
44  return Kind;
47  LLVM_DEBUG(dbgs() << "ForcedAttribute: " << KV.second
48  << " unknown or not a function attribute!\n");
49  }
50  return Kind;
51  };
52 
53  for (const auto &S : ForceAttributes) {
54  auto Kind = ParseFunctionAndAttr(S);
55  if (Kind == Attribute::None || F.hasFnAttribute(Kind))
56  continue;
57  F.addFnAttr(Kind);
58  }
59 
60  for (const auto &S : ForceRemoveAttributes) {
61  auto Kind = ParseFunctionAndAttr(S);
62  if (Kind == Attribute::None || !F.hasFnAttribute(Kind))
63  continue;
64  F.removeFnAttr(Kind);
65  }
66 }
67 
68 static bool hasForceAttributes() {
69  return !ForceAttributes.empty() || !ForceRemoveAttributes.empty();
70 }
71 
74  if (!hasForceAttributes())
75  return PreservedAnalyses::all();
76 
77  for (Function &F : M.functions())
79 
80  // Just conservatively invalidate analyses, this isn't likely to be important.
81  return PreservedAnalyses::none();
82 }
83 
84 namespace {
85 struct ForceFunctionAttrsLegacyPass : public ModulePass {
86  static char ID; // Pass identification, replacement for typeid
87  ForceFunctionAttrsLegacyPass() : ModulePass(ID) {
90  }
91 
92  bool runOnModule(Module &M) override {
93  if (!hasForceAttributes())
94  return false;
95 
96  for (Function &F : M.functions())
98 
99  // Conservatively assume we changed something.
100  return true;
101  }
102 };
103 }
104 
106 INITIALIZE_PASS(ForceFunctionAttrsLegacyPass, "forceattrs",
107  "Force set function attributes", false, false)
108 
110  return new ForceFunctionAttrsLegacyPass();
111 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
llvm::Function
Definition: Function.h:61
llvm::Attribute::getAttrKindFromName
static Attribute::AttrKind getAttrKindFromName(StringRef AttrName)
Definition: Attributes.cpp:222
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:158
Module.h
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
CommandLine.h
llvm::createForceFunctionAttrsLegacyPass
Pass * createForceFunctionAttrsLegacyPass()
Create a legacy pass manager instance of a pass to force function attrs.
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::StringRef::split
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:749
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
ForceFunctionAttrs.h
hasForceAttributes
static bool hasForceAttributes()
Definition: ForceFunctionAttrs.cpp:68
llvm::Attribute::canUseAsFnAttr
static bool canUseAsFnAttr(AttrKind Kind)
Definition: Attributes.cpp:505
llvm::Attribute::None
@ None
No attributes have been set.
Definition: Attributes.h:73
forceAttributes
static void forceAttributes(Function &F)
If F has any forced attributes given on the command line, add them.
Definition: ForceFunctionAttrs.cpp:39
ForceRemoveAttributes
static cl::list< std::string > ForceRemoveAttributes("force-remove-attribute", cl::Hidden, cl::desc("Remove an attribute from a function. This should be a " "pair of 'function-name:attribute-name', for " "example -force-remove-attribute=foo:noinline. This " "option can be specified multiple times."))
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::initializeForceFunctionAttrsLegacyPassPass
void initializeForceFunctionAttrsLegacyPassPass(PassRegistry &)
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::ForceFunctionAttrsPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
Definition: ForceFunctionAttrs.cpp:72
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
Function.h
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
ForceAttributes
static cl::list< std::string > ForceAttributes("force-attribute", cl::Hidden, cl::desc("Add an attribute to a function. This should be a " "pair of 'function-name:attribute-name', for " "example -force-attribute=foo:noinline. This " "option can be specified multiple times."))
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
LLVMContext.h
llvm::cl::desc
Definition: CommandLine.h:414
raw_ostream.h
InitializePasses.h
Debug.h
INITIALIZE_PASS
INITIALIZE_PASS(ForceFunctionAttrsLegacyPass, "forceattrs", "Force set function attributes", false, false) Pass *llvm
Definition: ForceFunctionAttrs.cpp:106
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37
llvm::cl::list
Definition: CommandLine.h:1642