LLVM  13.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/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"
16 #include "llvm/Support/Debug.h"
18 using namespace llvm;
19 
20 #define DEBUG_TYPE "forceattrs"
21 
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 
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 
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("nosanitize_coverage", Attribute::NoSanitizeCoverage)
57  .Case("optforfuzzing", Attribute::OptForFuzzing)
58  .Case("optnone", Attribute::OptimizeNone)
59  .Case("optsize", Attribute::OptimizeForSize)
60  .Case("readnone", Attribute::ReadNone)
61  .Case("readonly", Attribute::ReadOnly)
62  .Case("argmemonly", Attribute::ArgMemOnly)
63  .Case("returns_twice", Attribute::ReturnsTwice)
64  .Case("safestack", Attribute::SafeStack)
65  .Case("shadowcallstack", Attribute::ShadowCallStack)
66  .Case("sanitize_address", Attribute::SanitizeAddress)
67  .Case("sanitize_hwaddress", Attribute::SanitizeHWAddress)
68  .Case("sanitize_memory", Attribute::SanitizeMemory)
69  .Case("sanitize_thread", Attribute::SanitizeThread)
70  .Case("sanitize_memtag", Attribute::SanitizeMemTag)
71  .Case("speculative_load_hardening", Attribute::SpeculativeLoadHardening)
72  .Case("ssp", Attribute::StackProtect)
73  .Case("sspreq", Attribute::StackProtectReq)
74  .Case("sspstrong", Attribute::StackProtectStrong)
75  .Case("strictfp", Attribute::StrictFP)
76  .Case("uwtable", Attribute::UWTable)
77  .Case("vscale_range", Attribute::VScaleRange)
79 }
80 
81 /// If F has any forced attributes given on the command line, add them.
82 /// If F has any forced remove attributes given on the command line, remove
83 /// them. When both force and force-remove are given to a function, the latter
84 /// takes precedence.
85 static void forceAttributes(Function &F) {
86  auto ParseFunctionAndAttr = [&](StringRef S) {
87  auto Kind = Attribute::None;
88  auto KV = StringRef(S).split(':');
89  if (KV.first != F.getName())
90  return Kind;
91  Kind = parseAttrKind(KV.second);
92  if (Kind == Attribute::None) {
93  LLVM_DEBUG(dbgs() << "ForcedAttribute: " << KV.second
94  << " unknown or not handled!\n");
95  }
96  return Kind;
97  };
98 
99  for (auto &S : ForceAttributes) {
100  auto Kind = ParseFunctionAndAttr(S);
101  if (Kind == Attribute::None || F.hasFnAttribute(Kind))
102  continue;
103  F.addFnAttr(Kind);
104  }
105 
106  for (auto &S : ForceRemoveAttributes) {
107  auto Kind = ParseFunctionAndAttr(S);
108  if (Kind == Attribute::None || !F.hasFnAttribute(Kind))
109  continue;
110  F.removeFnAttr(Kind);
111  }
112 }
113 
114 static bool hasForceAttributes() {
115  return !ForceAttributes.empty() || !ForceRemoveAttributes.empty();
116 }
117 
120  if (!hasForceAttributes())
121  return PreservedAnalyses::all();
122 
123  for (Function &F : M.functions())
125 
126  // Just conservatively invalidate analyses, this isn't likely to be important.
127  return PreservedAnalyses::none();
128 }
129 
130 namespace {
131 struct ForceFunctionAttrsLegacyPass : public ModulePass {
132  static char ID; // Pass identification, replacement for typeid
133  ForceFunctionAttrsLegacyPass() : ModulePass(ID) {
136  }
137 
138  bool runOnModule(Module &M) override {
139  if (!hasForceAttributes())
140  return false;
141 
142  for (Function &F : M.functions())
144 
145  // Conservatively assume we changed something.
146  return true;
147  }
148 };
149 }
150 
152 INITIALIZE_PASS(ForceFunctionAttrsLegacyPass, "forceattrs",
153  "Force set function attributes", false, false)
154 
156  return new ForceFunctionAttrsLegacyPass();
157 }
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
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::StringSwitch::Default
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
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::MCID::Convergent
@ Convergent
Definition: MCInstrDesc.h:182
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:122
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:132
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:727
parseAttrKind
static Attribute::AttrKind parseAttrKind(StringRef Kind)
Definition: ForceFunctionAttrs.cpp:36
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
ForceFunctionAttrs.h
hasForceAttributes
static bool hasForceAttributes()
Definition: ForceFunctionAttrs.cpp:114
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:85
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::Attribute::AttrKind
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:71
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::CallingConv::Cold
@ Cold
Definition: CallingConv.h:48
llvm::ForceFunctionAttrsPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
Definition: ForceFunctionAttrs.cpp:118
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
JumpTable
MIPS Relocation Principles In there are several elements of the llvm::ISD::NodeType enum that deal with addresses and or relocations These are defined in include llvm Target TargetSelectionDAG td JumpTable
Definition: Relocation.txt:6
Function.h
StringSwitch.h
llvm::orc::ReadOnly
static constexpr sys::Memory::ProtectionFlags ReadOnly
Definition: DebugObjectManagerPlugin.cpp:111
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::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
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:152
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38
llvm::cl::list
Definition: CommandLine.h:1630