LLVM  7.0.0svn
ForceFunctionAttrs.cpp
Go to the documentation of this file.
1 //===- ForceFunctionAttrs.cpp - Force function attrs for debugging --------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/IR/Function.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.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 
30  .Case("alwaysinline", Attribute::AlwaysInline)
31  .Case("builtin", Attribute::Builtin)
32  .Case("cold", Attribute::Cold)
33  .Case("convergent", Attribute::Convergent)
34  .Case("inlinehint", Attribute::InlineHint)
35  .Case("jumptable", Attribute::JumpTable)
36  .Case("minsize", Attribute::MinSize)
37  .Case("naked", Attribute::Naked)
38  .Case("nobuiltin", Attribute::NoBuiltin)
39  .Case("noduplicate", Attribute::NoDuplicate)
40  .Case("noimplicitfloat", Attribute::NoImplicitFloat)
41  .Case("noinline", Attribute::NoInline)
42  .Case("nonlazybind", Attribute::NonLazyBind)
43  .Case("noredzone", Attribute::NoRedZone)
44  .Case("noreturn", Attribute::NoReturn)
45  .Case("norecurse", Attribute::NoRecurse)
46  .Case("nounwind", Attribute::NoUnwind)
47  .Case("optnone", Attribute::OptimizeNone)
48  .Case("optsize", Attribute::OptimizeForSize)
49  .Case("readnone", Attribute::ReadNone)
50  .Case("readonly", Attribute::ReadOnly)
51  .Case("argmemonly", Attribute::ArgMemOnly)
52  .Case("returns_twice", Attribute::ReturnsTwice)
53  .Case("safestack", Attribute::SafeStack)
54  .Case("sanitize_address", Attribute::SanitizeAddress)
55  .Case("sanitize_hwaddress", Attribute::SanitizeHWAddress)
56  .Case("sanitize_memory", Attribute::SanitizeMemory)
57  .Case("sanitize_thread", Attribute::SanitizeThread)
58  .Case("ssp", Attribute::StackProtect)
59  .Case("sspreq", Attribute::StackProtectReq)
60  .Case("sspstrong", Attribute::StackProtectStrong)
61  .Case("strictfp", Attribute::StrictFP)
62  .Case("uwtable", Attribute::UWTable)
64 }
65 
66 /// If F has any forced attributes given on the command line, add them.
68  for (auto &S : ForceAttributes) {
69  auto KV = StringRef(S).split(':');
70  if (KV.first != F.getName())
71  continue;
72 
73  auto Kind = parseAttrKind(KV.second);
74  if (Kind == Attribute::None) {
75  DEBUG(dbgs() << "ForcedAttribute: " << KV.second
76  << " unknown or not handled!\n");
77  continue;
78  }
79  if (F.hasFnAttribute(Kind))
80  continue;
81  F.addFnAttr(Kind);
82  }
83 }
84 
87  if (ForceAttributes.empty())
88  return PreservedAnalyses::all();
89 
90  for (Function &F : M.functions())
92 
93  // Just conservatively invalidate analyses, this isn't likely to be important.
94  return PreservedAnalyses::none();
95 }
96 
97 namespace {
98 struct ForceFunctionAttrsLegacyPass : public ModulePass {
99  static char ID; // Pass identification, replacement for typeid
100  ForceFunctionAttrsLegacyPass() : ModulePass(ID) {
103  }
104 
105  bool runOnModule(Module &M) override {
106  if (ForceAttributes.empty())
107  return false;
108 
109  for (Function &F : M.functions())
111 
112  // Conservatively assume we changed something.
113  return true;
114  }
115 };
116 }
117 
119 INITIALIZE_PASS(ForceFunctionAttrsLegacyPass, "forceattrs",
120  "Force set function attributes", false, false)
121 
123  return new ForceFunctionAttrsLegacyPass();
124 }
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:81
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:302
F(f)
static void addForcedAttributes(Function &F)
If F has any forced attributes given on the command line, add them.
No attributes have been set.
Definition: Attributes.h:72
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(const char(&S)[N], const T &Value)
Definition: StringSwitch.h:74
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:43
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:583
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
static Attribute::AttrKind parseAttrKind(StringRef Kind)
LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(const T &Value) const
Definition: StringSwitch.h:244
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:727
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:224
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:225
const unsigned Kind
void initializeForceFunctionAttrsLegacyPassPass(PassRegistry &)
#define DEBUG(X)
Definition: Debug.h:118
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
Definition: Function.h:211
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
A container for analyses that lazily runs them and caches their results.
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