LLVM  10.0.0svn
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/Support/Debug.h"
16 using namespace llvm;
17 
18 #define DEBUG_TYPE "forceattrs"
19 
21  ForceAttributes("force-attribute", cl::Hidden,
22  cl::desc("Add an attribute to a function. This should be a "
23  "pair of 'function-name:attribute-name', for "
24  "example -force-attribute=foo:noinline. This "
25  "option can be specified multiple times."));
26 
29  .Case("alwaysinline", Attribute::AlwaysInline)
30  .Case("builtin", Attribute::Builtin)
31  .Case("cold", Attribute::Cold)
32  .Case("convergent", Attribute::Convergent)
33  .Case("inlinehint", Attribute::InlineHint)
34  .Case("jumptable", Attribute::JumpTable)
35  .Case("minsize", Attribute::MinSize)
36  .Case("naked", Attribute::Naked)
37  .Case("nobuiltin", Attribute::NoBuiltin)
38  .Case("noduplicate", Attribute::NoDuplicate)
39  .Case("noimplicitfloat", Attribute::NoImplicitFloat)
40  .Case("noinline", Attribute::NoInline)
41  .Case("nonlazybind", Attribute::NonLazyBind)
42  .Case("noredzone", Attribute::NoRedZone)
43  .Case("noreturn", Attribute::NoReturn)
44  .Case("nocf_check", Attribute::NoCfCheck)
45  .Case("norecurse", Attribute::NoRecurse)
46  .Case("nounwind", Attribute::NoUnwind)
47  .Case("optforfuzzing", Attribute::OptForFuzzing)
48  .Case("optnone", Attribute::OptimizeNone)
49  .Case("optsize", Attribute::OptimizeForSize)
50  .Case("readnone", Attribute::ReadNone)
51  .Case("readonly", Attribute::ReadOnly)
52  .Case("argmemonly", Attribute::ArgMemOnly)
53  .Case("returns_twice", Attribute::ReturnsTwice)
54  .Case("safestack", Attribute::SafeStack)
55  .Case("shadowcallstack", Attribute::ShadowCallStack)
56  .Case("sanitize_address", Attribute::SanitizeAddress)
57  .Case("sanitize_hwaddress", Attribute::SanitizeHWAddress)
58  .Case("sanitize_memory", Attribute::SanitizeMemory)
59  .Case("sanitize_thread", Attribute::SanitizeThread)
60  .Case("sanitize_memtag", Attribute::SanitizeMemTag)
61  .Case("speculative_load_hardening", Attribute::SpeculativeLoadHardening)
62  .Case("ssp", Attribute::StackProtect)
63  .Case("sspreq", Attribute::StackProtectReq)
64  .Case("sspstrong", Attribute::StackProtectStrong)
65  .Case("strictfp", Attribute::StrictFP)
66  .Case("uwtable", Attribute::UWTable)
68 }
69 
70 /// If F has any forced attributes given on the command line, add them.
72  for (auto &S : ForceAttributes) {
73  auto KV = StringRef(S).split(':');
74  if (KV.first != F.getName())
75  continue;
76 
77  auto Kind = parseAttrKind(KV.second);
78  if (Kind == Attribute::None) {
79  LLVM_DEBUG(dbgs() << "ForcedAttribute: " << KV.second
80  << " unknown or not handled!\n");
81  continue;
82  }
83  if (F.hasFnAttribute(Kind))
84  continue;
85  F.addFnAttr(Kind);
86  }
87 }
88 
91  if (ForceAttributes.empty())
92  return PreservedAnalyses::all();
93 
94  for (Function &F : M.functions())
96 
97  // Just conservatively invalidate analyses, this isn't likely to be important.
98  return PreservedAnalyses::none();
99 }
100 
101 namespace {
102 struct ForceFunctionAttrsLegacyPass : public ModulePass {
103  static char ID; // Pass identification, replacement for typeid
104  ForceFunctionAttrsLegacyPass() : ModulePass(ID) {
107  }
108 
109  bool runOnModule(Module &M) override {
110  if (ForceAttributes.empty())
111  return false;
112 
113  for (Function &F : M.functions())
115 
116  // Conservatively assume we changed something.
117  return true;
118  }
119 };
120 }
121 
123 INITIALIZE_PASS(ForceFunctionAttrsLegacyPass, "forceattrs",
124  "Force set function attributes", false, false)
125 
127  return new ForceFunctionAttrsLegacyPass();
128 }
Super simple passes to force specific function attrs from the commandline into the IR for debugging p...
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:80
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:323
F(f)
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
static void addForcedAttributes(Function &F)
If F has any forced attributes given on the command line, add them.
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
No attributes have been set.
Definition: Attributes.h:72
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:156
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
iterator_range< iterator > functions()
Definition: Module.h:609
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
static Attribute::AttrKind parseAttrKind(StringRef Kind)
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."))
Module.h This file contains the declarations for the Module class.
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:696
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
INITIALIZE_PASS(ForceFunctionAttrsLegacyPass, "forceattrs", "Force set function attributes", false, false) Pass *llvm
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:224
void initializeForceFunctionAttrsLegacyPassPass(PassRegistry &)
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
Definition: Function.h:229
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
A container for analyses that lazily runs them and caches their results.
#define LLVM_DEBUG(X)
Definition: Debug.h:122
Pass * createForceFunctionAttrsLegacyPass()
Create a legacy pass manager instance of a pass to force function attrs.
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results...
Definition: Attributes.h:70